How do i get a transparent background after rotaing a png image with php?

前端 未结 7 1233
天命终不由人
天命终不由人 2020-12-19 10:38

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 = imagecol         


        
相关标签:
7条回答
  • 2020-12-19 10:46

    Have you tried this?

    imagecolortransparent

    Hope i understood your question!

    0 讨论(0)
  • 2020-12-19 10:49

    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');
    
    }
    
    0 讨论(0)
  • 2020-12-19 10:54

    You can try this: http://www.exorithm.com/algorithm/view/rotate_image_alpha

    0 讨论(0)
  • 2020-12-19 11:00
        $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);
    
    0 讨论(0)
  • 2020-12-19 11:03
    $destimg = imagecreatefromjpeg("image.png");
    $transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
    $rotatedImage = imagerotate($destimg, 200, $transColor);
    imagesavealpha($rotatedImage, true);
    imagepng($rotatedImage,"rotated.png");
    
    0 讨论(0)
  • 2020-12-19 11:06

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

    imagesavealpha($image, TRUE);

    0 讨论(0)
提交回复
热议问题