PHP旋转和合并时的图像质量问题

发布于 2024-10-14 21:34:20 字数 2872 浏览 2 评论 0原文

当我合并两个图像作为一个背景,另一个作为目标图像时。我正在使用 png。当我旋转目标图像然后合并时,是的,一切都很好,除了旋转图像的边缘变得锯齿形,我意味着不平滑。如何使用php GD使边缘平滑???

正常图像

旋转后

我正在使用的代码:

<?php

// Create image instances

$dest = imagecreatefrompng('bg.png');
$src = imagecreatefrompng('text.png');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 40, $color);
imagesavealpha($rotated, true);

// $trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
// imagefill($rotated, 0, 0, $trans_colour);

imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png?');
$width = imagesx($new_img);
$height = imagesy($new_img);

// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);

imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);

// Output and free from memory

header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
    if (!isset($pct)) {
        return false;
    }

    $pct/= 100;

    // Get image width and height

    $w = imagesx($src_im);
    $h = imagesy($src_im);

    // Turn alpha blending off

    imagealphablending($src_im, false);

    // Find the most opaque pixel in the image (the one with the smallest alpha value)

    $minalpha = 127;
    for ($x = 0; $x < $w; $x++)
    for ($y = 0; $y < $h; $y++) {
        $alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF;
        if ($alpha < $minalpha) {
            $minalpha = $alpha;
        }
    }

    // loop through image pixels and modify alpha for each

    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {

            // get current alpha value (represents the TANSPARENCY!)

            $colorxy = imagecolorat($src_im, $x, $y);
            $alpha = ($colorxy >> 24) & 0xFF;

            // calculate new alpha

            if ($minalpha !== 127) {
                $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
            }
            else {
                $alpha+= 127 * $pct;
            }

            // get the color index with new alpha

            $alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);

            // set pixel with the new color + opacity

            if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
                return false;
            }
        }
    }

    // The image copy

    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}

?>

When i merge two images as one background and the other as target image. I m using pngs. When I rotate the target image and then merge, yes everything is fine except that the edges of rotated image becomes zigzag i means not smooth. How to make the edges smooth using php GD???

Normal Image

After Rotation

The code I am using:

<?php

// Create image instances

$dest = imagecreatefrompng('bg.png');
$src = imagecreatefrompng('text.png');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 40, $color);
imagesavealpha($rotated, true);

// $trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
// imagefill($rotated, 0, 0, $trans_colour);

imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png?');
$width = imagesx($new_img);
$height = imagesy($new_img);

// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);

imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);

// Output and free from memory

header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
    if (!isset($pct)) {
        return false;
    }

    $pct/= 100;

    // Get image width and height

    $w = imagesx($src_im);
    $h = imagesy($src_im);

    // Turn alpha blending off

    imagealphablending($src_im, false);

    // Find the most opaque pixel in the image (the one with the smallest alpha value)

    $minalpha = 127;
    for ($x = 0; $x < $w; $x++)
    for ($y = 0; $y < $h; $y++) {
        $alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF;
        if ($alpha < $minalpha) {
            $minalpha = $alpha;
        }
    }

    // loop through image pixels and modify alpha for each

    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {

            // get current alpha value (represents the TANSPARENCY!)

            $colorxy = imagecolorat($src_im, $x, $y);
            $alpha = ($colorxy >> 24) & 0xFF;

            // calculate new alpha

            if ($minalpha !== 127) {
                $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
            }
            else {
                $alpha+= 127 * $pct;
            }

            // get the color index with new alpha

            $alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);

            // set pixel with the new color + opacity

            if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
                return false;
            }
        }
    }

    // The image copy

    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-10-21 21:34:20

我在一个旧项目中有这个并为我工作。无论如何,我不记得我的 png 是否真正具有 alpha 透明度或“gif”透明度...

$image = imagecreatefrompng($href);
$newImage = imagecreatefrompng($href2);
imagealphablending($image, true);
imagealphablending($newImage, true);
$newImage = imagerotate($newImage, $r, -1);
$blue = imagecolorallocate($newImage, 0, 0,255);
imagecolortransparent($newImage, $blue);
imagecopy($image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage));

根据手册,在这种情况下,imagealphablending 应该替换为 imagesavealpha

I had this in an old project and worked for me. Anyway, I don't remember if my pngs had really alpha transparency or "gif" transparency...

$image = imagecreatefrompng($href);
$newImage = imagecreatefrompng($href2);
imagealphablending($image, true);
imagealphablending($newImage, true);
$newImage = imagerotate($newImage, $r, -1);
$blue = imagecolorallocate($newImage, 0, 0,255);
imagecolortransparent($newImage, $blue);
imagecopy($image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage));

From the manual, in that case, imagealphablending should be replaced by imagesavealpha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文