在PHP中将TIFF字节数组转换为JPEG字节数组

时间:2017-01-21 04:21:00

标签: php jpeg gd tiff

我找不到在PHP中将TIFF字节数组转换为JPEG字节数组的方法。我尝试了以下方法:

$im = imagecreatefromstring("49 49 2a 00 16 1d 00 00 80 3f e0 4f f0 04 16 0d II.........O....07 84 42 61 50 b8 64 36 1d 0f 88 44 62 51 38 a4 ..BaP.d6...DbQ8.56 2d 17 8c 46 63 51 b8 e4 76 3d 1f 90 48 64 52 V...FcQ..v...HdR
39 24 96 4d 27 94 4a 65 52 b9 64 b6 5d 2f 90 c0 9..M..JeR.d.....");

但它会返回Data is not recognized format

那么如何将TIFF字节转换为JPEG字节?

1 个答案:

答案 0 :(得分:2)

GD扩展目前不支持加载TIFF图像。您可以改为使用Imagick扩展程序:

try {
  $im = new Imagick();
  $im->readImageBlob($tiff_bytes);
  $im->setFormat('JPEG');
  file_put_contents('test.jpeg', $im->getImageBlob());
} catch (Exception $e) {
  trigger_error($e->getMessage(), E_USER_ERROR);
}

在上面的代码中,$tiff_bytes是TIFF图像的二进制字符串。

或者,您可以安装official command line tools,将TIFF图像保存到文件系统,然后使用以下命令将其转换为JPEG:

convert file.jpg file.tiff 

有许多方法可以在PHP中执行shell命令。我更喜欢exec()用于我不需要太多控制执行的情况,而proc_open()则需要完全控制文件描述符的内容,即在大多数情况下。

相关问题