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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

put watermark (image stamp) on uploaded image's


Go to End


2 Posts   3110 Views

Avatar
trumi

Community Member, 14 Posts

27 October 2010 at 9:06pm

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

Avatar
BP

Community Member, 25 Posts

29 February 2012 at 4:16am

Need help with this - does not work says:

"There was a problem with the upload"

try again.

Any hints why ? 2.4.5