简体   繁体   中英

Image resize in PHP with anti-aliasing

I want to make my images smaller, but the resulting scaled images have sharp edges.

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

    }

Original:

在此处输入图片说明

Result: 在此处输入图片说明

How can I do it with anti-aliasing?

Use imagecopyresampled instead of imagecopyresized . It takes the same parameters and will resample the image instead of just altering the resolution.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM