Jump to content

imagick with php: upset by other code


darwin_tech

Recommended Posts

Hi,

 

I am using imageMagick extension with php. The problem is that I can get it to run fine as a stand alone script:

 

<?php 
$im = new imagick('test.pdf[0]');
$im->setImageFormat( "jpg" );
header( "Content-Type: image/jpeg" );
$im->thumbnailImage(400, 0);
echo $im;
?>

 

however as soon as I introduce any more php/ html code or call the script from within code it fails with no error message. For example I would like to use it like this:

 

# loop through results and output in a table
        while ($stmt->fetch())
        {				
          $im = new imagick($file[0]);
          $im->setImageFormat( "jpg" );
          header( "Content-Type: image/jpeg" );
          $im->thumbnailImage(400, 0);
          echo $im;     		
        }

 

Any clues as to why this won't work?

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/235101-imagick-with-php-upset-by-other-code/
Share on other sites

This line:

 

header( "Content-Type: image/jpeg" );

Is setting the header to indicate a jpeg image. You have a few problems with the appended script that you showed us - firstly you're setting the same header multiple times. That's just bad.

 

Secondly, you're setting the header of the PHP script (+ output) itself, which means that any web browser will interpret your page as an image. What you should be doing is referencing your standalone script using a basic image tag. Example:

 

<img src="standalone.php?file=<?php echo $file[0]; ?>" />

 

And then in the standalone.php:

 

<?php 
$file = $_GET['file']; // You'll want to sanitize this, seriously. It's a huge security hole if you don't.
$im = new imagick($file);
$im->setImageFormat( "jpg" );
header( "Content-Type: image/jpeg" );
$im->thumbnailImage(400, 0);
echo $im;
?>

Whoa!

 

Thanks alot mattal999!

 

It would have taken me a while to figure out what I was doing wrong there. I didn't realise you had to call the standalone script in the img tag.

 

You saved me a heap of time, and judging by some of the threads I looked at in relation to this problem, this will be useful to other people too.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.