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

Custom Image decorator – how do I set a filename?


Reply

2 Posts   693 Views

Avatar
joelg

10 March 2012 at 12:56am Community Member, 129 Posts

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?

Avatar
Devlin

10 March 2012 at 5:28am Community Member, 215 Posts

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.