Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » $gd->writeTo function

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: 594 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    $gd->writeTo function Link to this post

    I'm looking for more information on writeTo() function. I want to be able to change the names of resampled images for SEO purposes and I'm wondering how to properly set the the writeTo function on an extended image class:

    function generateSmallimage($gd) {
    $gd->writeTo("this.jpg");
    $gd->resizeRatio(150,150);
    return $gd;
    }

    The class documentation doesn't provide much direction on the actual use of the function.

    EDIT

    After a little more playing around with code pulled from the IMage Object I've gotten this far

    function generateSmallimage($gd) {
       
        //Get asset Folder
        $folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/";
       
        //Get Current Filename
        $filename = $this->Name;
       
        //Get Extension
        $ext = strtolower(substr($filename, strrpos($filename,'.')+1));
       
        //set new path and filename;
        $newFilename = $folder . "_resampled/my-file-name." . $ext;
             
        $smallImage = $gd->resizeRatio(150,150);
        $smallImage = $smallImage->writeTo($newFilename);
       
       
    return $smallImage;
    }

    This will generate a resized image in the correct folder, but it still returns a link to a filename titled smallImage-originalFilename

    Cheers,

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