3

我在下面有这个功能,可以让我调整上传的图片大小,

function resize_image($image,$width,$height,$scale) 
{
    list($imagewidth, $imageheight, $imageType) = getimagesize($image);
    $imageType = image_type_to_mime_type($imageType);
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

    switch($imageType) 
    {
        case "image/gif":
            $source = imagecreatefromgif($image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            $source = imagecreatefromjpeg($image); 
            break;
        case "image/png":
        case "image/x-png":
            $source = imagecreatefrompng($image); 
            break;
    }

    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

    switch($imageType) 
    {
        case "image/gif":
            imagegif($newImage,$image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            imagejpeg($newImage,$image,90); 
            break;
        case "image/png":
        case "image/x-png":
            imagepng($newImage,$image);  
            break;
    }

    chmod($image, 0777);
    return $image;
}

但是这个函数有一个问题,当我有一个透明背景的 png 或 gif 时,它无法创建透明度。

如何修复此功能以允许 php 透明?

4

4 回答 4

4

这是我的答案,

# This is the resizing/resampling/transparency-preserving magic
    if ( ($imageType == "image/gif") || ($imageType == "image/png") ) 
    {
        $transindex = imagecolortransparent($source);

        if($transindex >= 0) 
        {
            $transcol = imagecolorsforindex($source, $transindex);
            $transindex = imagecolorallocatealpha($newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
            imagefill($newImage, 0, 0, $transindex);
            imagecolortransparent($newImage, $transindex);
        }

        elseif ($imageType == "image/png" || $imageType == "image/x-png") 
        {
            imagealphablending($newImage, false);
            $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
            imagefill($newImage, 0, 0, $color);
            imagesavealpha($newImage, true);
        }
    }

并且工作完全正常

于 2011-07-25T18:49:12.123 回答
3

我发现这个解决方案真的很有帮助!

http://www.php.net/manual/ru/function.imagecopyresampled.php#104028

您只需在 imagecreatetruecolor() 和 imagecopyresampled() 之间添加此代码。不要忘记定义 $type。

$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

// preserve transparency
if($type == "gif" || $type == "png"){
  imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 0, 0, 0, 127));
  imagealphablending($newImage, false);
  imagesavealpha($newImage, true);
}

imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
于 2014-05-14T20:29:41.657 回答
1

查看开源的smart_resize_image方法,他们似乎会检查imagecolortransparent并在存在时将其重新应用于新图像。

可能值得在 github 上查看该项目,而不是重新发明轮子。还可以让您深入了解他们是如何做到的,并且是共同努力的成果(很多人都在解决已知 [和未知] 的错误)。

于 2011-07-25T17:14:31.357 回答
0

为了让 GD Lib 绘制透明度,您必须定义透明颜色。您可以在图形的某处采样颜色并使用它。

http://www.php.net/manual/en/function.imagecolortransparent.php

于 2011-07-25T17:16:00.173 回答