php gd imagecreatefromstring() 和图像 mime 类型

发布于 2024-12-01 07:43:28 字数 52 浏览 3 评论 0原文

有没有办法使用 imagecreatefromstring() 并以某种方式获取图像类型?

is there a way to use imagecreatefromstring() and get somehow what is the image type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

穿透光 2024-12-08 07:43:28

当您使用 imagecreatefrom... 方法时,图像将作为未压缩的位图加载到内存中。此时还没有真正的图像类型。您可以使用 image... 功能将其保存为您想要的任何类型。

$img = imagecreatefromstring($data);

imagepng($img, "file path and name");

imagedestroy($img);

https://www.php.net/manual/en/function.imagecreatefromstring。 php

When you use imagecreatefrom... methods, the image is loaded into memory as the uncompressed bitmap. there is not really a image type at this point. you can save it back out as whatever type you wish using the image... function.

$img = imagecreatefromstring($data);

imagepng($img, "file path and name");

imagedestroy($img);

https://www.php.net/manual/en/function.imagecreatefromstring.php

七颜 2024-12-08 07:43:28

使用 imagecreatefromstring( $string ) 之前,您提供的实际 $string 作为该函数的参数可以用于确定图像类型:

$imgstring = "...";
// imagecreatefromstring( $imgstring ); -- DON'T use this just yet

$f = finfo_open();

$mime_type = finfo_buffer($f, $imgstring, FILEINFO_MIME_TYPE);
// $mime_type will hold the MIME type, e.g. image/png

来源:https://stackoverflow.com/a/6061602/3705191

您必须将获得的字符串与常见图像文件的 MIME 类型(image/jpegimage/pngimage/gif 等)。

$img = imagecreatefromstirng( $imgstring );

if( $mime_type == "image/png" )
  imagepng( $img, $filepath_to_save_to );
if( $mime_type == "image/jpeg" )
  imagejpeg( $img, $filepath_to_save_to );
// ...

检查此常见 MIME 类型列表参考。

Before using imagecreatefromstring( $string ), the actual $string you're providing as the argument for that function can be used to determine the image type:

$imgstring = "...";
// imagecreatefromstring( $imgstring ); -- DON'T use this just yet

$f = finfo_open();

$mime_type = finfo_buffer($f, $imgstring, FILEINFO_MIME_TYPE);
// $mime_type will hold the MIME type, e.g. image/png

Credit: https://stackoverflow.com/a/6061602/3705191

You'll have to compare the string you get with the MIME type of common image files (image/jpeg, image/png, image/gif, etc.).

$img = imagecreatefromstirng( $imgstring );

if( $mime_type == "image/png" )
  imagepng( $img, $filepath_to_save_to );
if( $mime_type == "image/jpeg" )
  imagejpeg( $img, $filepath_to_save_to );
// ...

Check this List of Common MIME Types for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文