2017-02-20 133 views
1

我想縮小圖像尺寸,但縮放後的圖像具有銳利的邊緣。使用抗鋸齒功能在PHP中調整圖像大小

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

如何與抗鋸齒辦呢?

回答

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

} 
+0

這條線對我來說會引發一個錯誤,但是我想這對我的php和gd lib是正確的嗎? – tttaaabbb

+0

你看到什麼錯誤? – Jim

+0

致命錯誤:調用未定義的函數imageantialias() – tttaaabbb