dongsang6899 2013-08-07 16:58
浏览 54
已采纳

PHP GD水印脚本产生空白水印

I'm trying to add a watermark to images right after uploading them to my website, but it seems the watermark keeps coming out as a black object with no details. I believe the script is working a little bit because if it wasn't, I probably wouldn't see any kind of watermark or the script would fail.

This is my script so far:

$watermark = imagecreatefrompng('preview-watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);        
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file);  
$size = getimagesize($portfolio_preview_dir.'/'.$file);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image, $portfolio_preview_dir.'/'.$file);  
imagedestroy($image); 
imagedestroy($watermark);

This is what it's producing. The shape of the watermark is correct, as the watermark is 325x37 pixels:

enter image description here

I have tried playing with the watermark image itself. My first attempt was to save the photoshop watermark (with transparent bg) using 'save for web' and selected 'PNG-24'. This didn't work, so I then saved it as a normal PNG (without 'save for web') and it still fails.

I'm not sure whether it's the script or the image! Can somebody please share some knowledge with me and help fix this issue?

  • 写回答

2条回答 默认 最新

  • douba1904 2013-08-07 17:10
    关注
    $watermark = imagecreatefrompng('preview-watermark.png');
    imagealphablending($watermark , false);
    imagesavealpha($watermark , true);
    $watermark_width = imagesx($watermark);  
    $watermark_height = imagesy($watermark);        
    $image = imagecreatetruecolor($watermark_width, $watermark_height);  
    $image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file);  
    $size = getimagesize($portfolio_preview_dir.'/'.$file);  
    $dest_x = $size[0] - $watermark_width - 5;  
    $dest_y = $size[1] - $watermark_height - 5;  
    imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);  
    imagejpeg($image, $portfolio_preview_dir.'/'.$file);  
    imagedestroy($image); 
    imagedestroy($watermark);
    

    There are things:

    1. imagecopymerge does not allow transparency for PNG-24

    (Source: https://drupal.org/node/80369)

    1. imagesavealpha + imagealphablending allow to save transparency

    (Source: http://php.net/manual/en/function.imagesavealpha.php)

    I hope it fixes your problem.

    Also you output a JPEG, why? Stay with PNG and your image will support transparent watermarks, right now it does not!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