繁体   English   中英

php gd imagecreatefromstring() 和图像 mime 类型

[英]php gd imagecreatefromstring() and image mime type

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

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

$img = imagecreatefromstring($data);

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

imagedestroy($img);

http://us2.php.net/manual/en/function.imagecreatefromstring.php

使用imagecreatefromstring( $string )之前,您作为 function 的参数提供的实际$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 );
// ...

查看此List of Common MIME Types以供参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM