Unable to load base64 encoded JPEG2000 string in ImageMagick

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Indark
Posts: 1
Joined: 2018-12-08T07:56:53-07:00
Authentication code: 1152

Unable to load base64 encoded JPEG2000 string in ImageMagick

Post by Indark »

Hello All,
I have just been introduced to ImageMagick - so please pardon my ignorance on this library.

Im trying to load the below string using ImageMagick - However no joy. Any help would be very much appreciated. Here is my sample code.

Code: Select all

$imagedata ="AAAADGpQICANCocKAAAAFGZ0eXBqcDIgAAAAAGpwMiAAAAAtanAyaAAAABZpaGRyAAAAyAAAAKAAAwcHAAAAAAAPY29scgEAAAAAABAAAAGXanAyY/9P/1EALwAAAAAAoAAAAMgAAAAAAAAAAAAAAKAAAADIAAAAAAAAAAAAAwcBAQcBAQcBAf9SAAwAAAABAQUEBAAA/1wAI0JvGG7qbupuvGcAZwBm4l9MX0xfZEgDSANIRU/ST9JPYf9kACIAAUNyZWF0ZWQgYnk6IEpKMjAwMCB2ZXJzaW9uIDQuMf+QAAoAAAAAAQMAAf9SAAwAAAABAQUEBAAA/5PPoKgT/dHUscn3uMJWDWKb153z8hPvSInB8QsdvHSg4pzoLevV6cHhwCOWrDWed1zB8RKHyC4PEhigx/MYuIx4wci8q/CEo2kiHBrV8DhszG7ymZ/UH7atm39cdbppgIDD4VYfCrB00E+GI+Qf3v1IHzVdC6k/pMRXolANASf+TQYCTKERfZoHB65rCU23EcMzjiQo+2MAmLli7aos4tyAgMOrw6tBVpk5rPA9rz1HB6Wn+siLUizMFl3TKpn7s1pJGcCba3pGnanMUNO8OP+EwaMdppACpwb6vbqSpeUbgICAgICAgID/2Q==";

// Decode image from base64
$image=base64_decode($imagedata);

// Create Imagick object
$im = new Imagick();

// Convert image into Imagick
$im->readimageblob($image);

// Create thumbnail max of 200x82
$im->thumbnailImage(200,82,true);

// Add a subtle border
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$im->borderImage($color,1,1);

// Output the image
$output = $im->getimageblob();
$outputtype = $im->getFormat();

header('Content-type: image/jpeg');

echo $output;
The error message is"The image cannot be displayed because it contains error" . The string that I have with me (base64encoded) is not something that I generated. It's a response that I get from an API call - So I'm assuming it to be valid jpeg2000 encoded string. I tried decoding it at convertstring.com/EncodeDecode/Base64Decode . I'm seeing plain text strings.. something like ... "Created by: JJ2000 version 4.1" .. So I'm guessing the string has some meta data that needs to be stripped off before it can work
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Unable to load base64 encoded JPEG2000 string in ImageMagick

Post by fmw42 »

This works for me in command line IM 6.9.10.15 Q16

# convert image to jp2000

Code: Select all

convert image.gif image.jp2
# convert image to base64 string and save in variable img64

Code: Select all

img64=$(convert image.jp2 inline:jp2:-)
echo $img64

Code: Select all

data:image/jp2;base64,AAAADGpQICANCocKAAAAFGZ0eXBqcDIgAAAAAGpwMiAAAABPanAyaAAAABZpaGRyAAAAIAAAACAABAcHAAAAAAAPY29scgEAAAAAABAAAAAiY2RlZgAEAAAAAAABAAEAAAACAAIAAAADAAMAAQAAAAAJOmpwMmP/T/9RADIAAAAAACAAAAAgAAAAAAAAAAAAAAAgAAAAIAAAAAAAAAAAAAQHAQEHAQEHAQEHAQH/UgAMAAAAAQADBAQAAf9cAA1AQEhIUEhIUEhIUP9kACUAAUNyZWF0ZWQgYnkgT3BlbkpQRUcgdmVyc2lvbiAyLjMuMP+QAAoAAAAACLYAAf+T34CgDAhR3w8oi/QUGQIprnhXYn6YF6ffgKAMCFHhhcRAay6Y+DPdi7y4HkE0H9+AmAwJcbTLeqULWLIrAF4vqJ6usV/fgJgMCab3yy3+aMURUYLWGZQHk1sh35ikfaIx+AoAIxgVPUgLcdTG5xycERzZFez5ym8X3dPjcFZr73R33bYhOUbSJxgDKVnZ3YwTOMEkEK9oqDPvJVFB35icfaIx+AmAIxgbi2YNGNBnjWG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AZZKPB2fxWQhzziG7Z1/lA9QP70ctsadw3d/YlA9Ld01uqmQFUF4P2jytouX+djf0HRcIRWes1EYT4KXAa5BUCG3h5MQMn2FFibsyeKQ7Cz0vFIibSxby/qY8QdFJ9x5iDvJtUf7My3W40M/j/ZVrsdHXI0xgUlznqQtbRfXrC17gzU9vL/qPG9/D0YHntTnLhCx5fccNqbiETjISynxCqAB3tZjZCm8vnHs/Bcn4KMfgfAHbbT/AM0JH2PsjU+TYkO4YONAwsRAzyw0g4xlDpXcTm9e18QONwVnxxVhhjY8ic09ey9VxrCCq1W8+Z8WMcXfaA9b1X7enckXKOmyeEnj2pekXJdtV8SePal6RXqCaHupUroAg7GSxutPZwwyxzz5b3Wy/S5+m2uDec80k2fIEt6Y1CnzT7PQtJCM6xvUiV50XtnXSStyexNlEoD/D9htUNpEoD/V5MahtIlAf3JgBJdoDsnuvoHhl/79wYVotzb09sE359SjIZe/KbqM1iKnMDRsMHP/zBvtvBDgv5wQE5pf9lCacmPTENuV3/2Q==
# read base 64 string from variable into inline: format to convert to base 64 data to png image

Code: Select all

convert inline:$img64 image_b64.png
I do not know Imagick that well, but see

https://imagemagick.org/Usage/files/#inline
http://us3.php.net/manual/en/imagick.newpseudoimage.php
https://imagemagick.org/script/formats.php#pseudo <-- INLINE seems to be missing here but will be fixed within about a day
Post Reply