Imagick

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I have been learning to use Imagick recently for resizing images already stored on the server for various size requirements on different pages.

But how do I get around the requirement of providing the header info to echo out an image after I resize it?

<?php
$im
= new Imagick("image.jpg");
$im->thumbnailImage( 200, 200, true );
$shadow = $im->clone();
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$shadow->shadowImage( 80, 1, 5, 5 );
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );

//this bit
header( "Content-Type: image/jpg" );
echo
$shadow;
?>

I get headers already sent error as this code is used after I have a few include files.
A login check is included, then the header page with the head tag and navigation links and page logo etc.
There must surely be a way to resize an image and output it without having to output the raw image data.

Surely most peoples usage of Imagick to resize images will be somewhere within their scripts where output/header info has already been sent!

Cheers

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

You are probably getting errors because you tried to send a header after sending output.

You do have to output the raw image data, and you need to have a separate script for that. Put that code in your original post in its own PHP file, and call it like it was an image from your main page:

<img src="http://www.example.com/path/to/script.php" />

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Excellent thanks, worked perfectly.

I used some url info on it as images to be displayed depends on member and member settings.

I understand the header error, I knew it was coming before I tried the script, but why does sending more header info in an include file avoid the error?
The include file called by the IMG tag is still after headers are already sent.

Cheers

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

When you are calling it in an image tag, your browser treats it as a completely separate HTTP request. So instead of thinking it as an include file, think of it as a separate request/instance altogether.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

So it sort of applies the header to the img only.

Cheers pr0gr4mm3r

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.