0

I wanted to rotate PNG images which is coming from DB and again update back to mysql.

$DataImage=$rs[0]['file_image']; // image from DB as base 64

My Rotate Function

ob_start();
header( 'Content-Type: image/png' );
$destImage = imagerotate($DataImage, 90, 0) ;
imagepng($destImage);
$image_thumb =mysql_real_escape_string(ob_get_contents());
$imageDV=$image_thumb;
ob_end_clean();

What i am getting is

My PNG Images are displaying with background black. And Rotation is not at all happening. Thanks

2 Answers2

0

You need to create a transparent layer. You will find the solution here :

ob_start();
header( 'Content-Type: image/png' );
$transp = imagecolorallocatealpha($DataImage,0,0,0,127 );
$destImage  = imagerotate($DataImage, 90, $transp, 1);
imagealphablending($destImage, false );
imagesavealpha($destImage, true );
imagepng($destImage);
ob_end_clean();

Sincerely, Mathieu

  • Let me know, also try with the $DataImage = imagecreatefromstring($DataImage) to convert the base64 stored string to a png file. Let me know in any case :) – ITMG-Consulting Sep 18 '14 at 08:01
0

You could see this answers to rotate a PNG.

To save the image in DDBB, you could change the function of the link that i attached you with ob_get_content like this:

ob_start();
$im = imagecreatefrompng($DataImage);
// create a transparent "color" for the areas which will be new after rotation
// only quadratic images will not change dimensions
// r=0,b=0,g=0 ( black ), 127 = 100% transparency - we choose "invisible black"
$transparency = imagecolorallocatealpha( $im,0,0,0,127 );

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

// disable blendmode, we want real transparency
imagealphablending( $rotated, false );
// set the flag to save full alpha channel information
imagesavealpha( $rotated, true );  
// we send image/png
imagepng( $rotated );
// save the result of imagepng in var "$imagedata" with ob_get_contents
$imagedata = ob_get_contents();
ob_end_clean();
imagedestroy( $im );
imagedestroy( $rotated );

Then, you will save $imagedata to your DDBB:

UPDATE your_table SET file_image = '.$imagedata.' WHERE .....
Community
  • 1
  • 1
Avara
  • 1,753
  • 2
  • 17
  • 24