旋转PNG图像并更新回Mysql

时间:2014-09-17 10:32:34

标签: php mysql image-rotation

我想要旋转来自数据库的PNG图像并再次更新回mysql。

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

我的旋转功能

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

我得到的是

我的PNG图像显示为背景黑色。旋转根本不会发生。 感谢

2 个答案:

答案 0 :(得分:0)

您需要创建透明图层。 你会在这里找到解决方案:

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

此致 马修

答案 1 :(得分:0)

您可以看到this answers来旋转PNG。

要在DDBB中保存图像,您可以使用ob_get_content更改我附加的链接的功能,如下所示:

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

然后,您将$ imagedata保存到您的DDBB:

UPDATE your_table SET file_image = '.$imagedata.' WHERE .....