如何在PHP中从图像变量加载blob

时间:2014-10-07 19:11:30

标签: php mysql blob

我有一个应用程序,通过将fopen($fileServerTempName, 'br')传递给PDO参数出价,将图像加载到MySQL blob中。这非常有效。然而,我收到了新的要求,要编辑内存中的照片,同时使用imagecreatefrompng功能初始创建图像对象。现在我不确定如何从生成的图像对象中获取数据流以将修改后的图像加载回MySQL。如何从这样的数据流中获取数据流:

$sourceImage = imagecreatefrompng($fileServerTempName);
// do some modifications to $sourceImage

fopen生成的格式相同?我已经尝试将变量转换为二进制,编码和解码,但没有运气。任何帮助,将不胜感激。如果可能的话,我想避免在磁盘上创建实际修改映像的临时副本。

感谢。

1 个答案:

答案 0 :(得分:1)

ob_start ();
    imagesavealpha($sourceImage, true); // enable transparency
    imagepng ($sourceImage);
    $image_data = ob_get_contents (); 
ob_end_clean ();

您现在可以将图像保存回MySQL。