...

View Full Version : resize image on the fly from dynamic image src



htcilt
03-08-2010, 03:53 PM
Hi,

There are lots of scripts on the web that resize an image, when the url is known e.g.
http://www.wprecipes.com/how-to-resize-images-on-the-fly

However, I need to resize an image from a blob in a table field rather than an existing file.

Is there a way to do this?
I'm using Oracle tables

Fou-Lu
03-08-2010, 06:54 PM
Same was as from a file.
Instead of loading via the imagecreatefrom*, you instead use the imagecreatefromstring (http://php.ca/manual/en/function.imagecreatefromstring.php) function. This will give you a resource that can be manipulated via the image* functions from the gd library. Continue as normally from that point.

htcilt
03-20-2010, 06:40 PM
Thanks for this.

I'm now trying to crop a cut out window of 67(w)x49(h) of an image and echo onscreen.

Are there any simple examples out there that do this?

htcilt
03-20-2010, 07:03 PM
I've found this code that does what I need. But I cant figure out how to display the cropped image on screen?

I've put the header in but get:

Warning: Cannot modify header information - headers already sent by (output started at D:\Program Files\XAmpp\xampp\htdocs\thumbnail.php:20) in D:\Program Files\XAmpp\xampp\htdocs\thumbnail.php on line 23


<?php

// Crop dimensions.
$width = 67;
$height = 49;
// Set the path to the image to resize
$input_image = "images/myimage.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Prepare canvas
$canvas = imagecreatetruecolor( $width, $height );
// Create a new image in the memory from the file
$cropped = imagecreatefromjpeg( $input_image );
// Prepare image crop - center the crop on the image
$newwidth = $size[0] / 2;
$newheight = $size[1] / 2;
$cropLeft = ( $newwidth/2 ) - ( $width/2 );
$cropHeight = ( $newheight/2 ) - ( $height/2 );
// Generate the cropped image
echo imagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, $size[0], $size[1], $newwidth, $newheight );
// Save the cropped image as cropped.jpg
//imagejpeg( $canvas, "cropped.jpg" );

header('Content-type: image/jpeg');
imagejpeg($canvas);
// Clear the memory of the tempory images

imagedestroy( $canvas );
imagedestroy( $cropped );
?>

met
03-20-2010, 07:31 PM
header declarations need to be before any output - you're echoing out the image before sending the headers.

MattF
03-20-2010, 07:57 PM
Edited: I'd totally overlooked/missed the first echo in that code.

htcilt
03-20-2010, 08:31 PM
Thanks for the replies.
I've found the problem, there was an extra echo in there I hadn't spotted. Removed that and it works fine...


<?php
// Crop dimensions.
$width = 67;
$height = 49;
// Set the path to the image to resize
$input_image = "images/news/news.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Prepare canvas
$canvas = imagecreatetruecolor( $width, $height );
// Create a new image in the memory from the file
$cropped = imagecreatefromjpeg( $input_image );
// Prepare image crop - center the crop on the image
$newwidth = $size[0] / 2;
$newheight = $size[1] / 2;
$cropLeft = ( $newwidth/2 ) - ( $width/2 );
$cropHeight = ( $newheight/2 ) - ( $height/2 );
// Generate the cropped image
imagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, $size[0], $size[1], $newwidth, $newheight );

header('Content-type: image/jpeg');
imagejpeg($canvas);
// Clear the memory of the tempory images

imagedestroy( $canvas );
imagedestroy( $cropped );
?>

However, the cropped window isn't from the center of the image, but instead the top left corner. How can I edit this to do the crop from the center?

htcilt
03-20-2010, 08:56 PM
Its okay - I've found some even better code:

<?php
function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
$myImage = imagecreatefromjpeg($imgSrc);
$ratio_orig = $width_orig/$height_orig;

if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}

$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle

$process = imagecreatetruecolor(round($new_width), round($new_height));

imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);

imagedestroy($process);
imagedestroy($myImage);
return $thumb;
}

//Create the thumbnail
$newThumb = CroppedThumbnail("images/news/portrait.jpg",67,49);

// And display the image...
header('Content-type: image/jpeg');
imagejpeg($newThumb);
?>

Taken from seifer at loveletslive dot com's post at http://php.net/manual/en/function.imagecopyresampled.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum