Jump to:

5554 Posts in 1742 Topics by 1227 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: 2031 Views
  • trumi
    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

    class ImageDecorator extends DataObjectDecorator {

    * Put on uploaded images watermark
    function 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

    //imagedestroy — Destroy an image http://si.php.net/manual/en/function.imagedestroy.php

    in _config.php insert this code


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

  • Zyzzza
    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

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.