Jump to:

23478 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » How can I use GD Functions? (not on the fly)

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

Page: 1
Go to End
Author Topic: 897 Views
  • Miroke
    Avatar
    Community Member
    9 Posts

    How can I use GD Functions? (not on the fly) Link to this post

    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);
    }

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

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    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 %>

  • Miroke
    Avatar
    Community Member
    9 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    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.

  • Miroke
    Avatar
    Community Member
    9 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    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 ;)

  • Aram
    Avatar
    Community Member
    598 Posts
  • Miroke
    Avatar
    Community Member
    9 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    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 >.<

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: How can I use GD Functions? (not on the fly) Link to this post

    Sweet, I look forward to it

    897 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.