Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How can I use GD Functions? (not on the fly)


Reply

8 Posts   922 Views

Avatar
Miroke

8 October 2011 at 12:45pm Community Member, 9 Posts

I've been using on the fly GD Functions in my templates and that's great!

But now i need to use it inside my backend personal function and i don't know how to call it.

-------------------------
class Example extends ImageGalleryPage {

static $has_one = array(
'Picture' => 'ExampleThumb'
);

public function MyFunction() {

$query = DataObject::get("Example");

foreach($query as $object)
{
if ($object->PictureID)
{
$image = DataObject::get_by_id("File", $object->PictureID);
$display_url = $image->URL;

************************************************************
*** OK! How should i use GD Functions to change my image right now? ***
************************************************************
}
}
}

}

class ExampleThumb extends Image {

function generateGoodOne($gd) {
$gd->setQuality(100);
return $gd->resizeByWidth(150);
}

}
-------------------------

Avatar
biapar

9 October 2011 at 1:26am Forum Moderator, 435 Posts

You need to create a class:

class SpecialDataPage_BannerImage extends Image {

function generatePageBanner($gd) {
$gd->setQuality(90);
//return $gd->paddedResize(678,300); original
return $gd->paddedResize(970,300);
}

}

So in other class:

class SpecialDataPage extends Page {

static $has_one = array(
'Banner1' => 'SpecialDataPage_BannerImage',
'Banner2' => 'SpecialDataPage_BannerImage',
'Banner3' => 'SpecialDataPage_BannerImage',
'Banner4' => 'SpecialDataPage_BannerImage',
);

....

$fields->addFieldToTab("Root.Content.Banners", new ImageField("Banner1", "Immagine Banner 1"));

}

and in template:

<% control Page(SpecialDataPage) %>
<!-- first slide -->
<% if Banner1 %>
<div>
<img src="$Banner1.URL" width="980" alt="Header Image" />

</div>
<% end_if %>
<% end_control %>

Avatar
Miroke

9 October 2011 at 2:51pm Community Member, 9 Posts

That's exactly what I don't want =/

This is an "on the fly" example of GD Function working in template.

I need to use it directly on the backend!

Sure I could write classic PHP code to do the work but...

How can I use "setQuality" and "resizeByWidth" inside my DataObject loop?

Avatar
Willr

10 October 2011 at 9:11am Forum Moderator, 5511 Posts

Well first you need to get the Image object and not the file as Images can be resized, files cannot.

$image = DataObject::get_by_id("Image", $object->PictureID);

Then once you have the image object you can call any of the methods you wish (http://api.silverstripe.org/2.4/sapphire/filesystem/Image.html).

$example = $image->generateFormattedImage('ResizedImage', 100, 40);

If you want the GD methods directly you can create a new GD object with the filename of the image

$gd = new GD(Director::baseFolder()."/" . $this->Filename);
$gd->rotatePixelByPixel(90);
...

Consult the API documentation for more information.

Avatar
Miroke

11 October 2011 at 6:39am Community Member, 9 Posts

Working like a charm !!!

$query = DataObject::get("Example");

foreach($query as $object)
{
if ($object->FotoID)
{
$image = DataObject::get_by_id("Image", $object->FotoID);
$image_url = $image->URL;
$image->generateFormattedImage('ResizedImage', 150, 89);
$thumb_url = $image->cacheFilename('ResizedImage', 150, 89);
}
}

Thanks biapar and especially Willr ;)

Avatar
Carbon Crayon

11 October 2011 at 10:19am Community Member, 598 Posts

There is also a Tutorial on this on SSBits: http://www.ssbits.com/tutorials/2010/rotating-and-greyscaling-images-using-gd-and-decorators/

Aram

Avatar
Miroke

11 October 2011 at 3:59pm Community Member, 9 Posts

Aram, I'm a very old fan from SSbits Blog and certainly this post like so many others helped me a lot to enhance my SilverStripe limits ;)

When I finaly finish my project, I'll be the next site of the month >.<

Avatar
Carbon Crayon

12 October 2011 at 12:56am Community Member, 598 Posts

Sweet, I look forward to it :)