Friday, December 12

Losing transparency of png with php and imagemagick when rotation is applied?

re is working code for above problem.


<?php

    /* GD CODE */
        //save file
        $OutPutFileName = 'rotate_png.png';
        $angle=30;
        $left=10;
        $top=10;
       
        $png_image = imagecreatefrompng( 'source path/rotate_png.png' );
        $transparency = imagecolorallocatealpha( $png_image,0,0,0,127 );
        // rotate, last parameter preserves alpha when true
        $rotated = imagerotate( $png_image, $angle, $transparency, 1);
       
        // disable blendmode, we want real transparency
        imagealphablending( $rotated, false );
        // set the flag to save full alpha channel information
        imagesavealpha( $rotated, true );
       
        // now we want to start our output
        ob_end_clean();
        // we send image/png
        //header( 'Content-Type: image/png' );
        imagepng( $rotated, 'destination path/'.$FileName );
        // clean up the garbage
        imagedestroy( $png_image );
        imagedestroy( $rotated );
    /* End of GD CODE */
   
    /* Imagick CODE */
        //Let's read the images.
        $im = new Imagick();
        $im->readImage('source path/glass.jpg');
       
        $indexImg = new Imagick();
        $indexImg->readImage('destination path/'.$FileName);
       
        //Composite one image onto another
        $im->compositeImage($indexImg, Imagick::COMPOSITE_OVER, $left, $top);
       
        //Merges a sequence of images
    $im->flattenImages();
       
        //Save final Image
        $im->setImageFileName('destination path/final.jpg');
        // Let's write the image.
        if  (FALSE == $im->writeImage())
        {
            throw new Exception();
        }
        $im->destroy();
    /* End of Imagick CODE */

?>


No comments:

Post a Comment