imagerotate() 中的背景透明度

发布于 2024-08-29 12:47:31 字数 527 浏览 3 评论 0原文

自过去 2 天以来,我尝试使用 imagerotate() PHP-GD 函数旋转图像后向背景添加透明度。

但是,令我非常失望的是,它根本不起作用。

它只是在其背后提供黑色背景。

这是我的代码 -

$patchImageS    =   'image.png'; // the image to be patched over the final bg
$patchImage =   imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage     =   imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');

我尝试将函数中传递的参数更改为

imagerotate($patchImage, 23, 5, 0);

imagerotate($patchImage, 23, 0, 5);

任何帮助将不胜感激。

Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.

But, to my great disappointment, it's not working at all.

It's just giving out a black background behind it.

Here's my code -


$patchImageS    =   'image.png'; // the image to be patched over the final bg
$patchImage =   imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage     =   imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');

I tried to change the parameters being passed in function to

imagerotate($patchImage, 23, 5, 0);

imagerotate($patchImage, 23, 0, 5);

Any help would be highly appreciated.

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

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

发布评论

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

评论(3

以歌曲疗慰 2024-09-05 12:47:31

在完成了 99% 的答案之后,这是我找到的解决方案:

// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);

// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);

// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);

// Set your appropriate header
header('Content-Type: image/png');

// Render canvas to the browser
imagepng($png);

// Clean up
imagedestroy($png);

这里的关键是将你的 imagecolorallocatealpha() 包含在你的 imagerotate() 调用中......

After a number of 99% finished answers, here's the solution I've found:

// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);

// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);

// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);

// Set your appropriate header
header('Content-Type: image/png');

// Render canvas to the browser
imagepng($png);

// Clean up
imagedestroy($png);

The key here is to include your imagecolorallocatealpha() in your imagerotate() call...

白色秋天 2024-09-05 12:47:31

在 php 文档中查找 imagesavealpha() - 我认为这就是您正在寻找的。

编辑:这是一个例子:

$png = imagecreatefrompng('./alphachannel_example.png');

// Do required operations
$png = imagerotate($png, 23, 0, 0);

// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

// Output image to browser
header('Content-Type: image/png');

imagepng($png);
imagedestroy($png);

look for imagesavealpha() in the php-documentation - i think this is what you are looking for.

EDIT: here's an example:

$png = imagecreatefrompng('./alphachannel_example.png');

// Do required operations
$png = imagerotate($png, 23, 0, 0);

// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

// Output image to browser
header('Content-Type: image/png');

imagepng($png);
imagedestroy($png);
Saygoodbye 2024-09-05 12:47:31

对于任何遇到背景上有黑条的 imagecopyresampledimagerotate 问题的人,我在这里找到了一个代码示例:

https://qna.habr.com/q/646622#answer_1417035

        // get image sizes (X,Y)
        $wx = imagesx($imageW);
        $wy = imagesy($imageW);

        // create a new image from the sizes on transparent canvas
        $new = imagecreatetruecolor($wx, $wy);

        $transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
        $rotate = imagerotate($imageW, 280, $transparent);
        imagealphablending($rotate, true);
        imagesavealpha($rotate, true);

        // get the newest image X and Y
        $ix = imagesx($rotate);
        $iy = imagesy($rotate);

        //copy the image to the canvas
        imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);

For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:

https://qna.habr.com/q/646622#answer_1417035

        // get image sizes (X,Y)
        $wx = imagesx($imageW);
        $wy = imagesy($imageW);

        // create a new image from the sizes on transparent canvas
        $new = imagecreatetruecolor($wx, $wy);

        $transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
        $rotate = imagerotate($imageW, 280, $transparent);
        imagealphablending($rotate, true);
        imagesavealpha($rotate, true);

        // get the newest image X and Y
        $ix = imagesx($rotate);
        $iy = imagesy($rotate);

        //copy the image to the canvas
        imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文