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
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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.