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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

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


Go to End


8 Posts   2015 Views

Avatar
Miroke

Community Member, 9 Posts

8 October 2011 at 12:45pm

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

Forum Moderator, 435 Posts

9 October 2011 at 1:26am

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

Community Member, 9 Posts

9 October 2011 at 2:51pm

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

Forum Moderator, 5523 Posts

10 October 2011 at 9:11am

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

Community Member, 9 Posts

11 October 2011 at 6:39am

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

Community Member, 598 Posts

11 October 2011 at 10:19am

Avatar
Miroke

Community Member, 9 Posts

11 October 2011 at 3:59pm

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

Community Member, 598 Posts

12 October 2011 at 12:56am

Sweet, I look forward to it :)