P
P
Petr Volkhanov2014-10-22 09:33:01
PHP
Petr Volkhanov, 2014-10-22 09:33:01

Problems with watermark, the background becomes black instead of transparent. What could be wrong?

I have a problem - I make images for the site with a transparent PNG background and using PHP I put watermark on the images with a transparent PNG background too, and such a misfortune occurs - the watermark is superimposed on the image, but the background becomes black, and not transparent as it should be. What could be wrong?
Here is the PHP code:

<?php
  waterMark($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'], "watermark.png", "bottom=5,right=5");
 
function waterMark($original, $watermark, $placement = 'bottom=5,right=5', $destination = null) {  
   $original = urldecode($original); 
   $info_o = @getImageSize($original);  
   if (!$info_o)  
         return false;  
   $info_w = @getImageSize($watermark);  
   if (!$info_w)  
         return false;  
 
   list ($vertical, $horizontal) = split(',', $placement,2);  
   list($vertical, $sy) = split('=', trim($vertical),2);  
   list($horizontal, $sx) = split('=', trim($horizontal),2);  
 
   switch (trim($vertical)) {  
      case 'bottom':  
         $y = $info_o[1] - $info_w[1] - (int)$sy;  
         break;  
      case 'middle':  
         $y = ceil($info_o[1]/2) - ceil($info_w[1]/2) + (int)$sy;  
         break;  
      default:  
         $y = (int)$sy;  
         break;  
      }  
 
   switch (trim($horizontal)) {  
      case 'right':  
         $x = $info_o[0] - $info_w[0] - (int)$sx;  
         break;  
      case 'center':  
         $x = ceil($info_o[0]/2) - ceil($info_w[0]/2) + (int)$sx;  
         break;  
      default:  
         $x = (int)$sx;  
         break;  
      }  
 
   header("Content-Type: ".$info_o['mime']);  
 
   $original = @imageCreateFromString(file_get_contents($original));  
   $watermark = @imageCreateFromString(file_get_contents($watermark));  
   $out = imageCreateTrueColor($info_o[0],$info_o[1]);  
 
   imageCopy($out, $original, 0, 0, 0, 0, $info_o[0], $info_o[1]);  
   if( ($info_o[0] > 250) && ($info_o[1] > 250) ) 
   { 
   imageCopy($out, $watermark, $x, $y, 0, 0, $info_w[0], $info_w[1]); 
   } 
 
   switch ($info_o[2]) {  
      case 1:  
         imageGIF($out);  
         break;  
      case 2:  
         imageJPEG($out);  
         break;  
      case 3:  
         imagePNG($out);  
         break;  
         }  
 
   imageDestroy($out);  
   imageDestroy($original);  
   imageDestroy($watermark);  
 
   return true;  
   }  
?>

And here is the .htaccess file that I put in my images folder:
DirectoryIndex index.php  
<FilesMatch "\.(gif|jpg|png|PNG|GIF|JPG)$">  
   RewriteEngine On  
   RewriteCond %{REQUEST_FILENAME} -f  
   RewriteRule ^(.*)$ /wm/_wm.php [T=application/x-httpd-php,L,QSA]  
</FilesMatch>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2014-10-22
@eprivalov

Processing without alpha channel...
Add under

$out = imageCreateTrueColor($info_o[0],$info_o[1]);
the code:
imagesavealpha( $out, true );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question