如何在PHP(GD)上修复图像质量?

如何在PHP(GD)上修复图像质量?,php,gd,Php,Gd,我已经尝试解决边界图像问题很多天了,但我仍然无法做出决定。。我真的需要你的帮助 我有密码: $image = imagecreatefromjpeg('https://vignette4.wikia.nocookie.net/matrix/images/1/1f/Monica_Bellucci_Dolce_and_Gabbana.jpg/revision/latest?cb=20130227074822'); $w = imagesx($image); $h = imagesy($image);

我已经尝试解决边界图像问题很多天了,但我仍然无法做出决定。。我真的需要你的帮助

我有密码:

$image = imagecreatefromjpeg('https://vignette4.wikia.nocookie.net/matrix/images/1/1f/Monica_Bellucci_Dolce_and_Gabbana.jpg/revision/latest?cb=20130227074822');
$w = imagesx($image);
$h = imagesy($image);

$border = imagecreatefrompng('http://meson.ad-l.ink/8mRbHMnS9/thumb.png');
$borderW = imagesx($border);
$borderH = imagesy($border);

$borderSize = 70;


// New image width and height

$isHorizontalAlign = true;

$coProp = $w / $h;

if ($coProp > 1)
    $isHorizontalAlign = false;

if ($isHorizontalAlign) {
    $newWidth = $w - $borderSize * 2;
    $newHeight = $newWidth * ($h / $w);
} else {
    $newHeight = $h - $borderSize * 2;
    $newWidth = $newHeight * ($w / $h);
}


// Transparent border

$indent = imagecreatetruecolor($w, $h);

//imagesavealpha($indent, true);
$color = imagecolorallocatealpha($indent, 0, 0, 0, 127);
imagefill($indent, 0, 0, $color);


$paddingLeft = ($w - $newWidth) / 2;
$paddingTop = ($h - $newHeight) / 2;

imagecopyresampled($indent, $image, $paddingLeft, $paddingTop, 0, 0, $newWidth, $newHeight, $w, $h);


// New border width

$x1 = $newWidth;
$x2 = $borderSize;
$x3 = (int)($x1 / $x2);

if ($x3 == $x1 / $x2)
    $bw = $borderSize;
else {
    $x4 = $x1 - $x3 * $x2;
    $x5 = $x4 / $x3;
    $x2 = $x2 + $x5;

    $bw = $x2;
}


// New border height

$y1 = $newHeight;
$y2 = $borderSize;
$y3 = (int)($y1 / $y2);

if ($y3 == $y1 / $y2)
    $bh = $borderSize;
else {
    $y4 = $y1 - $y3 * $y2;
    $y5 = $y4 / $y3;
    $y2 = $y2 + $y5;

    $bh = $y2;
}


// Horizontal

$percent1 = $bw / $borderSize;

$borderNewWidth1 = (int)$borderW * $percent1;
$borderNewHeight1 = (int)$borderH * $percent1;

$thumb1 = imagecreatetruecolor($borderNewWidth1, $borderNewHeight1);
imagesavealpha($thumb1, true);
$color = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
imagefill($thumb1, 0, 0, $color);
imagecopyresized($thumb1, $border, 0, 0, 0, 0, $borderNewWidth1, $borderNewHeight1, $borderW, $borderH);


// Vertical

$percent2 = $bh / $borderSize;

$borderNewWidth2 = (int)$borderW * $percent2;
$borderNewHeight2 = (int)$borderH * $percent2;

$thumb2 = imagecreatetruecolor($borderNewWidth2, $borderNewHeight2);
imagesavealpha($thumb2, true);
$color = imagecolorallocatealpha($thumb2, 0, 0, 0, 127);
imagefill($thumb2, 0, 0, $color);
imagecopyresized($thumb2, $border, 0, 0, 0, 0, $borderNewWidth2, $borderNewHeight2, $borderW, $borderH);


// Angles

$borderNewWidth3 = (int)$borderW * $percent1;
$borderNewHeight3 = (int)$borderH * $percent2;

$thumb3 = imagecreatetruecolor($borderNewWidth3, $borderNewHeight3);
imagesavealpha($thumb3, true);
$color = imagecolorallocatealpha($thumb3, 0, 0, 0, 127);
imagefill($thumb3, 0, 0, $color);
imagecopyresized($thumb3, $border, 0, 0, 0, 0, $borderNewWidth3, $borderNewHeight3, $borderW, $borderH);


// Horizontal border

$horizontalX = ($w - $newWidth) / 2;
$horizontalY = (($h - $newHeight) / 2 - $bw) + 1;
$horizontalY2 = $h - ($h - $newHeight) / 2;

for ($i = 0; $i < round($newWidth / $bw); $i++) {
    // Top
    imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY, $borderSize * $percent1, 0, $bw + 1, $bw);

    // Bottom
    imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY2, $borderSize * $percent1, $borderSize * 2 * $percent1, $bw + 1, $bw - 1);
}


// Vertical border

$verticalY = ($h - $newHeight) / 2;
$verticalX = (($w - $newWidth) / 2 - $bh) + 1;
$verticalX2 = $w - ($w - $newWidth) / 2;

for ($i = 0; $i < round($newHeight / $bh); $i++) {
    // Left
    imagecopy($indent, $thumb2, $verticalX, $verticalY + ($i * $bh), 0, $borderSize * $percent2, $bh, $bh + 1);

    // Right
    imagecopy($indent, $thumb2, $verticalX2, $verticalY + ($i * $bh), ($borderSize * $percent2) * 2, $borderSize * $percent2, $bh, $bh + 1);
}


