1

I could not find a way to convert TIFF byte array into JPEG byte array in PHP. I tried the following:

$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.....");

but it returns Data is not recognized format.

So how do I convert TIFF bytes into JPEG bytes?

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Software Engineer
  • 290
  • 1
  • 3
  • 14

1 Answers1

2

The GD extension currently does not support loading TIFF images. You can use Imagick extension instead:

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);
}

In the code above, $tiff_bytes is a binary string of a TIFF image.

Alternatively, you can install the official command line tools, save the TIFF image to filesystem, and convert it to JPEG using the following command:

convert file.jpg file.tiff 

There is a number of ways to execute a shell command in PHP. I prefer exec() for the cases when I do not need much of control over the execution, and proc_open() when I need full control over the contents of the file descriptors, i.e. in most cases.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • I try to use above code but i am getting error:- Fatal error: Class 'Imagick' not found. i have also registered php_imagick.dll file in php.ini – Software Engineer Jan 23 '17 at 09:11
  • 1
    @SoftwareEngineer, the extension is not loaded for some reason. Have a look at the output of `phpinfo();`, if you are running a Web SAPI, or at the output of `php -m` (list of loaded modules) in CLI. Have you [installed](http://php.net/manual/en/imagick.installation.php) the PECL extension? Is the library present in the extensions directory (`extension_dir` in _php.ini_)? – Ruslan Osmanov Jan 23 '17 at 09:12
  • 1
    You need to instal Imagick module if it isn't already installed. Once installed, restart PHP and it'll exist :) – Martin Jan 23 '17 at 09:40
  • @SoftwareEngineer, https://pecl.php.net/package/imagick is the official page. If you are on Windows, download the DLL into the `extension_dir`, restart the Web server, and check the output of ` – Ruslan Osmanov Jan 23 '17 at 10:15
  • i try to download and put on "C:\wamp64\bin\php\php5.6.25\ext" directory and add php.ini file extension_dir but i got soapcall error. i also use soapcall function – Software Engineer Jan 23 '17 at 10:43
  • @SoftwareEngineer, there are many posts on this issue on SO, examples: [1](http://stackoverflow.com/questions/33336327/how-to-install-imagemagick-for-wamp-2-5), [2](http://stackoverflow.com/questions/33336327/how-to-install-imagemagick-for-wamp-2-5), [3](http://stackoverflow.com/questions/2942523/step-by-step-instructions-for-installing-imagemagick-on-wamp?rq=1). By the way, Windows is not very good OS for PHP, IMO, and I doubt that a single foolproof solution even exists for this OS. – Ruslan Osmanov Jan 23 '17 at 11:34
  • @RuslanOsmanov:- I try above code i got error "NoDecodeDelegateForThisImageFormat `' @ error\/blob.c\/BlobToImage\/355" – Software Engineer Jan 24 '17 at 05:54
  • @SoftwareEngineer, could you upload the blob data somewhere and give me the link? You can save the blob data as `file_put_contents('blob.data', $blob);`. The simplest way to run the shell command is `exec("convert input.tiff output.jpg");`. Note, `convert` (or maybe `convert.exe` on Windows) executable must be available in the PATH environment variable. Otherwise, you need to provide absolute path to it, e.g. `exec("/usr/bin/convert in.tiff out.jpg");` on Unix-like OS, and something like `exec("C:\\Program Files\\Path-to-Imagick\\convert.exe in.tiff out.jpg");`. – Ruslan Osmanov Jan 24 '17 at 07:19
  • @SoftwareEngineer, it is slightly off-topic, but I recommend you to work on a Unix-like system, as Windows is not appropriate OS for Web development and even for software development unless the task is Windows-specific, IMO. – Ruslan Osmanov Jan 24 '17 at 07:20