0

When I use a php script using imagick basics in the same folder where the image is and using the filename the output is perfect, it outputs the image of the PDF's 1st page:

// Read page 1 
$im = new imagick( 'Miami_Guide.pdf[0]' );
// Convert to png 
$im->setImageFormat( "png" );
// Send out
header( "Content-Type: image/png" );
echo $im;

Here is the challenge: how do I move this script to a different directory AND using a fieldname called $uploaded from a database record instead? I tried this but it didn't work:

$path: '../../otherplace/';
// $uploaded is the field with the filename value
$fileloc: $path . $uploaded;
// Read page 1 
$im = new imagick( '$fileloc[0]' );
// Convert to png 
$im->setImageFormat( "png" );
// Send out 
header( "Content-Type: image/png" );
echo $im;

I get a "Fatal error: Uncaught exception 'ImagickException' with message 'Unable to read the file: $fileloc' in /home/user/public_html/dir/folder/script.php:334 Stack trace: #0 /home/user/public_html/dir/folder/script.php(334): Imagick->__construct('$fileloc') #1 /home/user/public_html/dir/folder/script.php(226): show_record(Array) #2 {main} thrown in /home/user/public_html/dir/folder/script.php on line 334

Also tried pathinfo() and fatal error again

I made a few tweaks but now Imagick is returning gibberish instead of image:

$sourcePath = 'dir/folder/'; // Path of original image
$sourceUrl = 'http://www.site.com/';
$thumbPath = $sourcePath; // Writeable thumb path
$thumbUrl = $sourceUrl . $thumbPath . $uploaded ; 
$im = new Imagick($thumbUrl);
$im->setImageFormat( "jpg" );
$im->thumbnailImage(100, 100);
header("Content-type: image/jpg");
echo "$im";

"No error" but outputs gibberish instead of image:

ÿÛCÿÀdd"ÿÄÿÄ9 !1"#A2QVh‘•¦Ôä$Waq±ÓÿÄÿÄ)!1Qq"A‘Òar’¡áÿÚ?Íkßµ+¾;é=oÕÜŽW#ü–ÙænÝ»ë‘çOo´OÒÝr2_^¬ÇiÓFãaR›5$ò~\’0#Ïn¢—·©ÕuµðGMXÍ«ùÛæÿÃ'g§lfÍ,Ø”Sú*¥Áw“h‡/qk,=Ýržzï–>~ÂÒút ƒÔ«á3žŽÃ‹¶s8yd“4–ݤ®§‚ÇqW¼ºÎ£‹Áß~.—´¤ŸÅKn@ «DE ±gÄ©"hm&㥰öÔ«b¬..........

Any help?

cchap
  • 371
  • 1
  • 3
  • 6

2 Answers2

0

The Reason your first example doesn't work is because you have your variable inside single quotes, not double quotes.

This:

$im = new imagick( '$fileloc[0]' );

Tells imagemagick to look for a file named $fileloc, which of course does not exist.

do this instead:

$im = new imagick( "$fileloc[0]" );

As for your second example, I belive you are having problems because for some reason you've put $im inside quotes when you echo it. You should echo it the same way you were in your working test:

echo $im;

not

echo "$im";
Gordon Bailey
  • 3,881
  • 20
  • 28
0

Here is the solution:

instead of:

echo $img;

you should echo:

echo "<img src='data:image/jpg;base64,".base64_encode($img)."' />";

This solved my Imagick problem, I hope it will help others.

One thing that I made sure was to test the basics: test.pdf > test.jpg, also tested: test.pdf > test.png, test.pdf > test.tiff - once I was sure that all the extensions were in the delegates and working fine, I moved to different dir and pulling the file from someplace else.

Brief of all the steps:

1- installed imagick (php) - I believe it was devel

2- set the path to the file thru http://

3- the rest was pretty much Imagick basics:

 $pdfUrl = "http://www.site.com/dir/$file";
 $img = new Imagick($pdfUrl);
 $img->setImageFormat("jpeg");
 echo "<img src='data:image/jpg;base64,".base64_encode($img)."' />";
cchap
  • 371
  • 1
  • 3
  • 6
  • This isn't accomplishing what you originally set out to do, which is to send an image. Here you are sending an html snippet with an image embedded in it, which is not the same thing at all. – Gordon Bailey Aug 08 '12 at 19:54
  • I know but since after 3 days trying to find a solution somewhat similar, this solved my needs - echo $im; or echo "$im"; - both were resulting in gibberish and header("Content-type: image/jpg"); was resolving with a delegate error – cchap Aug 08 '12 at 20:06