Jump to content

resizeable watermark image on product pictures


Recommended Posts

I don't have Version 1.3, but I'll try to do an example in the 1.4.6.2 version. I expect the 1.3 version wouldn't be that different...

 

In file: /modules/watermark/watermark.php we have this function:

	private function watermarkByImage($imagepath, $watermarkpath, $outputpath)
	{	
		$Xoffset = $Yoffset = $xpos = $ypos = 0;
		if (!$image = imagecreatefromjpeg($imagepath))
			return false;
		if (!$imagew = imagecreatefromgif ($watermarkpath))
			die ($this->l('The watermark image is not a real gif, please CONVERT the image.'));
		list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath); 
		list($imageWidth, $imageHeight) = getimagesize($imagepath); 
		if ($this->xAlign == "middle") { $xpos = $imageWidth/2 - $watermarkWidth/2 + $Xoffset; } 
		if ($this->xAlign == "left") { $xpos = 0 + $Xoffset; } 
		if ($this->xAlign == "right") { $xpos = $imageWidth - $watermarkWidth - $Xoffset; } 
		if ($this->yAlign == "middle") { $ypos = $imageHeight/2 - $watermarkHeight/2 + $Yoffset; } 
		if ($this->yAlign == "top") { $ypos = 0 + $Yoffset; } 
		if ($this->yAlign == "bottom") { $ypos = $imageHeight - $watermarkHeight - $Yoffset; } 
		if (!imagecopymerge($image, $imagew, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency))
			return false;
		return imagejpeg($image, $outputpath, 100); 
	} 

To change the size of the watermark according to image size, we can try to do this:

Check the size of your 'large' picture in Preferences->Image (I hope you have that in 1.3 :-)  ).

Say it is 125x125 pxls.

Lets then define

$baseWidth=125; 

$baseHeight=125;

 

After these two lines from the code above:

        list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath);
        list($imageWidth, $imageHeight) = getimagesize($imagepath);

 
we know the image size and the watermark size.
Create your watermark image and give it a size so that it would be perfect on the 'large' image, without scaling.
 
Then:
after the two lines above, add:
 
$origWatermarkWidth = $watermarkWidth;  // keep the original width and height of the watermark
$origWatermarkHeight = $watermarkHeight;
$watermarkWidth = round(($origWatermarkWidth*$imageWidth)/$baseWidth);
$watermarkHeight = round(($origWatermarkHeight*$imageHeight)/$baseHeight);
 
What it does:
 
