dongwei5740 2019-08-07 09:04
浏览 80

PHP GD Library在dev vs staging上生成不同的图像

I am getting a strange banding / lines artefact in the transparent part of a resized PNG image using my PHP resizing script and the GD Library. It seems that the alpha channel in the parts of the image which should have an alpha value of 0, is fluctuating between 0 and 2 in a banding pattern.

I have no idea what's going on here, if anyone could shed some light on this?

This is only happening when resizing the image down to this thumbnail size. The large size is resized correctly without any banding.

Image with banding on Staging:

Image With Banding On Staging

Image without banding on Development:

Image Without Banding On Development

Enhanced view of the banding artefacts:

Banding Artefacts

(The black pixels represent an alpha value of 0, and the white pixels represent an alpha value of 2).

My two environments are set up as follows:

Development - PHP Version 7.2.2-1+ubuntu14.04.1+deb.sury.org+1 with GD Library:

  • GD headers Version 2.2.5
  • GD library Version 2.1.1
  • FreeType Version 2.5.2
  • libJPEG Version 8
  • libPNG Version 1.2.50
  • libXpm Version 30411

Staging - PHP Version 7.0.33-0ubuntu0.16.04.5 with GD Library:

  • GD headers Version 2.1.1
  • GD library Version 2.1.1
  • FreeType Version 2.6.1
  • libJPEG Version 8
  • libPNG Version 1.2.54
  • libXpm Version 30411

The resizing code is as follows:

public static function resizeImage($filename, $desiredWidth, $desiredHeight, $outputDir, $streamResult = true)
{

    $imgSrc = null;
    $imgDst = null;

    $imginfo = getimagesize($filename);
    $type = '';

    if($imginfo['mime']=='image/png')
    {
        $type = ImageResizer::TYPE_PNG;
        $imgSrc = imagecreatefrompng($filename);
//              imagealphablending($imgSrc, true);
        imagesavealpha($imgSrc, true);
    }
    else if ($imginfo['mime']=='image/jpg' || $imginfo['mime']=='image/jpeg' || $imginfo['mime']=='image/pjpeg')
    {
        $type = ImageResizer::TYPE_JPEG;
        $imgSrc = imagecreatefromjpeg($filename);
    }

    if (empty($type)) return false;

    $oldW = $imginfo[0];
    $oldH = $imginfo[1];
    $newW = $desiredWidth;
    $newH = $desiredHeight;


    if ($newW == -1)
    {
        $aspect = ($oldW / $oldH);
        $newW    =   $desiredHeight*$aspect;
        $newH    =   $desiredHeight;

    }
    else if ($newH == -1)
    {
        $aspect = ($oldH / $oldW);
        $newW    =   $desiredWidth;
        $newH    =   $desiredWidth*$aspect;
    }
    else
    {
        if ($oldW > $oldH)
        {
            $newW = $desiredWidth;
            $newH = $oldH*($desiredWidth/$oldW);
        }
        else
        {
            $newW = $oldW*($desiredHeight/$oldH);
            $newH = $desiredHeight;
        }
    }


    $imgDst = imagecreatetruecolor($newW,$newH);
    imagealphablending($imgDst, false);
    imagesavealpha($imgDst, true);
    $colourTransparent = imagecolorallocatealpha($imgDst, 255, 255, 255, 255);
    imagecolortransparent($imgDst, $colourTransparent);
    // Fill image
    imagefill($imgDst, 0, 0, $colourTransparent);

    imagecopyresampled($imgDst,$imgSrc,0,0,0,0,$newW,$newH,$oldW,$oldH);


    // New save location:

    $outputFilename = $outputDir . basename($filename);

    $result = false;

    if ($type == ImageResizer::TYPE_PNG)
    {
        $result = imagepng($imgDst,$outputFilename);
    }
    else if ($type == ImageResizer::TYPE_JPEG)
    {
        $result = imagejpeg($imgDst,$outputFilename,80);
    }

    if ($result && $streamResult)
    {

        header('Content-Type: '.$imginfo['mime']);
        if ($type == ImageResizer::TYPE_PNG)
        {
            imagepng($imgDst);
        }
        else if ($type == ImageResizer::TYPE_JPEG)
        {
            imagejpeg($imgDst, null, 80);
        }
    }

    imagedestroy($imgDst);
    imagedestroy($imgSrc);

    return $result;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数