product transparent png and watermark ugly black background

Hi all ,



This is a fix to the problem with the watermark being placed on top of a product image that is a transparent .png.

I needed this for 2 carts, one is 2.2.4 (that doesn't have the watermark addon) and one is 3+

The problem is that when you have a transparent product .png, and you add a watermark on it the background turns black.

After hours of searching the only solution i could find (credit goes to mattls) and this post http://forum.cs-cart.com/topic/21496-watermarks-addon-changes/



I think the cs-cart team should really implement this to the watermark addon, saving us hours of work !!



From what i understand, you first have to create an image with a white background, place the product image on it and then place the watermark on that.



so here goes :

place a folder watermark in the root of you cs-cart, in the folder place your watermark.png file and the file w.php with the following contents :

```php

// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where mark.png is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named w.php
$imagesource = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $file_1 = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $file_1 = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $file_1 = @imagecreatefrompng($imagesource);
$file_2 = "watermark.png";
// open image 1
$image_1 = $file_1;
imageAlphaBlending($image_1, false);
imageSaveAlpha($image_1, true);
$x1 = imagesx($image_1);
$y1 = imagesy($image_1);
// open image 2
$image_2 = imagecreatefrompng($file_2);
imageAlphaBlending($image_2, false);
imageSaveAlpha($image_2, true);
$x2 = imagesx($image_2);
$y2 = imagesy($image_2);
// make a transparent background
$slate = imagecreatetruecolor(max($x1, $x2), max($y1, $y2));
$transparent = imagecolorallocatealpha($slate,0,255,0,127);
imagefill($slate,0,0,$transparent);
// now do the copying
imagecopy($slate, $image_1, 0, 0, 0, 0, imagesx($image_1)-1, imagesy($image_1)-1);
imagecopy($slate, $image_2, 0, 0, 0, 0, imagesx($image_2)-1, imagesy($image_2)-1);
// for the background do this after copying is finished
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);
imagepng($slate);
imagedestroy($image_1);
imagedestroy($image_2);
?>

```



then place an .htaccess file in your images detailed, with this in it :


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .(gif|jpeg|jpg|png)$ /watermark/w.php [QSA,NC]
######################## If your store lives in a dir like /shop make sure and edit the last line and add the dir name before /watermark/w.php ###############




works on both versions, uses server resources … but it's the only fix i could find