imagecopyresampled теряет качество изображения php

Я хочу скопировать изменение размера изображения, используя imagecopyresampled в php. Он правильно изменяет размер, но теряет качество, или я могу сказать, что его края разрушаются, почему? Ниже приведен мой код, пожалуйста, просмотрите его и предоставьте решение .PHP GD-Library.

<?php
  $percent = 1.0;
  $filename ='filepath.png';
  $image_source = imagecreatefrompng($filename);
  list($old_width, $old_height) = getimagesize($filename );
  $width=$old_width;
  $height=$old_height;
  $new_width = $old_width * $percent;
  $new_height = $old_height * $percent;
  $transColor=imagecolorallocatealpha($image_source, 0, 0, 0, 0);
  imagecolortransparent($image_source, $transColor);
  $image_p = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_p, $image_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  header('Content-Type: image/png');
  imagepng($image_p);
  imagedestroy($image_p);
?>

person Anand Jain    schedule 19.09.2014    source источник
comment
возможный дубликат низкое качество php imagecopyresampled   -  person i alarmed alien    schedule 19.09.2014
comment
@тревожный инопланетянин, привет, я тоже использую jpg, но в этом случае я теряю прозрачность своих дизайнов, у тебя есть другая идея ??   -  person Anand Jain    schedule 19.09.2014


Ответы (2)


Попробуйте использовать этот код

<?php
$percent = 1.0;
$filename ='filepath.png';
$image_source = imagecreatefrompng($filename);
list($old_width, $old_height) = getimagesize($filename );
$width=$old_width;
$height=$old_height;
$new_width = $old_width * $percent;
$new_height = $old_height * $percent;
$transColor=imagecolorallocatealpha($image_source, 0, 0, 0, 0);
imagecolortransparent($image_source, $transColor);
$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
imagecopyresampled($image_p, $image_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
header('Content-Type: image/png');
imagepng($image_p);
imagedestroy($image_p);
?>
person Web Sticklers    schedule 19.09.2014
comment
это работает для моего кода, спасибо.... imagealphablending($image_p, false); изображениясохранитьальфа($image_p, правда); мне нужно было всегда добавлять обе строки, прежде чем использовать imagecopyresampled? - person Anand Jain; 19.09.2014

Ниже приведен код работы для меня, который вы можете настроить по своему требованию:

function textOutWithStroke($textSize, $textAngle, $x, $y, $textColor, $textFont, $text, $strokeColor = null, $borderSize = 1, $spacing = 0) {
if ($this->isImageCreated()){
    imagealphablending($this->handle, true);
    $textColor = imagecolorallocatealpha($this->handle, $textColor[0], $textColor[1], $textColor[2], $textColor[3]);
    if($strokeColor === null)
        $strokeColor = $textColor;
    else
        $strokeColor = imagecolorallocatealpha($this->handle, $strokeColor[0], $strokeColor[1], $strokeColor[2], $strokeColor[3]);

    if ($borderSize < 1){
        if ($spacing == 0)
            imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text);
        else{
            for ($i = 0; $i < strlen($text); $i++) {
                $letterBox = imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text[$i]);
                $x += $spacing + ($letterBox[2] - $letterBox[0]);
            }
        }
    }
    else{
        if ($spacing == 0) {
            for ($c1 = $x - $borderSize; $c1 <= $x + $borderSize; $c1++)
                for ($c2 = $y - $borderSize; $c2 <= $y + $borderSize; $c2++)
                    imagettftext($this->handle, $textSize, $textAngle, $c1, $c2, $strokeColor, $textFont, $text);

            imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text);
        }
        else{
            for ($i = 0; $i < strlen($text); $i++) {
                for ($c1 = $x - $borderSize; $c1 <= $x + $borderSize; $c1++)
                    for ($c2 = $y - $borderSize; $c2 <= $y + $borderSize; $c2++)
                        imagettftext($this->handle, $textSize, $textAngle, $c1, $c2, $strokeColor, $textFont, $text[$i]);

                $letterBox = imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text[$i]);
                $x += $spacing + ($letterBox[2] - $letterBox[0]) + (2 * $borderSize);
            }
        }


    }
    imagealphablending($this->handle, $this->alphaBlending);
    return true;
}
else
    return false;
}
person Community    schedule 02.05.2016