Problems clipping & displaying an image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kenoli
Forum Newbie
Posts: 8
Joined: Tue Oct 24, 2006 10:15 am

Problems clipping & displaying an image

Post by kenoli »

I am trying to upload an image, extract a clip of a particular size and display that clip. 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 it includes the use of header() which crashes the code since it is not the first thing sent to the page. 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.

My code is below. The commented out echo prior to calling the function crashes the action 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.

I'm sure there is something obvious here, but I am not seeing it.

Thanks,

--Kenoli

Code: Select all

<?php

function clip_image($filename) {

// Get width & height of image
list($current_width, $current_height) = getimagesize($filename);

//echo "Width = $current_width, Height = $current_height";

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

//echo "<br/><br/>Left origin = $origin_left";
//echo "<br/><br/>Top origin = $origin_top";

// Create the crop template image and place image in a function
$src = imagecreatefromjpeg($filename);
$dest = imagecreatetruecolor(150, 150);

// Clip the image
imagecopy($dest, $src, 0, 0, $origin_left, $origin_top, $current_width, $current_height);

// Output image
header('Content-Type: image/jpeg');
$dest = imagejpeg($dest, $stuff);

// Clear memory
imagedestroy($dest);
imagedestroy($src);

}

//echo 'This is the beginning';  // This line if uncommented prevents the display of the image

$filename = 'images/arrow.jpg';

$stuff = clip_image($filename);

echo "<img src = \"$stuff\" />";

?>

Post Reply