PHP在PNG图像中添加PNG水印

时间:2017-12-27 23:57:15

标签: php

我使用imagecopyresampled()在图片上添加水印,所有功能都适用于JPG,或32/24位PNG,但有8位PNG我有这个错误图像:

enter image description here

我该如何解决?

我的代码

$tempFile = $_FILES['upload']['tmp_name'];
list($width, $height) = getimagesize($tempFile);

$new_canvas = imagecreatefrompng($tempFile);

// blending the images together
imagealphablending($new_canvas, true);

// Watermark
$watermark = imagecreatefrompng('watermark.png'); //watermark image
$width_watermark = imagesx($watermark); // 300px
$height_watermark = imagesy($watermark); // 100px

// blending the images together
imagealphablending($watermark, true); 

//calculate size of watermark
$stw = $width / 4;
$sth = $stw / 3;

//calculate center position of watermark image
$watermark_left = ($width / 2) - ($stw / 2); //watermark left
$watermark_bottom = ($height / 2) - ($sth / 2); //watermark bottom

imagecopyresampled($new_canvas, $watermark, $watermark_left, $watermark_bottom, 0, 0, $stw, $sth, $width_watermark, $height_watermark);

我已经尝试更改为false imagealphableding,但结果相同

1 个答案:

答案 0 :(得分:1)

创建PNG图像(如水印)时,您应该这样做

$image = imagecreatefrompng($file_src);
imagealphablending($image, true); // setting alpha blending on
imagesavealpha($image, true); // save alphablending setting (important)

你可以尝试我的图像类,它已经很老了,所以我没有永远使用它。也许它会起作用..我喜欢50/50

https://github.com/ArtisticPhoenix/MISC/tree/master/Image