Log in

View Full Version : Image Resizing



GhettoT
12-14-2006, 07:48 PM
How do I make an image resize to a certain size (in pixels or size %) using PHP?
I found a script on the PHP website that looks like this:


<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>
But how do I make it so that I put all that into a single PHP file then just call to it(<?php include() ?>) in an individual PHP page file?

GhettoT
12-15-2006, 03:59 AM
BUMP. Any help would be appreciated. (I can't even get that script on its own to work...)

djr33
12-15-2006, 06:05 AM
You wouldn't use an include statement for this.

I'd recommend, for example, assuming you want a thumbnail for images (like I have done), creating thumbnail.php and using a GET variable to set the image.

Replace:
$filename = 'test.jpg';
With:
$filename = $_GET['img'];

And use:
...thumbnail.php?img=filename.jpg

And that can be changed each time.

Then, just use normal image tags to include that as an IMAGE.

Remember, a php file outputting an image is just like a .jpg/.gif/.png and should be treated as such. Including it into another php file would just screw things up, with image data and headers being output within html data and headers. a php file outputting a valid image can be used within an <img> tag, viewed in a browser as an image, used as a background, etc etc.