【问题标题】:PHP - How can I copy the image as transparent pngPHP - 如何将图像复制为透明png
【发布时间】:2016-07-17 21:03:55
【问题描述】:

首先,我不是 PHP 大师。我正在使用调整图像大小和裁剪图像的功能。在我上传透明 png 之前,它运行良好。 :)

它保存黑色背景的png。我在 stackoverflow 上找到了一些答案,但我无法将它与我的代码结合起来。

这是我的功能:

//resize and crop image
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 90){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            $format = "gif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            $format = "png";
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $format = "jpg";
            break;

        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }


    // you can ignore these 4 lines. I'm using it for change the name.
    $nameforimage   = rand('11111111', '9999999999');
    $nameforimage2  = rand('11111111', '9999999999');
    $newname        = $nameforimage."_".$nameforimage2;
    $newdir         = $dst_dir."".$newname.".".$format;

    $image($dst_img, $newdir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);

    return $newname.".".$format;

}

编辑: 好的,我找到了解决方案。

只需添加以下几行:

imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $max_width, $max_height, $transparent);

在这一行之后:

$dst_img = imagecreatetruecolor($max_width, $max_height);

【问题讨论】:

  • 我更新了我的错误代码。请再看一遍..
  • 以下问题中的选定答案将向您展示如何保留 Alpha 混合:stackoverflow.com/questions/32243/….
  • 好的,谢谢。我找到了,现在我将添加我的问题的答案。

标签: php png transparent


【解决方案1】:

您必须启用保存 Alpha 通道。

可以用imagesavealpha()完成,例如:

// As per the manual, alpha blending must be disabled
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);

【讨论】:

  • 谢谢。它正在工作:)。那么,这些是必需的还是不需要的? >>$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); imagefilledrectangle($dst_img, 0, 0, $max_width, $max_height, $transparent);
  • @InterestingKnox 对我来说似乎没有必要,你正在“绘制”一个透明的矩形,但无论如何你都会在上面复制源图像。
  • 啊,好吧!这是另一个答案的示例代码。非常感谢@outlyer。
猜你喜欢
  • 2015-03-02
  • 2010-11-26
  • 1970-01-01
  • 2021-06-01
  • 2013-03-17
  • 2014-10-05
  • 2011-03-04
  • 2018-10-09
  • 2020-11-26
相关资源
最近更新 更多