ImageCopy重新采样图像质量损失php

ImageCopy重新采样图像质量损失php,php,image-resizing,image-manipulation,php-gd,Php,Image Resizing,Image Manipulation,Php Gd,我想用php中的imagecopyresampled复制并合并图像。它正在适当调整大小,但失去了它的质量,或者我可以说它的边缘被破坏了,为什么? 下面是我的代码,请查看并给出solution.PHP GD库 <?php $percent = 1.0; $filename ='filepath.png'; $image_source = imagecreatefrompng($filename); list($old_width, $old_height) = getimag

我想用php中的imagecopyresampled复制并合并图像。它正在适当调整大小,但失去了它的质量,或者我可以说它的边缘被破坏了,为什么? 下面是我的代码,请查看并给出solution.PHP GD库

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

尝试使用此代码

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

以下代码适用于我您可以根据您的要求定制:

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;
}
函数textOutWithStroke($textSize、$textAngle、$x、$y、$textColor、$textFont、$text、$strokeColor=null、$borderSize=1、$spacing=0){
如果($this->isImageCreated()){
imagealphablending($this->handle,true);
$textColor=imagecolorallocatealpha($this->handle,$textColor[0]、$textColor[1]、$textColor[2]、$textColor[3]);
如果($strokeColor==null)
$strokeColor=$textColor;
其他的
$strokeColor=imagecolorallocatealpha($this->handle,$strokeColor[0]、$strokeColor[1]、$strokeColor[2]、$strokeColor[3]);
如果($borderSize<1){
如果($spating==0)
imagettftext($this->handle,$textSize,$textAngle,$x,$y,$textColor,$textFont,$text);
否则{
对于($i=0;$ihandle,$textSize,$textAngle,$x,$y,$textColor,$textFont,$text[$i]);
$x+=$SPACE+($letterBox[2]-$letterBox[0]);
}
}
}
否则{
如果($spating==0){
对于($c1=$x-$borderSize;$c1句柄、$textSize、$textAngle、$x、$y、$textColor、$textFont、$text);
}
否则{
对于($i=0;$ihandle,$this->alphaBlending);
返回true;
}
其他的
返回false;
}

可能重复@alarmed alien,您好,我也使用了jpg,但在这种情况下,我的设计失去了透明度。您有其他想法吗?它适用于我的代码thanx。。。。imagealphablending($image\u p,false);imagesavealpha($image_p,true);在使用imagecopyresampled之前,是否必须始终添加两行?