【问题标题】:PHP image transparentPHP图片透明
【发布时间】:2013-01-19 05:22:53
【问题描述】:

我正在尝试使用以下代码调整 PNG 透明图像的大小:

$src=ImageCreateFrompng($uploadedfile);
$background=imagecolorallocate($src,0,0,0);
imagecolortransparent($src,$background);
imagealphablending($src,false);
imagesavealpha($src,true);
$dst=ImageCreateTrueColor($tn_width,$tn_height);
imagecopyresampled($dst,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
Imagepng($dst,$dstfile);

我使用了imagealphablending($src,false)imagesavealpha($src,true),但它仍然会上传黑色背景的图像,而不是透明的。

问题出在哪里?

【问题讨论】:

    标签: php image resize thumbnails transparent


    【解决方案1】:

    这是因为您将透明图像复制到黑色图像上。您对 aplhablending 的错误设置仅适用于原始图像,因此当您在新图像上复制时,aplha blending 已打开。
    您的代码只需要一点改进:

    $transparent = imagecolorallocatealpha($dst, 0,0,0,127);  //Transparent background color
    imagealphablending($dst,false);            //Not to compound transparent colors with opaque
    imagesavealpha($dst,true);                 //Allow transparency
    imagefilledrectangle($dst, 0, 0, imagesx($dst), imagesy($dst), $transparent);   //Give the destination image transparent background
    //Now you can copy
    

    或者只是

    imagealphablending($dst,false);   //I'm not 100% sure this will make it, but its worth a try
    

    【讨论】:

    • 我应该怎么做才能修复它?
    • 我的帖子略有改进。很抱歉第一次发布这么短:)
    • 只是一个小错误:imagecolorallocatealpha 而不是 imagecollorallocatealpha... 它有效!!
    • 哦,很抱歉出现拼写错误。随时建议对包含错误的帖子进行编辑。
    • 我不确定您点击的是哪个代码,但如果您回复我的不是建议编辑,请务必阅读 Stackoverflow:About 页面
    【解决方案2】:

    imagecolorallocate($src,0,0,0);

    该行看起来像是从源获取图像,然后使用 000 背景。哪个是黑色的。只是猜测,对函数不熟悉

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2011-03-13
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2013-07-17
      • 2014-01-10
      相关资源
      最近更新 更多