Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用php旋转png图像后获得透明背景?_Php_Rotation - Fatal编程技术网

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

如何在使用php旋转png图像后获得透明背景?,php,rotation,Php,Rotation,所以我有png图像,我旋转它,但我得到一个黑色的背景。。或者如果我做了R白色的色码,我会得到白色。。我试过这样做 $trans = imagecolorallocatealpha(image, 0, 0, 0, 127); imagerotate($image, $degree, $trans) 我也试过 $trans = imagecolorallocatealpha($image, 255, 255, 255, 127); 有人能帮我吗 这是我的密码。。如果我将allocatealpha

所以我有png图像,我旋转它,但我得到一个黑色的背景。。或者如果我做了R白色的色码,我会得到白色。。我试过这样做

$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仍然是黑色的


函数旋转($degrees){
$image=$this->image;
imagealphablending($image,false);
$color=IMAGECOLORDALLOCATEALPHA($image,0,0,0,127);
imagefill($this->image,0,0,$color);
$rotate=imagerotate($image,$degrees,$color);
imagesavealpha($image,TRUE);
$this->image=$rotate;

您尝试过这个吗


希望我理解你的问题!

确保将
imagesavealpha
设置为TRUE以保持透明度


imagesavealpha($image,TRUE);

你可以试试这个:

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

}

原始代码使用
image
而不是像这里这样使用
$image
吗?是的,他们原始代码使用$imageOk,所以我撒谎说imagecolortransparent不工作,只是因为白色背景看起来像那样。我再次尝试了这种方法,我知道它有效,因为我可以将背景更改为蓝色和红色等,但当我s与alpha通道一起,它使它对黑色透明。例如,如果我有这样的分配。0,0,255,75它只是使蓝色变暗,好像它使蓝色透明,但后面有黑色it@Great听!!记住检查一个答案是否正确!(不一定是我的,但对你帮助最大的人)是的,我发现它实际上不起作用。它只是一个白色背景,所以看起来是那样的,所以我仍然在寻找答案。谢谢你给我展示了这个函数!“imagecreatefromjpeg(“image.png”)”应该是“imagecreatefrompng(“image.png”)”
$destimg = imagecreatefromjpeg("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");
    $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);
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');

}