I am trying to extract a clip of a particular size from an image and display that clip on a web page using php. I have some code that will do that, however, I don’t know how to use it within the context of an actual page as the only method I have found for displaying the image includes the use of the header() function. Since I am inserting the images in the body of the page, other content is sent before the header() function called in the clip_image() function and the image in is not displayed.
My code is below. The commented out echo prior to calling the function prevents the display of the image if I uncomment it. Since I will be inserting a number of clipped images onto the page, there will, of necessity be content sent prior to calling the function that includes the header() function. If I do away with the header() function, I just get a bunch of BLOB data displayed.
I’m sure there is something obvious here, but I am not seeing it.
Thanks,
–Kenoli
<?php
function clip_image($filename) {
// Get width & height of image
list($current_width, $current_height) = getimagesize($filename);
// Set final crop size
$crop_width = 150; $crop_height = 150;
// Calculate origin of crop
$origin_left = ($current_width - $crop_width)/2;
$origin_top = ($current_height - $crop_height)/2;
// Create the crop template image and extract the image into a variable
$src = imagecreatefromjpeg($filename);
$dest = imagecreatetruecolor(150, 150);
// Clip the image starting at origin given
imagecopy($dest, $src, 0, 0, $origin_left, $origin_top, $current_width, $current_height);
// Output image
header('Content-Type: image/jpeg');
$dest = imagejpeg($dest, $stuff);
// What else can I do here to output the image???
// Clear memory
imagedestroy($dest);
imagedestroy($src);
}
//echo 'This is some content sent before the header function'; // This line if uncommented prevents the display of the image
$filename = 'images/arrow.jpg';
$stuff = clip_image($filename);
echo "<img src = \\"$stuff\\" />";
?>