简体   繁体   English

php gd imagecreatefromstring() 和图像 mime 类型

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

is there a way to use imagecreatefromstring() and get somehow what is the image type?有没有办法使用imagecreatefromstring()并以某种方式获取图像类型?

When you use imagecreatefrom... methods, the image is loaded into memory as the uncompressed bitmap. 使用imagecreatefrom ...方法时,图像将作为未压缩的位图加载到内存中。 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);

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

Before using imagecreatefromstring( $string ) , the actual $string you're providing as the argument for that function can be used to determine the image type:使用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

Credit: https://stackoverflow.com/a/6061602/3705191信用: 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.).您必须将获得的字符串与常见图像文件的 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 );
// ...

Check this List of Common MIME Types for reference.查看此List of Common MIME Types以供参考。

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

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