Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Custom Image decorator – how do I set a filename?

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: 596 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    Custom Image decorator – how do I set a filename? Link to this post

    Hi there

    I have a custom image decorator for creating watermarks and would like to set the filename dynamically in php for each image? How would I do this?

    These are the functions in my image decorator:

    public function CroppedCopyrightImage($width, $height)
    {
          if ($this->owner->ID && $this->owner->Filename && Director::fileExists($this->owner->Filename))
          {
             $cacheFile = $this->owner->cacheFilename("CroppedCopyrightImage", $width, $height);
             
           if (!file_exists("../".$cacheFile) || isset ($_GET['flush']))
           {
              $this->generateCroppedCopyrightImage($width, $height);
           }

           return new Image_Cached($cacheFile);
          }
    }

    public function generateCroppedCopyrightImage($width, $height)
    {
          $cacheFile = $this->owner->cacheFilename("CroppedCopyrightImage", $width, $height);
          
          $gd = new CustomGD("../".$this->owner->Filename);
          
          if ($gd->hasGD())
          {
             $gd = $gd->croppedResize($width,$height)->CopyrightImage($this->owner->CopyrightText);
             
           if ($gd)
           {
              $gd->writeTo("../".$cacheFile);
           }
          }
    }

    And this is the function in my extended GD class for creating the watermark:

    class CustomGD extends GD
    {
    public function watermarkImage(){
          
          $watermark = imagecreatefrompng(Director::absoluteBaseURL().'mysite/images/watermark_2.png');
          $watermark_width = imagesx($watermark);
          $watermark_height = imagesy($watermark);
          imagealphablending($watermark, false);
          imagesavealpha($watermark, true);
          
          $dest_x = 0;
          $dest_y = 0;
          
          if ($this->gd){
             imagecopymerge($this->gd, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 15);
          
          $output = new GD();
          $output->setGD($this->gd);
          
          return $output;
          
          }
       }
       
       public function CopyrightImage($text = ''){
          
          $font = $_SERVER['DOCUMENT_ROOT'] . '/ItalienskVinguideUpgrade2_4_5/mysite/code/arial.ttf';
          
          if ($this->gd){
             
             $im = $this->gd;
             $width = imagesx($im);
             $height = imagesy($im);
             $fontSize = 8;
             $angle = 0;
             imageSaveAlpha($im, true);
             ImageAlphaBlending($im, true);
             
             $black = imagecolorallocatealpha($im, 153, 19, 31, 0);
             
             imageSaveAlpha($im, true);
             ImageAlphaBlending($im, true);
             
             $dimensions = imagettfbbox($fontSize, $angle, $font, $text);
             $textWidth = abs($dimensions[4] - $dimensions[0]);
             
             imagettftext($this->gd, $fontSize, $angle, ($width - $textWidth - 10), 15, $black, $font, $text);
             
             $output = new GD();
             $output->setGD($this->gd);
          
             return $output;
          
          }
       }
    }

    I hope someone can help me?

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: Custom Image decorator – how do I set a filename? Link to this post

    I'm doing something similar at the moment.

    Object::useCustomClass('Image', 'RSImage');
    Object::add_extension('Image', 'RSImageDecorator');

    class RSImage extends Image {
       // @todo overload Filename, Name, etc.
    }

    class RSImageDecorator extends DataObjectDecorator {
       function onAfterUpload() {
          $imageObj = $this->owner;
          if( !empty($imageObj) ) {
             // @todo set Filename
          }
       }
    }

    Hope this helps.

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