简体   繁体   中英

PHP Add PNG watermark in PNG image

I use imagecopyresampled() for add Watermark over an image, all work great with JPG, or 32/24Bits PNG but with 8bits PNG I have this error image:

在此处输入图片说明

How could I fix that?

My code

$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);

I have already tried to change to false imagealphableding but its the same result

When creating PNG images ( like your watermark ) you should do this

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

You can try my image class, it's pretty old so I haven't used it in forever. Maybe it will work .. im like 50/50

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM