Echoing Image using PHP problem

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
barry100

Echoing Image using PHP problem

Post by barry100 »

Hi all. I am using the following to echo an image but i think it is not the best way

Code: Select all

 $resource = NewMagickWand();
   
MagickReadImage( $resource, $showpics ); 
		
	
$resource = MagickTransformImage( $resource, '0x0', '300x300' ); 
	$width = MagickGetImageWidth( $resource );
$height = MagickGetImageHeight( $resource ); 

$newheight = ($height/2/2);
$newwidth = ($width/2/2);
MagickCropImage( $resource, 150, 120, $newwidth, $newheight ); 

		 $src_file = 'imagesresized/'.$showpics;
MagickWriteImage($resource, $src_file);
		
				 
			         
					 
				echo'<img src="imagesresized/'.$showpics.'" />'; 
					 

The problem is that i dont know how to simply echo the image

I have used

header( 'Content-Type: image/jpeg' );
MagickEchoImageBlob( $resource );

But it dosnt seem to work. Btw just to let you know I am getting the images as part of a sequence of images, resizing them and then displaying them - I dont want a load of extra images made and saved on my server though so I just want something like

MagickEchoImageBlob( $resource );

and this to simply show the image. I dont want to use

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

any help would be greaT!! ;)
hankr

Re: Echoing Image using PHP problem

Post by hankr »

You are on the right track. Use readfile() to dump the written file directly to the browser:

Code: Select all

$src_file = 'imagesresized/'.$showpics;
MagickWriteImage($resource, $src_file);
header( 'Content-Type: image/jpeg' );
readfile( $src_file );
http://www.php.net/manual/en/function.readfile.php for details and an example
Post Reply