// Left top border
imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, (($h - $newHeight) / 2 - $bh) + 1, 0, 0, $bw, $bh);

// Left bottom border
imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, $h - ($h - $newHeight) / 2, 0, ($borderSize * 2) * $percent2, $bw, $bh);

// Right top border
imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, (($h - $newHeight) / 2 - $bh) + 1, ($borderSize * 2) * $percent1, 0, $bw, $bh);

// Right bottom border
imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, $h - ($h - $newHeight) / 2, ($borderSize * 2) * $percent1, ($borderSize * 2) * $percent2, $bw, $bh);

// Save result to base64

header('Content-Type: image/png');
imagepng($indent);
$image=imagecreatefromjpeg('https://vignette4.wikia.nocookie.net/matrix/images/1/1f/Monica_Bellucci_Dolce_and_Gabbana.jpg/revision/latest?cb=20130227074822');
$w=imagesx($image);
$h=imagesy($image);
$border=imagecreatefrompng('http://meson.ad-l.ink/8mRbHMnS9/thumb.png');
$borderW=imagesx($border);
$borderH=imagesy($border);
$borderSize=70;
//新图像宽度和高度
$isHorizontalAlign=true;
$coProp=$w/$h;
如果($coProp>1)
$isHorizontalAlign=false;
如果($isHorizontalAlign){
$newWidth=$w-$borderSize*2;
$newHeight=$newWidth*($h/$w);
}否则{
$newHeight=$h-$borderSize*2;
$newWidth=$newHeight*($w/$h);
}
//透明边框
$indent=ImageCreateTureColor($w,$h);
//imagesavealpha($indent,true);
$color=imagecolorallocatealpha($indent,0,0,0127);
图像填充($indent,0,0,$color);
$paddingLeft=($w-$newWidth)/2;
$paddingTop=($h-$newHeight)/2;
imagecopyresampled($indent,$image,$paddingLeft,$paddingTop,0,0,$newWidth,$newHeight,$w,$h);
//新边框宽度
$x1=$newWidth;
$x2=$borderSize;
$x3=(整数)($x1/$x2);
如果($x3==$x1/$x2)
$bw=$borderSize;
否则{
$x4=$x1-$x3*$x2;
$x5=$x4/$x3;
$x2=$x2+x5;
$bw=$x2;
}
//新边界高度
$y1=$newHeight;
$y2=$borderSize;
$y3=(整数)($y1/$y2);
如果($y3=$y1/$y2)
$bh=$borderSize;
否则{
$y4=$y1-$y3*$y2;
$y5=$y4/$y3;
$y2=$y2+$y5;
$bh=$y2;
}
//水平的
$percent1=$bw/$borderSize;
$borderNewWidth1=(int)$borderW*$percent1;
$borderNewHeight1=(int)$borderH*$percent1;
$thumb1=ImageCreateTureColor($borderNewWidth1,$borderNewHeight1);
imagesavealpha($thumb1,true);
$color=imagecolorallocatealpha($thumb1,0,0,0127);
imagefill($thumb1,0,0,$color);
imagecopyresized($thumb1,$border,0,0,0,$borderNewWidth1,$borderNewHeight1,$borderW,$borderH);
//垂直的
$percent2=$bh/$borderSize;
$borderNewWidth2=(int)$borderW*$percent2;
$borderNewHeight2=(int)$borderH*$percent2;
$thumb2=ImageCreateTureColor($borderNewWidth2,$borderNewHeight2);
imagesavealpha($thumb2,true);
$color=imagecolorallocatealpha($thumb2,0,0,0127);
imagefill($thumb2,0,0,$color);
imagecopyresized($thumb2,$border,0,0,0,$borderNewWidth2,$borderNewHeight2,$borderW,$borderH);
//角度
$borderNewWidth3=(int)$borderW*$percent1;
$borderNewHeight3=(int)$borderH*$percent2;
$thumb3=ImageCreateTureColor($borderNewWidth3,$borderNewHeight3);
imagesavealpha($thumb3,true);
$color=imagecolorallocatealpha($thumb3,0,0,0127);
imagefill($thumb3,0,0,$color);
imagecopyresized($thumb3,$border,0,0,0,$borderNewWidth3,$borderNewHeight3,$borderW,$borderH);
//水平边界
$horizontalX=($w-$newWidth)/2;
$horizontalY=($h-$newHeight)/2-$bw)+1;
$horizontalY2=$h-($h-$newHeight)/2;
对于($i=0;$i
(默认情况下,我有一个透明的背景,但举个例子,我删除了它)

当图像尺寸较大时,一切都能完美工作:

但当我添加一个小图像时,它的质量会显著降低。看看这个:

我不知道如何解决这个问题

注:我正在尝试使用类似的边框图像为我的照片制作边框: 我做的计算花费了很多努力,但结果我无法实现

谢谢大家的关注!
我希望能解决这个问题。

您可能会有更好的时间将此问题分为两个问题,一个是关于编程问题的质量,另一个是关于底部重复的问题,可能更适合数学论坛。小错误已修复。我只是在圆上更改了这个(int)($newWidth/$bw),也在第二个中更改了。边界问题的质量是因为角度在像素之间。由于您似乎没有进行任何抗锯齿处理,因此它似乎是在选取最接近的整像素。整数是整数对于质量问题来说是有意义的。请不要使用天花板、地板和圆形。我能做到。但对于质量,您应该使用imagemagic。它速度快,可以选择过滤器/压缩质量(平滑算法、缩放、压缩等)。您可能会有更好的时间将其分为两个问题:一个是关于编程问题的质量,另一个是关于编程问题