darwin_tech Posted April 29, 2011 Share Posted April 29, 2011 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 More sharing options...
requinix Posted April 29, 2011 Share Posted April 29, 2011 So what's the value of $file[0]? Also (a) where is the script running from and (b) where are the files located? Link to comment https://forums.phpfreaks.com/topic/235101-imagick-with-php-upset-by-other-code/#findComment-1208255 Share on other sites More sharing options...
mattal999 Posted April 29, 2011 Share Posted April 29, 2011 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 https://forums.phpfreaks.com/topic/235101-imagick-with-php-upset-by-other-code/#findComment-1208263 Share on other sites More sharing options...
darwin_tech Posted April 29, 2011 Author Share Posted April 29, 2011 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 https://forums.phpfreaks.com/topic/235101-imagick-with-php-upset-by-other-code/#findComment-1208333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.