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

时间:2010-11-10 20:30:49

标签: php rotation

所以我有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;

7 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:2)

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

imagesavealpha($image, TRUE);

答案 2 :(得分:0)

你试过这个吗?

imagecolortransparent

希望我理解你的问题!

答案 3 :(得分: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);

答案 4 :(得分:0)

答案 5 :(得分: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);

答案 6 :(得分: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');

}