The $watermarkWidth will be it's original size *   ($imagewidth/$baseWidth) and then rounded to the nearest integer
Example:
Say the watermark image is 40x20pxls
if we have to add it to a 'large' image:
    $watermarkWidth = round(40 * "large"width / $baseWidth) = round(40*125/125) = 40. Which is correct, as we took the "large" size as base. (For "large" images we didn't have to resize, remember?)
 

if we now take a "medium" sized image (let's say this is defined in Preferences->Image as 60*60pxls) and want to add the watermark image:

$watermarkWidth = round($origWatermarkWidth * "medium"width / $baseWidth) = round(40 * 60/125) = round(19.2) = 19pxl.

 

60/125 = just over 50% reduction, so the watermark should also be a little over 50% smaller, which is correct (50% of 40 = 20 =~19pxl)

 

Same of course with the height.

 

So now we have the new size of the watermark. What we now have to do is resizing the watermark so that we can add it to the image:

 

$resizedWaterMark=imagecreate($watermarkWidth,$watermarkHeight);  // create empty watermark image of new size

 

imagecopyresampled($resizedWatermark,$imagew,0,0,0,0,$watermarkWidth,$watermarkHeight,$origWatermarkWidth,$origWatermarkHeight);   // resize and copy the original watermark into the new watermark image.

 

(N.B.  $imagew is the original watermark image)

 

Finally, we have to add the resized watermark image instead of the original to the product image:

se we need to change:

if (!imagecopymerge($image, $resizedWatermark, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency))

 

I think this does the trick. I didn't try it for real, but give it a try: (hope there are no syntax errors... )

 

The total solutions should thus be:

	private function watermarkByImage($imagepath, $watermarkpath, $outputpath)
	{	

                $baseWidth = 125;  // change acc. to own "large"-image size in Preferences -> Image
                $baseHeight = 125; // change acc. to own "large"-image size in Preferences -> Image

		$Xoffset = $Yoffset = $xpos = $ypos = 0;
		if (!$image = imagecreatefromjpeg($imagepath))
			return false;
		if (!$imagew = imagecreatefromgif ($watermarkpath))
			die ($this->l('The watermark image is not a real gif, please CONVERT the image.'));
		list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath); 
		list($imageWidth, $imageHeight) = getimagesize($imagepath); 
 
                $origWatermarkWidth = $watermarkWidth;  // keep the original size of the watermark
                $origWatermarkHeight = $watermarkHeight;
                $watermarkWidth = round(($origWatermarkWidth*$imageWidth)/$baseWidth);
                $watermarkHeight = round(($origWatermarkHeight*$imageHeight)/$baseHeight);
 
// create empty watermark image of new size

$resizedWaterMark=imagecreate($watermarkWidth,$watermarkHeight);  

 

// resize and copy the original watermark into the new watermark image

imagecopyresampled($resizedWatermark,$imagew,0,0,0,0,$watermarkWidth,$watermarkHeight,

$origWatermarkWidth,$origWatermarkHeight);  

 
 

if ($this->xAlign == "middle") { $xpos = $imageWidth/2 - $watermarkWidth/2 + $Xoffset; } if ($this->xAlign == "left") { $xpos = 0 + $Xoffset; } if ($this->xAlign == "right") { $xpos = $imageWidth - $watermarkWidth - $Xoffset; } if ($this->yAlign == "middle") { $ypos = $imageHeight/2 - $watermarkHeight/2 + $Yoffset; } if ($this->yAlign == "top") { $ypos = 0 + $Yoffset; } if ($this->yAlign == "bottom") { $ypos = $imageHeight - $watermarkHeight - $Yoffset; } // Changed the original watermark image with the resized one...
if (!imagecopymerge($image, $resizedWatermark, $xpos, $ypos, 0, 0, $watermarkWidth,
$watermarkHeight, $this->transparency)) return false; return imagejpeg($image, $outputpath, 100); }

 

Be careful, maybe in PrestaShop 1.3. the variables may have other names, (or may be even a totally different function...).

If you can't integrate the changes it in the 1.3. version, please copy the full function of 1.3. here, then I'll try to add it for you.

 

Let me know if it works!

 

pascal.

Link to comment
Share on other sites

public function hookwatermark($params)
	{
		global $smarty;
		$file = _PS_PROD_IMG_DIR_.$params['id_product'].'-'.$params['id_image'].'-watermark.jpg';
		
		//first make a watermark image
		$return = $this->watermarkByImage(_PS_PROD_IMG_DIR_.$params['id_product'].'-'.$params['id_image'].'.jpg',  dirname(__FILE__).'/watermark.gif', $file, 90, 0, 0, 'right');

		//go through file formats defined for watermark and resize them
		foreach($this->imageTypes as $imageType)
		{
		    $newFile = _PS_PROD_IMG_DIR_.$params['id_product'].'-'.$params['id_image'].'-'.stripslashes($imageType['name']).'.jpg';
		    if (!imageResize($file, $newFile, intval($imageType['width']), intval($imageType['height'])))
				$return = false;    
		}
		return $return;
	}

	private function watermarkByImage($imagepath, $watermarkpath, $outputpath)
	{	
		$Xoffset = $Yoffset = $xpos = $ypos = 0;
		if (!$image = imagecreatefromjpeg($imagepath))
			return false;
		if (!$imagew = imagecreatefromgif($watermarkpath))
			die ($this->l('the watermark image is not a real gif, please CONVERT and not rename it'));
		list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath); 
		list($imageWidth, $imageHeight) = getimagesize($imagepath); 
		
		if ($this->xAlign == "middle") { $xpos = $imageWidth/2 - $watermarkWidth/2 + $Xoffset; } 
		if ($this->xAlign == "left") { $xpos = 0 + $Xoffset; } 
		if ($this->xAlign == "right") { $xpos = $imageWidth - $watermarkWidth - $Xoffset; } 
		if ($this->yAlign == "middle") { $ypos = $imageHeight/2 - $watermarkHeight/2 + $Yoffset; } 
		if ($this->yAlign == "top") { $ypos = 0 + $Yoffset; } 
		if ($this->yAlign == "bottom") { $ypos = $imageHeight - $watermarkHeight - $Yoffset; } 
		if (!imagecopymerge($image, $imagew, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency))
			return false;
		return imagejpeg($image, $outputpath, 100); 
	} 

