PHP 中的 imagecreatefromjpeg()/png/gif/etc 中的所有文件类型是否都有 shorthand?

Is there a shorthand for all the filetypes in imagecreatefromjpeg()/png/gif/etc in PHP?

在使用 PHP 的 imagecreatefromXXX() 方法时,我总是写出像这样烦人的长结构(此处对换行符进行了优化以提高可读性),其中基本上每一行和每个函数都使用完全相同的。看起来像是可以优化的场景:

switch ($mimeType) {
    case 'image/jpeg': $myImage = imagecreatefromjpeg($source_image); break;
    case 'image/png': $myImage = imagecreatefrompng($source_image); break;
    case 'image/gif': $myImage = imagecreatefromgif($source_image); break;
    // ... and so on, for every possible filetype
    }

问题:

PHP 中是否有原生函数/shorthand 接管这个恼人的长检查每个文件类型开关?似乎这是一种常见的情况,但没有更简单的方法来做到这一点...

差不多。不是来自 mime 类型,而是来自流本身:

imagecreatefromstring(file_get_contents($source_image))

imagecreatefromstring() returns an image identifier representing the image obtained from the given image. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.

(manual)