August 24, 2016

Opencart: Adding a brand logo watermark on product images

Opencart: Adding a brand logo watermark on product images : best way to do it.

This is not a extension , so i am using a file to create a dynamic URL which creates a watermark image. I am taking two paramter image and watermark as a image and using it to generate an image.

  1. Create a watermark.php and place it at the root directory of the Opencart. This is itself able to do the trick but i have modified this for supporting opencart path. this should be your cpanel path.
<?php

function imagecreatefromfile($image_path) {
$image_path = ‘/home2/mattsspo/public_html/image/’.$image_path;

// retrieve the type of the provided image file
list($width, $height, $image_type) = getimagesize($image_path);

// image type
switch ($image_type)
{
case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
default: return ”; break;
}
}

// load source image to memory
$image = imagecreatefromfile($_GET[‘image’]);
if (!$image) die(‘Unable to open image’);

// load watermark to memory
$watermark = imagecreatefromfile($_GET[‘watermark’]);
if (!$image) die(‘Unable to open watermark’);

// calculate the position of the watermark in the output image (the
// watermark shall be placed in the lower right corner)
$watermark_pos_x = imagesx($image) – imagesx($watermark);
$watermark_pos_y = imagesy($image) – imagesy($watermark);

$watermark_pos_x = 5;
$watermark_pos_y = 5;

// merge the source image and the watermark
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0,
imagesx($watermark), imagesy($watermark));

// output watermarked image to browser
header(‘Content-Type: image/jpeg’);
imagejpeg($image, NULL, 100); // use best image quality (100)

// remove the images from memory
imagedestroy($image);
imagedestroy($watermark);

?>

2. Now Add another function at opencart model ModelToolImage. this will be able to generate a proper URL for opencart images with watermark.

First parameter ia a image URL and second parameter is a watermark.

public function watermark($image,$watermark) {
$image = str_replace(HTTP_SERVER.’image’,”,$image);
$image = str_replace(HTTPS_SERVER.’image’,”,$image);

$watermark = str_replace(HTTP_SERVER.’image’,”,$watermark);
$watermark = str_replace(HTTPS_SERVER.’image’,”,$watermark);

return ‘watermark.php?image=’.$image.’&watermark=’.$watermark;
}

3. Getting manufacturer image for watermark. Remember we are changing manufacture variable into array() and tpl manufacturer name will be called by its array key. array key image will be an watermark image and its resized here at 80×40.

if($data[‘manufacturer’][‘image’]){
$data[‘manufacturer’][‘image’] = $this->model_tool_image->resize( $data[‘manufacturer’][‘image’],80,40);
}else{
$data[‘manufacturer’][‘image’] = ”;
}

4. Now at product controller replace the image variable with. Its generally we are passing it with $this->model_tool_image->watermark() function.

if ($product_info[‘image’]) {
$data[‘popup’] = $this->model_tool_image->resize($product_info[‘image’], $this->config->get(‘config_image_popup_width’), $this->config->get(‘config_image_popup_height’));
$data[‘popup’] = $this->model_tool_image->watermark($data[‘popup’],$data[‘manufacturer’][‘image’]);
} else {
$data[‘popup’] = ”;
}

 

 

 

You may also like...

Leave a Reply