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?

7
  • How do you get the "byte array"? Commented Jan 21, 2017 at 5:30
  • i am using call wsdl SoapClient() method. Commented Jan 21, 2017 at 5:50
  • Is it a hex-encoded sequence of space-separated bytes? I wonder what the characters II, M, JeR mean. Commented Jan 21, 2017 at 5:55
  • i try to put binary data but it's not possible. so just it example Commented Jan 21, 2017 at 5:58
  • @Ruslan is it possible to tiff binary image value store as jpeg image in local folder? pls help sir Commented Jan 21, 2017 at 6:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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
@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 the PECL extension? Is the library present in the extensions directory (extension_dir in php.ini)?
You need to instal Imagick module if it isn't already installed. Once installed, restart PHP and it'll exist :)
@SoftwareEngineer, 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 <?php phpinfo();. You may find this page useful
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.