使用抗锯齿在PHP中调整图像大小

时间:2017-02-20 14:12:22

标签: php image filter gd

我想让我的图像更小,但生成的缩放图像有锐利的边缘。

 foreach ($images as $image){
        $filename=$initPath.$sku.'/'.$srcFolder.'/'.$image;
        //$percent=0.5;
        list($width, $height) = getimagesize($filename);
        //$newwidth = $width * $percent;
        //$newheight = $height * $percent;
        $fh = fopen($initPath.$sku.'/'.$distFolder.'/'.$image, 'w');
        fclose($fh);
        $wtf= realpath($initPath.$sku.'/'.$distFolder.'/'.$image);

        // загрузка
        $thumb = imagecreatetruecolor(200, 200);
        imagesetinterpolation($thumb,IMG_BICUBIC);
        imagealphablending($thumb, false);
        imagesavealpha($thumb,true);
        $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);

        $source = imagecreatefrompng($filename);
        // изменение размера
        imagecopyresized($thumb, $source, 0, 0, 0, 0, 200, 200, $width, $height);
        // вывод
        imagepng($thumb,$wtf,1);

    }

原件:

enter image description here

结果: enter image description here

如何使用抗锯齿进行此操作?

1 个答案:

答案 0 :(得分:4)

使用imagecopyresampled代替imagecopyresized。它采用相同的参数并重新采样图像,而不仅仅是改变分辨率。

foreach ($images as $image){
    $filename=$initPath.$sku.'/'.$srcFolder.'/'.$image;
    //$percent=0.5;
    list($width, $height) = getimagesize($filename);
    //$newwidth = $width * $percent;
    //$newheight = $height * $percent;
    $fh = fopen($initPath.$sku.'/'.$distFolder.'/'.$image, 'w');
    fclose($fh);
    $wtf= realpath($initPath.$sku.'/'.$distFolder.'/'.$image);

    // загрузка
    $thumb = imagecreatetruecolor(200, 200);
    imagesetinterpolation($thumb,IMG_BICUBIC);
    imagealphablending($thumb, false);
    imagesavealpha($thumb,true);
    $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);

    $source = imagecreatefrompng($filename);
    // изменение размера
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, 200, 200, $width, $height);
    // вывод
    imagepng($thumb,$wtf,1);

}