2010-11-10 20 views
5

así que tengo la imagen PNG y girarlo pero consigo un fondo negro .. o si lo hago el código de color blanco OFR consigo blanco .. He intentado hacer esto ..¿Cómo obtengo un fondo transparente después de rotar una imagen png con php?

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

también he intentado ..

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

¿Alguien me puede ayudar?

aquí está mi código ... si cambio allocatealpha a 0, 0, 255, 0, entonces se vuelve azul. pero con 0, 0, 0, 127 sigue siendo negro.

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;

+0

¿El código original usa 'image' en lugar de' $ image' como aquí? – MiffTheFox

+0

sí, el código original usa $ image – Chris

Respuesta

0

Has probado esto?

imagecolortransparent

esperanza que entiende tu pregunta!

+0

¡Gracias! finalmente funcionó! – Chris

+0

@Great to hear !! ¡Recuerde verificar una respuesta como correcta! (Doest tiene que ser el mío, pero el que más te ayudó) – Trufa

+0

Sí, encontré que realmente no funcionaba. Era solo un fondo blanco, así que parecía de esa manera, así que aún estoy buscando una respuesta. ¡Gracias por mostrarme esa función! – Chris

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

Intenté esto antes y no funcionaba por alguna razón – Chris

+0

Ok, mentí diciendo que la imagen colortransparente no funciona, solo se veía así por un fondo blanco ... Intenté de esta manera otra vez y sé que funciona porque puedo cambiar el fondo a azul y rojo, etc., pero cuando me meto con el canal alfa, todo se vuelve transparente a negro ... por ejemplo, si tengo la asignación así ... 0, 0, 255, 75 solo hace un tono más oscuro de azul como si hace que el azul sea transparente pero hay un color negro detrás de él – Chris

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

Excelente respuesta ... funcionando perfectamente :-) – abhis

+2

"imagecreatefromjpeg (" image.png ")" debería ser "imagecreatefrompng (" image.png ")" –

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

Esto es lo que estoy utilizando, este trabajo grande para archivos .png con fondos transparentes. ¡Chocar los cinco!

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'); 

} 
Cuestiones relacionadas