用php旋转png图像后如何获得透明背景?

发布于 2024-10-02 07:13:50 字数 736 浏览 5 评论 0原文

所以我有 png 图像,我旋转它,但我得到黑色背景..或者如果我执行白色的颜色代码,我会得到白色..我尝试这样做..

$trans = imagecolorallocatealpha(image, 0, 0, 0, 127);
imagerotate($image, $degree, $trans)

我也尝试过..

$trans = imagecolorallocatealpha($image, 255, 255, 255, 127);

有人可以帮助我吗?

这是我的代码..如果我将 allocatealpha 更改为 0, 0, 255, 0 那么它会变成蓝色。但使用 0, 0, 0, 127 时它仍然是黑色的。

function rotate($degrees) {
    $image = $this->image;
    imagealphablending($image, false);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
    imagefill($this->image, 0, 0, $color);
    $rotate = imagerotate($image, $degrees, $color);
    imagesavealpha($image, TRUE);
    $this->image = $rotate;
}

So I have png image and I rotate it but i get a black background.. or if i do the color code ofr white i get white.. I tried doing this..

$trans = imagecolorallocatealpha(image, 0, 0, 0, 127);
imagerotate($image, $degree, $trans)

i have also tried..

$trans = imagecolorallocatealpha($image, 255, 255, 255, 127);

Can someone help me out?

here is my code.. if i change allocatealpha to 0, 0, 255, 0 then it goes blue. but with 0, 0, 0, 127 its still black.

function rotate($degrees) {
    $image = $this->image;
    imagealphablending($image, false);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
    imagefill($this->image, 0, 0, $color);
    $rotate = imagerotate($image, $degrees, $color);
    imagesavealpha($image, TRUE);
    $this->image = $rotate;
}

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

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

发布评论

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

评论(7

暖阳 2024-10-09 07:13:50
$destimg = imagecreatefromjpeg("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");

根据下面的正确评论,这应该是:

$destimg = imagecreatefrompng("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");
$destimg = imagecreatefromjpeg("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");

As per the correct comment below, this should be:

$destimg = imagecreatefrompng("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");
美胚控场 2024-10-09 07:13:50

确保将 imagesavealpha 设置为 TRUE 以保留透明度。 http://www.php.net/manual/en/function.imagesavealpha.php

imagesavealpha($image, TRUE);

Make sure you set imagesavealpha to TRUE in order to preserve transparency. http://www.php.net/manual/en/function.imagesavealpha.php

imagesavealpha($image, TRUE);

在梵高的星空下 2024-10-09 07:13:50

你试过这个吗?

imagecolortransparent

希望我理解你的问题!

Have you tried this?

imagecolortransparent

Hope i understood your question!

拍不死你 2024-10-09 07:13:50
        // Turn off transparency blending (temporarily)
        imagealphablending($image, false);

        // Create a new transparent color for image
        $color = imagecolorallocatealpha($image, 0, 0, 0, 127);

        // Completely fill the background of the new image with allocated color.
        imagefill($image, 0, 0, $color);

        // Restore transparency blending
        imagesavealpha($image, true);
        // Turn off transparency blending (temporarily)
        imagealphablending($image, false);

        // Create a new transparent color for image
        $color = imagecolorallocatealpha($image, 0, 0, 0, 127);

        // Completely fill the background of the new image with allocated color.
        imagefill($image, 0, 0, $color);

        // Restore transparency blending
        imagesavealpha($image, true);
洒一地阳光 2024-10-09 07:13:50
    $info = pathinfo($pathToImage);
    $name = str_replace("." . $info['extension'], "", $info['basename']);

    $size = getimagesize($pathToImage);



    $type = isset($size['type']) ? $size['type'] : $size[2];

    // Check support of file type
    if (!(imagetypes() & $type)) {
        // Server does not support file type
        return false;
    }

    $source = self::imageCreateFrom($pathToImage, trim($info['extension'])) or notfound();
    $transColor = imagecolorallocatealpha($source, 255, 255, 255, 127);

    // $transparency = imagecolorallocatealpha($source, 0, 0, 0, 127);
    $rotate = imagerotate($source, 360 - $rotate_angle, $transColor);
    //imagealphablending($rotate, false);


    imagesavealpha($rotate, TRUE);
    //imagejpeg($rotate,$pathToThumbs.DIRECTORY_SEPARATOR.$info['basename']);
    self::createImage($rotate, $pathToThumbs, $info['basename'], trim($info['extension']));

    // imagejpeg($rotate);
    imagedestroy($source);
    imagedestroy($rotate);
    $info = pathinfo($pathToImage);
    $name = str_replace("." . $info['extension'], "", $info['basename']);

    $size = getimagesize($pathToImage);



    $type = isset($size['type']) ? $size['type'] : $size[2];

    // Check support of file type
    if (!(imagetypes() & $type)) {
        // Server does not support file type
        return false;
    }

    $source = self::imageCreateFrom($pathToImage, trim($info['extension'])) or notfound();
    $transColor = imagecolorallocatealpha($source, 255, 255, 255, 127);

    // $transparency = imagecolorallocatealpha($source, 0, 0, 0, 127);
    $rotate = imagerotate($source, 360 - $rotate_angle, $transColor);
    //imagealphablending($rotate, false);


    imagesavealpha($rotate, TRUE);
    //imagejpeg($rotate,$pathToThumbs.DIRECTORY_SEPARATOR.$info['basename']);
    self::createImage($rotate, $pathToThumbs, $info['basename'], trim($info['extension']));

    // imagejpeg($rotate);
    imagedestroy($source);
    imagedestroy($rotate);
林空鹿饮溪 2024-10-09 07:13:50

这就是我正在使用的,这对于具有透明背景的 .png 文件非常有用。击掌!

function rotate($degrees) {

    // Switch from default counter-clockwise to clockwise
    $degrees = 360 - $degrees; 

    // Get the image 
    $source =  imagecreatefrompng("images/image.png");

    // Rotate the image
    $rotated_image = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127));

    // Save the rotated image
    imagepng($rotated_image, 'images/rotated_image.png');

}

This is what I am using, this work great for .png files with transparent backgrounds. High fives!

function rotate($degrees) {

    // Switch from default counter-clockwise to clockwise
    $degrees = 360 - $degrees; 

    // Get the image 
    $source =  imagecreatefrompng("images/image.png");

    // Rotate the image
    $rotated_image = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127));

    // Save the rotated image
    imagepng($rotated_image, 'images/rotated_image.png');

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