Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » put watermark (image stamp) on uploaded image's

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

Page: 1
Go to End
Author Topic: 1993 Views
  • trumi
    Avatar
    Community Member
    14 Posts

    put watermark (image stamp) on uploaded image's Link to this post

    Hello i would like to share this with you, there is a lot of tutrials writen on net for watermarking image, but i dident find example for silverstripe:

    create ImageDecorator.php (you can named as you want) put in this code

    <?php
    class ImageDecorator extends DataObjectDecorator {

    /**
    * Put on uploaded images watermark
    */
    function onAfterWrite(){
    parent::onAfterWrite();

    //absolute path to watermark in mycase is watermark located in http://localhost/ak/mysite/code/images/watermark.png
    $watermark = imagecreatefrompng(Director::absoluteBaseURL().'mysite/code/images/watermark.png');

    //get watermark width
    $watermark_width = imagesx($watermark);

    //get watermark height
    $watermark_height = imagesy($watermark);

    //imagecreatetruecolor() returns an image identifier representing a black image of the specified size. http://si.php.net/manual/en/function.imagecreatetruecolor.php
    $image = imagecreatetruecolor($watermark_width, $watermark_height);

    //imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename. http://si.php.net/manual/en/function.imagecreatefromjpeg.php
    $image = imagecreatefromjpeg(str_replace('/','\\',Director::baseFolder().'/'.$this->owner->Filename));

    //array getimagesize ( string $filename [, array &$imageinfo ] ) http://si.php.net/manual/en/function.getimagesize.php
    $size = getimagesize(str_replace('/','\\',Director::baseFolder().'/'.$this->owner->Filename));

    //x-coordinate of destination point.
    $dest_x = $size[0] - $watermark_width - 5;

    //y-coordinate of destination point.
    $dest_y = $size[1] - $watermark_height - 5;

    //imagecopymerge — Copy and merge part of an image http://si.php.net/manual/en/function.imagecopymerge.php
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

    //imagejpeg — Output image to browser or file , I override uploaded image with the watermarked image http://si.php.net/manual/en/function.imagejpeg.php
    imagejpeg($image,str_replace('/','\\',Director::baseFolder().'/'.$this->owner->Filename));

    //imagedestroy — Destroy an image http://si.php.net/manual/en/function.imagedestroy.php
    imagedestroy($image);
    imagedestroy($watermark);
    }
    }
    ?>

    in _config.php insert this code

    Object::add_extension('Image','ImageDecorator');

    that's all, it's work fine for me

  • Zyzzza
    Avatar
    Community Member
    25 Posts

    Re: put watermark (image stamp) on uploaded image's Link to this post

    Need help with this - does not work says:

    "There was a problem with the upload"

    try again.

    Any hints why ? 2.4.5

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