php image rotation and transparent png

Due to the need to deal with the resulting two-dimensional code image rotation, and then another one png picture merge, pictures are png

<?php
// this file writes the image into the http response,
// so we cant have any output other than headers and the file data
ob_start();

$filename       = 'qrcode.png'; //my qrcode background color is white
$degrees        = 7;

// open the image file
$im = imagecreatefrompng( $filename );

// create a transparent "color" for the areas which will be new after rotation
// r=255,b=255,g=255 ( white ), 127 = 100% transparency - we choose "invisible black"
$transparency = imagecolorallocatealpha( $im,255,255,255,0 );

// rotate, last parameter preserves alpha when true
$rotated = imagerotate( $im, $degrees, $transparency, 1);

//maybe there have make white color is transparent
$background = imagecolorallocate($rotated , 255,  255,  255);
imagecolortransparent($rotated,$background);

// 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 );
// clean up the garbage
imagedestroy( $im );
imagedestroy( $rotated );

Because, after image rotation, size of the picture will change, so you can use imagesx, imagesyyou can get the width and height of an image asset, you do not need to save the image, and then read the image, and then use getimagesize acquired image width and height.

reference
  1. imagecreatefrompng () Makes A Black background INSTEAD of transparent? - learn to use imagecolortransparent()methods
  2. A Resave with the then PNG Rotate Image Transparency - the main reference the answer and transparent png rotating low

Guess you like

Origin www.cnblogs.com/fsong/p/11260115.html
Recommended