here i have added the code of v1.3 its almost the same but i tried what you told has not done, actually i was little bit confused in editing as well, kindly tell me now..

Thanks

Link to comment
Share on other sites

private function watermarkByImage($imagepath, $watermarkpath, $outputpath)
	{	
		$Xoffset = $Yoffset = $xpos = $ypos = 0;
		if (!$image = imagecreatefromjpeg($imagepath))
			return false;
		if (!$imagew = imagecreatefromgif($watermarkpath))
			die ($this->l('the watermark image is not a real gif, please CONVERT and not rename it'));
		list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath); 
		list($imageWidth, $imageHeight) = getimagesize($imagepath); 
		$baseWidth=300; 
		$baseHeight=300;
		$origWatermarkWidth = $watermarkWidth;  // keep the original width and height of the watermark
		$origWatermarkHeight = $watermarkHeight;
		$watermarkWidth = round(($origWatermarkWidth*$imageWidth)/$baseWidth);
		$watermarkHeight = round(($origWatermarkHeight*$imageHeight)/$baseHeight);
		$resizedWaterMark=imagecreate($watermarkWidth,$watermarkHeight);  // create empty watermark image of new size
		gecopyresampled($resizedWatermark,$imagew,0,0,0,0,$watermarkWidth,$watermarkHeight,$origWatermarkWidth,$origWatermarkHeight);   // resize and copy the original watermark into the new watermark image.
		if ($this->xAlign == "middle") { $xpos = $imageWidth/2 - $watermarkWidth/2 + $Xoffset; } 
		if ($this->xAlign == "left") { $xpos = 0 + $Xoffset; } 
		if ($this->xAlign == "right") { $xpos = $imageWidth - $watermarkWidth - $Xoffset; } 
		if ($this->yAlign == "middle") { $ypos = $imageHeight/2 - $watermarkHeight/2 + $Yoffset; } 
		if ($this->yAlign == "top") { $ypos = 0 + $Yoffset; } 
		if ($this->yAlign == "bottom") { $ypos = $imageHeight - $watermarkHeight - $Yoffset; } 
		//if (!imagecopymerge($image, $imagew, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency))
		if (!imagecopymerge($image, $resizedWatermark, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency))
			return false;
		return imagejpeg($image, $outputpath, 100); 
	} 

its my edited function.. kindly check

but its not giving any type of watermark on image..

Link to comment
Share on other sites

One line seems a little odd. You forgot to copy a few characters:

 

gecopyresampled($resizedWatermark,$imagew,0,0,0,0,$watermarkWidth,$watermarkHeight,$origWatermarkWidth,$origWatermarkHeight); // resize and copy the original watermark into the new watermark image.

 

forgot the 'ima' of image at the front.

 

imagecopyresampled($resizedWatermark,$imagew,0,0,0,0,$watermarkWidth,$watermarkHeight,$origWatermarkWidth,$origWatermarkHeight); // resize and copy the original watermark into the new watermark image.

 
 
Try that again. Maybe it works then.
 
pascal
Link to comment
Share on other sites

×
×
  • Create New...