Ali*_*xel 6 php resources gd image image-processing
只有一个有效的GD图像资源是否可以找出原始图像的类型?
例如:
$image = ImageCreateFromPNG('http://sstatic.net/so/img/logo.png');
Run Code Online (Sandbox Code Playgroud)
我可以获得仅具有$ image变量的原始图像类型(PNG)吗?
我不确定它是否可以从$ image变量完成,但是为了获得MimeType,你通常可以使用以下四种中的任何一种:
// with GD
$img = getimagesize($path);
return $img['mime'];
// with FileInfo
$fi = new finfo(FILEINFO_MIME);
return $fi->file($path);
// with Exif (returns image constant value)
return exif_imagetype($path)
// deprecated
return mime_content_type($path);
Run Code Online (Sandbox Code Playgroud)
从您的问题描述中我想要使用远程文件,因此您可以执行以下操作来使其工作:
$tmpfname = tempnam("/tmp", "IMG_"); // use any path writable for you
$imageCopy = file_get_contents('http://www.example.com/image.png');
file_put_contents($tmpfname, $imageCopy);
$mimetype = // call any of the above functions on $tmpfname;
unlink($tmpfname);
Run Code Online (Sandbox Code Playgroud)
注意:如果您将使用的MimeType函数支持远程文件,请直接使用它,而不是先创建文件的副本
如果你需要MimeType来确定imagecreatefrom使用哪个函数,为什么不首先将文件作为字符串加载然后让GD决定,例如
// returns GD image resource of false
$imageString = file_get_contents('http://www.example.com/image.png');
if($imageString !== FALSE) {
$image = imagecreatefromstring($imageString);
}
Run Code Online (Sandbox Code Playgroud)