PNG和GIF透明度丢失了

我在使用图像和php时遇到问题。</ p>

我是 使用一个功能,根据我的限制调整图像大小,然后创建一个新的图像。</ p>

问题是当我有一个png或gif文件调整大小时,透明度会丢失生成 具有黑色背景的图像。</ p>

以下是函数:</ p>

  //调整图像大小 - 保留宽度和高度的比率。

函数resizeImage ($ sourceImage,$ targetImage,$ maxWidth,$ maxHeight,$ quality = 70)
{
//从给定的源文件中获取图像。
$ info = getimagesize($ sourceImage);
$ imgtype = image_type_to_mime_type( $ info [2]);

开关($ imgtype){
case'image / jpeg':
$ image = imagecreatefromjpeg($ sourceImage);
break;
case'image / png':

$ image = imagecreatefrompng($ sourceImage);
break;
默认值:
die('图像类型无效。');
}

//获取源图像的​​尺寸。

list($ origWidth,$ origHeight)= getimagesize($ sourceImage);

if if($ maxWidth == 0)
{
$ maxWidth = $ origWidth;
}

if($ maxHeight == 0)
{
$ maxHeight = $ origHeight;
}

//计算所需最大尺寸与原始尺寸的比率。
$ widthRatio = $ maxWidth / $ origWidth;
$ heightRatio = $ maxHeight / $ origHeight;

//用于计算新图像尺寸的比率。
$ ratio = min($ widthRatio,$ heightRatio);

//计算新图像尺寸。
$ newWidth =(int)$ origWidth * $ ratio;
$ newHeight =(int)$ origHeight * $ ratio;

//创建具有新尺寸的最终图像。
$ newImage = imagecreatetruecolor($ newWidth,$ newHeight) ;
imagecopyresampled($ newImage,$ image,0,0,0,0,$ newWidth,$ newHeight,$ origWidth,$ origHeight);
imagejpeg($ newImage,$ targetImage,$ quality);

//释放内存。
imagedestroy($ image);
imagedestroy($ newImage);

返回true;
}
</ code> </ pre>
</ div>

展开原文

原文

I am having a problem working with images and php.

I am using a function with which will resize an image according to my limitations and then create a new image.

The problem is when I have a png or gif file to resize, the transparency gets lost generating images with black background.

Here is the function:

// Resize image - preserve ratio of width and height.
function resizeImage($sourceImage, $targetImage, $maxWidth, $maxHeight, $quality = 70)
{
    // Obtain image from given source file.
    $info = getimagesize($sourceImage);
    $imgtype = image_type_to_mime_type($info[2]);

    switch ($imgtype) {
      case 'image/jpeg':
        $image = imagecreatefromjpeg($sourceImage);
      break;
      case 'image/png':
        $image = imagecreatefrompng($sourceImage);
      break;
      default:
        die('Invalid image type.');
    }

    // Get dimensions of source image.
    list($origWidth, $origHeight) = getimagesize($sourceImage);

    if ($maxWidth == 0)
    {
        $maxWidth  = $origWidth;
    }

    if ($maxHeight == 0)
    {
        $maxHeight = $origHeight;
    }

    // Calculate ratio of desired maximum sizes and original sizes.
    $widthRatio = $maxWidth / $origWidth;
    $heightRatio = $maxHeight / $origHeight;

    // Ratio used for calculating new image dimensions.
    $ratio = min($widthRatio, $heightRatio);

    // Calculate new image dimensions.
    $newWidth  = (int)$origWidth  * $ratio;
    $newHeight = (int)$origHeight * $ratio;

    // Create final image with new dimensions.
    $newImage = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
    imagejpeg($newImage, $targetImage, $quality);

    // Free up the memory.
    imagedestroy($image);
    imagedestroy($newImage);

    return true;
}

2个回答



您总是使用 imagejpeg </ code>,无论您获得什么扩展名,还有 imagepng </ 代码>和 imagegif </ code>。 你应该使用那些,如果你有.gif或.png </ p>

我在谈论这一行:</ p>

  imagejpeg($ newImage)  ,$ targetImage,$ quality); 
</ code> </ pre>

您应该调用 imagejpeg </ code>, imagepng </ code>或 imagegif </ code>取决于文件类型。</ p>
</ div>

展开原文

原文

You are always using imagejpeg no matter what extension you get, there is also imagepng and imagegif. You should use those if you have a .gif or .png

I'm talking about this line:

imagejpeg($newImage, $targetImage, $quality);

You should call imagejpeg, imagepng or imagegif depending the file type.

dora12345678
dora12345678 好吧,我试过这个,但仍然有黑色背景。
接近 5 年之前 回复



除了以支持透明度的格式保存图像外,还需要指示GD复制来自源的透明度信息 。 对于PNG-8和GIF,您必须手动复制和设置透明色(请参阅 imagecolortransparent </ a>)从源到目的地,对于PNG-24,你必须保存alpha通道(参见 imagesavealpha )。</ p>
</ div>

展开原文

原文

In addition to saving the image in a format that supports transparency, you also need to instruct GD to copy over the transparency information from the source. For PNG-8 and GIF you must manually copy and set the transparent color (see imagecolortransparent) from the source to the destination, and for PNG-24 you must save the alpha channel (see imagesavealpha).

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