我可以在不知道图像类型的情况下将图像读取到 GD 中吗?

发布于 2024-10-17 09:34:55 字数 451 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-10-24 09:34:55

可以使用imagecreatefromstring(),与实际文件格式无关,最好如果您已经读入该文件,则可以选择该选项。

但出于实际目的,您应该能够使用getimagesize($filename)。您不必事先读入该文件。只需使用文件名或 url。

print_r(
   getimagesize("http://sstatic.net/stackoverflow/img/sprites.png?v=2")
);

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.

print_r(
   getimagesize("http://sstatic.net/stackoverflow/img/sprites.png?v=2")
);
九命猫 2024-10-24 09:34:55

使用 imagecreatefromstringimagesx imagesy (后两个是为了防止另一个请求,因为它似乎与您有关):

$image = imagecreatefromstring(file_get_contents('http://path/to/image.jpg'));
list($width, $height) = array(imagesx($image), imagesy($image));

Use imagecreatefromstring, imagesx and imagesy (the latter two to prevent another request, since it seems to concern you):

$image = imagecreatefromstring(file_get_contents('http://path/to/image.jpg'));
list($width, $height) = array(imagesx($image), imagesy($image));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文