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

Image resize in PHP with anti-aliasing

我想让图像变小,但缩放后的图像边缘很锐利。

 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);

    }

原文:

结果:

如何使用抗锯齿来做到这一点?

使用 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);

}