将水印添加到jpg / jpeg并且png工作完美,但是当相同的水印添加到gif图像时会出现奇怪的颜色
the gif image with watermark is here
这里奇怪的颜色是我的png水印图像,如下所示
我的代码
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
将gif图像转换为真彩色图像后解决此问题
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
$tmp = imagecreatetruecolor(imagesx($image), imagesy($image));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$image = $tmp;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);