我可以在不知道图像类型的情况下将图像读取到 GD 中吗?
GD 似乎有各种 imagecreatefrom_x_
函数,如果您提前知道图像类型,这会很棒。在我的特殊情况下,我不会。有没有办法在不知道类型的情况下读取图像?
到目前为止,我唯一的选择是将其保存到磁盘(昂贵!),然后使用 getimagesize (它还提供 mime 类型) - 但这会导致我读取图像两次 - 一次确定 MIME键入,然后再次将其读入 GD。
或者,是否可以将变量视为文件?像这样的东西:
$image = file_get_contents('http://the.remote.file.com/myfile');
# doesn't work with already read in image ... can I treat this as a file w/o saving it?
$info = getimagesize($image);
GD seems to have various imagecreatefrom_x_
functions which is great if you know the image type ahead of time. In my particular situation, I don't. Is there a way to read in the image without knowing the type?
My only option so far is to save it to disk (expensive!) and then use getimagesize
(which also provides mime type) - but this results in me reading in the image twice - once to determine the MIME type and then again to read it into GD.
Alternatively, is it possible to just treat a variable as a file? something like this:
$image = file_get_contents('http://the.remote.file.com/myfile');
# doesn't work with already read in image ... can I treat this as a file w/o saving it?
$info = getimagesize($image);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用
imagecreatefromstring()
,与实际文件格式无关,最好如果您已经读入该文件,则可以选择该选项。但出于实际目的,您应该能够使用
getimagesize($filename)
。您不必事先读入该文件。只需使用文件名或 url。You can use
imagecreatefromstring()
, which is indifferent to the actual file format and the best option if you've read in the file already.But for practical purposes you should be able to use
getimagesize($filename)
. You don't have to read in the file before. Just use the filename or url.使用 imagecreatefromstring、imagesx 和 imagesy (后两个是为了防止另一个请求,因为它似乎与您有关):
Use imagecreatefromstring, imagesx and imagesy (the latter two to prevent another request, since it seems to concern you):