使用php gd2将图像从一种格式保存到另一种格式 [英] Save image from one format to another with php gd2
问题描述
我有一个数据库,其中的一列填充有图像二进制数据.在进行了一些研究之后,我弄清楚了如何检测数据以哪种图像格式.可以说图像列中的记录为 gif 格式,现在我想用php gd2将其保存为 jpeg 格式.拜托,有人可以告诉我该怎么做吗?
I have a database with a column filled with image binaries data. After I made some research I figuried out how to detect in which image format is the data. Lets say in of the records in my images column is in gif format, now I want to save it with php gd2 to jpeg format. Please, can someone tell me how can I do that?
推荐答案
如果您只想将图像数据转换为JPEG,则只需 imagecreatefromstring
和 imagejpeg
.基本上:
If you only want to convert the image data to JPEG then all you need is imagecreatefromstring
and imagejpeg
. Basically:
imagejpeg(imagecreatefromstring($gif_bindata), "temp.jpeg");
$jpeg_bindata = file_get_contents("temp.jpeg");
就这么简单,因为imagecreatefromstring
自动检测文件类型(前几个字节包含足以使检测可行的魔术字节).显然,您可能想使用实际的临时文件名.
It's that simple because imagecreatefromstring
detects the file type automatically (the first few bytes contain enough magic bytes to make detection feasible). And obviously you might want to use a real temporary filename instead.
这篇关于使用php gd2将图像从一种格式保存到另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!