How to add watermark to image for opencart?

These instructions are valid for Opencart 1.4.x and 1.5.x

Step#1. Create PNG watermark, name it as watermark.png and copy it to image/ folder of your Opencart installation (yes, it works fine with PNG files).
Step#2. Now edit 2 PHP files
Step#3. The first file: system/library/image.php

Go to lines 93-121 and you will see the following code:

public function watermark($file, $position = 'bottomright') {
        $watermark = $this->create($file);

        $watermark_width = imagesx($watermark);
        $watermark_height = imagesy($watermark);

        switch($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
            case 'bottomright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
        }

        imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);

        imagedestroy($watermark);
    }

You have to replace this code with the code below :

public function watermark($file, $position = 'bottomright') {

   //Pb ! you don't work on the true object => you must modify the "create" method ($mime = $this->info['mime']; it's work only if we have the same image format !
        //$watermark = $this->create($file);

        //Prefer PNG files => it's not optimized but it's work
   $watermark = imagecreatefrompng($file);

        $watermark_width  = imagesx($watermark);
        $watermark_height = imagesy($watermark);

        switch($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
            case 'bottomright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
         case 'center':
            $watermark_pos_x = ($this->info['width']- $watermark_width)/2;
            $watermark_pos_y = ($this->info['height']- $watermark_height)/2;
            break;
        }

        imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark_width, $watermark_height);

        imagedestroy($watermark);
    }

Step#4. Second file to edit is catalog/model/tool/image.php
Go to line 27 and you will see the follwing line:

$image = new Image(DIR_IMAGE . $old_image);

Right after this line add the following code:

if ($width > 400 || $height > 300) {   
   $image->watermark(DIR_IMAGE . 'watermark.png', 'center');
}

In the code above you can define desired position of the watermark (topleft, topright, bottomleft, bottomright or center).
You can also define minimum width or height of your image to which the watermark will be added (if you want watermarks appear even on thumbnails, put 0)

5. Final step: delete all data in folder image/cache/data in order to have the images recreated with watermarks.
That’s it!

Download File is here, you can view it after click any social button.

6 Replies to “How to add watermark to image for opencart?”

Leave a Reply

Your email address will not be published. Required fields are marked *


*