Jump to:

22988 Posts in 11842 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Watermark in the picture.

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: 701 Views
  • MarekStu
    Avatar
    Community Member
    4 Posts

    Watermark in the picture. Link to this post

    Hello, I try to display the watermark on the photo. The code I created a watermark displays, unfortunately the full-page displays. Lose content, etc.
    Can you tell me how to properly insert and displays a watermark in the picture? Thank you.
    Sorry about my English ...

    class Fotka extends DataObject {
    static $db = array (
    'Popis' => 'Text',
    'DatumVlozeni' => 'Date'
    );

    static $has_one = array (
    'Attachment' => 'Image',
    'PodKategorie2' => 'PodKategorie2'
    );

    ...
    public function Watermark(GD $gd) {
    $temp = $this->Attachment();
    $watermark = imagecreatefromjpeg(Director::absoluteBaseURL().'mysite/code/water.jpg');
    $imageToWatermark = imagecreatefromjpeg(Director::absoluteBaseURL().$temp->Filename);
    $imagewatermark_width = imagesx($imageToWatermark);
    imagecopymerge($imageToWatermark, $watermark, 39, 45, 0, 0, 400, 400, 40);
    $tm = imagejpeg($imageToWatermark);
    $GDoutput= new GD($temp->Filename);
    // return $tm;
    return $GDoutput;
    }

    My Template PodKategorie2.ss

    $Content
    <% control Fotky %>
    <ul>
       <li>$Popis</li>
       <li>$DatumVlozeni</li>
       <img="$Watermark"/>
    </ul>
    <% end_control %>

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Watermark in the picture. Link to this post

    Hi,

    could you try...
    return $temp->Filename;
    (in Watermark)

    and...
    <img src="$Watermark"/>
    (in the template)

    instead?

  • MarekStu
    Avatar
    Community Member
    4 Posts

    Re: Watermark in the picture. Link to this post

    Thank you for reply, unfortunately the result is the same. I still have a page on the picture.
    It's a simple thing to me:

    I need to present images to all users. But for registered users will have the opportunity to download images without the watermark. So I thought the simple way is, insert a watermark ...

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Watermark in the picture. Link to this post

    What is "Fotky" is that an instance of Fotka?

    what you are doing looks right with the watermark code (I've used that before), but there is quite a bit of missing detail about the "PodKategorie2" page, please post that too.

  • MarekStu
    Avatar
    Community Member
    4 Posts

    Re: Watermark in the picture. Link to this post

    Here is

    class PodKategorie2 extends Page {

    static $has_many = array (
    'Fotky' => 'Fotka'
    );

    public function getCMSFields()
    {
    $f = parent::getCMSFields();

    $manager = new ImageDataObjectManager(
    $this, // Controller
    'Fotky', // Source name
    'Fotka', // Source class
    'Attachment', // File name on DataObject
    null, // Headings
    'getCMSFields_forPopup' // Detail fields
    // Filter clause
    // Sort clause
    // Join clause
    );

    $f->addFieldToTab("Root.Content.Gallery",$manager);
    return $f;
    }
    }

    class PodKategorie2_Controller extends Page_Controller {

    }

  • MarekStu
    Avatar
    Community Member
    4 Posts

    Re: Watermark in the picture. Link to this post

    Finally, it has helped create a class WatermarkImage that inherits the image. GetWatermark method, which finally displays a watermark.

    class WatermarkImage extends Image {
    function getWatermark() {
    $watermark = imagecreatefromjpeg(Director::absoluteBaseURL().'mysite/code/watermark.jpg');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $image = imagecreatetruecolor($watermark_width, $watermark_height);
    $image = imagecreatefromjpeg(str_replace('/','\\',Director::baseFolder().'/'.$this->Filename));
    $size = getimagesize(str_replace('/','\\',Director::baseFolder().'/'.$this->Filename));
    $dest_x = $size[0] - $watermark_width - 5;
    $dest_y = $size[1] - $watermark_height - 5;
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 40);
    imagejpeg($image,str_replace('/','\\',Director::baseFolder().'/'.$this->Filename));
    imagedestroy($image);
    imagedestroy($watermark);
    }
    }


    in template

    <% control Images %>

    $Attachment.SetRatioSize(150,150)
    $Attachment.getWatermark

    <% end_control %>

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