让GD从二进制字符串中获取图像 [英] Have GD get image from binary string

查看:83
本文介绍了让GD从二进制字符串中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将图像存储在MySQL BLOB字段中的数据库。我设置了一个脚本,根据URL中的ID选择并显示图像,如果你追加?resize = 800x600,我也会这样做,它会调整图像大小(在这种情况下,为800x600)。

I have a database that stores images in a MySQL BLOB field. I setup a script that selects and displays the images based on an ID in the URL, and I also made it so if you append ?resize=800x600, it would resize the image (in this case, to 800x600).

我使用的主机没有安装Imagemagick,也不允许我自己动手,所以我需要使用PHP的GD库来调整​​图像大小。< br>
但是我还没有找到像Imagick的 readImageBlob()这样的函数,所以我无法编辑我从数据库中获得的二进制字符串首先创建一个临时文件,编辑它,从中获取二进制字符串,将其发送到浏览器,然后删除它(这是太多的步骤,特别是因为这将在生产时获得几千次点击) 。

The host that I use doesn't have Imagemagick installed and won't let me do it myself, so I need to use PHP's GD library to resize the image.
But I've yet to find a function like Imagick's readImageBlob(), so I can't edit the binary string that I get from the database without first creating a temporary file, editing it, getting the binary string from it, sending it to the browser, and then deleting it (which is waaaay too many steps, especially since this will be getting a few thousand hits when it goes into production).

所以我的问题是,有没有办法用PHP的GD复制 readImageBlob 而无需通过临时文件解决方案?

So my question is, is there any way to replicate readImageBlob with PHP's GD without going through the temporary file solution?

推荐答案

imagecreatefromstring()应该可以解决问题。我认为手册中的函数示例几乎就是您所需要的:

imagecreatefromstring() should do the trick. I think the function example in the manual is almost exactly what you need:

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

其中 $ data 是来自数据库的二进制数据字符串。

Where $data is your binary data string from the database.

这篇关于让GD从二进制字符串中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