如何在使用PHP旋转PNG图像后获得透明背景?

6

我有一个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;
}

原始代码是否使用 image 而不是像这里一样使用 $image - MiffTheFox
是的,它们原始代码使用 $image。 - Chris
7个回答

11
$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");

2
"imagecreatefromjpeg("image.png")" 应该是 "imagecreatefrompng("image.png")"。 - zeros-and-ones

2

0
        // 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);

好的,我承认我说谎了,imagecolortransparent 函数是可以正常工作的。之前看起来不行是因为背景是白色的。我再次尝试了一下,我知道它是可以正常工作的,因为我可以将背景更改为蓝色、红色等等。但是当我调整 alpha 通道时,它只会变成黑色透明。例如,如果我像这样分配颜色:0、0、255、75,它只会使蓝色变暗,就好像它使蓝色透明,但在其后面有一个黑色的颜色。 - Chris

0

这就是我正在使用的,非常适合具有透明背景的.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');

}

0

很高兴听到这个消息!记得选一个答案作为正确答案!(不一定是我的,而是对你最有帮助的答案) - Trufa
是的,我发现它实际上并没有起作用。它只是一个白色背景,所以它看起来是那样的,所以我还在寻找答案。谢谢你向我展示了那个函数! - Chris


0
    $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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接