Because of the need to process the generated two-dimensional code image rotation, and then merge with another png image, the pictures are all 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 the size of the image changes after rotation, imagesx and imagesy can be used to obtain the width and height of the image resources, so there is no need to save the image, then read the image, and then use getimagesize to get the width and height of the image.
Reference resources
- imagecreatefrompng() Makes a black background instead of transparent? - Learn to use the imagecolortransparent() method
- Rotate a PNG then resave with Image Transparency -- Refer to this answer mainly to rotate png and transparent low