News:

Support the VirtueMart project and become a member

Main Menu

Product image watermarking

Started by raunhar, May 18, 2017, 12:49:13 PM

Previous topic - Next topic

raunhar

Is there any plugin for virtuemart, which can add Watermarks to the product images.

jenkinhill

Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum

Ventsi Genchev

No plugin needed. It can only be done with a htaccess.
Audio Store:
https://vsystem.bg - Bulgarian language
https://vsystem.bg/en - English

raunhar

Please suggest the code to be added to htaccess

Sent from my Moto G (4) using Tapatalk


Ventsi Genchev

I made changes to this (thanks to Andre Catita for sharing):
https://github.com/andreCatita/imageWatermark

Required files are:
1. watermark.png (Some kind of your picture).

2. Changed by me watermark.php
<?php

/**
 *
 * PHP Image Watermark
 * Copyright (c) 2013 Andr顃atita http://andrecatita.com
 * http://github.com/andreCatita/
 *
 * GNU General Public License, version 2 (GPL-2.0)
 * http://opensource.org/licenses/GPL-2.0
 *
 */

// Configuration
$options = array(
    
'WATERMARK_IMAGE'     => ' watermark.png', // The location and name of the watermark  (If using ready a ready .PNG or .GIF set WATERMARK_IS_READY to TRUE)
    
'WATERMARK_OPACITY'     => '45', // The opacity the image will be merged with, this doesn't apply to WATERMARK_IS_READY
    
'WATERMARK_QUALITY'     => '100', // Image Quality - 0 to 100 - Higher is better
    
'WATERMARK_IS_READY'     => TRUE, // If your watermark image is already a .png or .gif with transparency set, set this to TRUE
    
'WATERMARK_PLACE'     => 'CENTER', // This value accepts -> BOTTOM_RIGHT, BOTTOM_LEFT, TOP_LEFT, TOP_RIGHT, CENTER, CENTER_LEFT, CENTER_RIGHT
    
'WATERMARK_MARGIN'     => '10',
);

function 
createImageFromFile($full_or_relative_path_to_image) {

    list(,, 
$image_type) = getimagesize($full_or_relative_path_to_image);

    if (
$image_type === NULL) {
return null;
    }

    switch (
$image_type) {
case IMAGETYPE_GIF:
    return imagecreatefromgif($full_or_relative_path_to_image);
    break;
case IMAGETYPE_JPEG:
    return imagecreatefromjpeg($full_or_relative_path_to_image);
    break;
case IMAGETYPE_PNG:
    return imagecreatefrompng($full_or_relative_path_to_image);
    break;
default:
    return null;
    break;
    }
}

// Overwrite Defaults
$original_image = (isset($_GET['image']) $_GET['image']     : null);
$watermark_image = (isset($_GET['watermark']) $_GET['watermark']     : $options['WATERMARK_IMAGE']);
$place = (isset($_GET['place']) $_GET['place']     : $options['WATERMARK_PLACE']);
$margin = (isset($_GET['margin']) $_GET['margin']     : $options['WATERMARK_MARGIN']);
$quality = (isset($_GET['quality']) $_GET['quality']     : $options['WATERMARK_QUALITY']);
$is_watermark_ready = (isset($_GET['is_ready']) ? ($_GET['is_ready'] == 'true' TRUE FALSE) : $options['WATERMARK_IS_READY']);

// Load Image & Watermark
$image createImageFromFile($original_image);
$watermark createImageFromFile($watermark_image);
if (!
$image || !$watermark)
    exit;

/*
 *  Math for watermark positions
 * - to avoid use down the road of php: imagesx and imagesy
 * - which can be used to obtain both height and width of the $image and $watermark element
 */

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

$image_width imagesx($image);
$image_height imagesy($image);

$watermark_pos_x $image_width -   $watermark_width;
$watermark_pos_y $image_height -   $watermark_height;

switch (
$place) {
    case 
'BOTTOM_LEFT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$margin$watermark_pos_y $margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$margin$watermark_pos_y $margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'BOTTOM_CENTER':
if ($is_watermark_ready) {
    imagecopy($image$watermark, ($watermark_pos_x 2), $watermark_pos_y $margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$watermark_pos_x $margin$watermark_pos_y $margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'BOTTOM_RIGHT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$watermark_pos_x $margin$watermark_pos_y $margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$watermark_pos_x $margin$watermark_pos_y $margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'TOP_LEFT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$margin$margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$margin$margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'TOP_CENTER':
if ($is_watermark_ready) {
    imagecopy($image$watermark, ($watermark_pos_x 2), $margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$watermark_pos_x $margin$margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'TOP_RIGHT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$watermark_pos_x $margin$margin00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$watermark_pos_x $margin$margin00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'CENTER':
if ($is_watermark_ready) {
    imagecopy($image$watermark, ($watermark_pos_x 2), ($watermark_pos_y 2), 00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark, ($watermark_pos_x 2), ($watermark_pos_y 2), 00$watermark_width$options['WATERMARK_OPACITY']);
}
break;
    case 
'CENTER_LEFT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$margin, ($watermark_pos_y 2), 00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$margin, ($watermark_pos_y 2), 00$watermark_width$watermark_height$options['WATERMARK_OPACITY']);
}
break;
    case 
'CENTER_RIGHT':
if ($is_watermark_ready) {
    imagecopy($image$watermark$watermark_pos_x $margin, ($watermark_pos_y 2), 00$watermark_width$watermark_height);
} else {
    imagecopymerge($image$watermark$watermark_pos_x $margin, ($watermark_pos_y 2), 00$watermark_width$options['WATERMARK_OPACITY']);
}
break;
}

// Output JPEG
header('Content-Type: image/jpeg');
imagejpeg($imagenull$quality);

// Clear Memory
imagedestroy($image);
imagedestroy($watermark);
?>


3. And .htaccess file:
RewriteEngine on
RewriteRule ^(resized) - [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.(gif|jp?g|png))$ watermark.php?image=$1&watermark=watermark.png [NC]


Put the three files in the product photo folder. For example: images/stories/virtuemart/product. Or where they are set.
To test you can download the picture watermark.png from the link at the top.
I've added a rule in the .htaccess that forbids the watermark for resized photos.

That is all. I wish you success!
Audio Store:
https://vsystem.bg - Bulgarian language
https://vsystem.bg/en - English