用于 PHP 的 BMP 图像代码


BMP image code for PHP

我有一个系统,您可以从文件夹中上传图像,压缩后它会自动出现在某个页面上。它适用于PNG,GIF和JPG,但我一直在尝试添加BMP支持,但它给了我错误。我找不到许多其他使用 BNP 代码的替代方案,有什么建议吗?

这是重要部分的样子:

     $dsg_allowed_extensions = array();
    if ( ! isset($dsg_allow_gif) || $dsg_allow_gif == TRUE ) {
      array_push($dsg_allowed_extensions, 'gif');
    }
    if ( ! isset($dsg_allow_bmp) || $dsg_allow_bmp == TRUE ) {
      array_push($dsg_allowed_extensions, 'bmp');
    }
    if ( ! isset($dsg_allow_jpg) || $dsg_allow_jpg == TRUE ) {
      array_push($dsg_allowed_extensions, 'jpg');
      array_push($dsg_allowed_extensions, 'jpeg');
    }
    if ( ! isset($dsg_allow_png) || $dsg_allow_png == TRUE ) {
      array_push($dsg_allowed_extensions, 'png');
    }

    function dsgLoadImage($image) {
      switch ($image['type']) {
        case IMAGETYPE_JPEG: return imagecreatefromjpeg( $image['fullpath'] );
        case IMAGETYPE_GIF:  return imagecreatefromgif( $image['fullpath'] );
        case IMAGETYPE_BMP:  return imagecreatefrombmp( $image['fullpath'] );
        case IMAGETYPE_PNG:  return imagecreatefrompng( $image['fullpath'] );
case IMAGETYPE_JPEG:
      if (! @imagejpeg($new_image, $destination_path, $compression) ) {
        throw new Exception ("Writing to file failed.");
      }
      break;
    case IMAGETYPE_GIF:
        if (! @imagegif($new_image, $destination_path) ) {
          throw new Exception ("Writing to file failed.");
        }
    break;
    case IMAGETYPE_PNG:
      if (! @imagepng($new_image, $destination_path) ) {
        throw new Exception ("Writing to file failed.");
      }
    break;
    case IMAGETYPE_BMP:
      if (! @imagebmp($new_image, $destination_path) ) {
        throw new Exception ("Writing to file failed.");

据我所知,函数imagecreatefrombmp()在PHP中不存在,因为GD2库不支持BMP格式的文件。有一个 imagecreatefromwbmp()(注意"w")适用于 WBMP 文件;不是你需要的。来自 Bytes 社区的某个人为缺失的函数编写了一个天真的替代品(并未涵盖所有情况)。见他的帖子。

更新

函数imagecreatefrombmp()从 PHP 7 开始可用