It appears you have not yet registered with our community. To register please click here...


Resizing an image in PHP and maintaining its aspect ratio

This is a discussion on Resizing an image in PHP and maintaining its aspect ratio within the HTML & CSS forums. Topic: Hi All Resizing an image in PHP and maintaining its aspect ratio ________________________________________ A question that has been asked many ...


Reply
 
Postbit Seperator LinkBack Thread Tools Postbit Seperator Search this Thread Postbit Seperator Display Modes
Old 08-23-2008, 08:16 AM   #1 (permalink)
 
Status: Member
Join Date: Jul 2008
Posts: 40
Trader Rating: (0)
sujal is on a distinguished road
TD$: 225.00
Donate
Default Resizing an image in PHP and maintaining its aspect ratio
Hi All

Resizing an image in PHP and maintaining its aspect ratio
________________________________________
A question that has been asked many times is how to resize an image in PHP while maintaining the aspect ration of the original image.

The function below does just that, it takes an image resource and the new dimensions for the image in pixels and returns the resized image.
Code:
/**
* Resize and image while maintaining its aspect ratio.
*
* @param resource $src The image to resize.
* @param int $w The target width.
* @param int $h The target height.
* @return resource The resized image or the original image if it did not need to be scaled.
*/
function resizeImage($src, $w, $h) {

// Get the current size
$width = ImageSx($src);
$height = ImageSy($src);

// If one dimension is right then nothing to do
if($width == $w || $height == $h)
return($src);

// Calculate new size
if(($w - $width) > ($h - $height)) { // use height
$s = $h / $height;
$nw = round($width * $s);
$nh = round($height * $s);
}
else { // Use width
$s = $w / $width;
$nw = round($width * $s);
$nh = round($height * $s);
}

// Resize to correct size
$im = ImageCreateTrueColor($nw, $nh);
ImageCopyResampled($im, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);

// Return the new image
return($im);
}
A simple example to load, resize and send an image to the browser would be:
Code:
$image = ImageCreateFromJPEG('images.jpg');
$image = resizeImage($image, 400, 400);
header("Content-Type: image/jpeg");
ImageJPEG($image, '', 80);


thanks
sujal is offline   Reply With Quote
Old 09-09-2008, 07:10 AM   #2 (permalink)
 
Status: Junior Member
Join Date: Aug 2008
Posts: 6
Trader Rating: (0)
kam33 is on a distinguished road
TD$: 45.00
Donate
Default Re: Resizing an image in PHP and maintaining its aspect ratio
The function below does just that, it takes an image resource and the new dimensions for the image in pixels and returns the resized image.
kam33 is offline   Reply With Quote
Table Bottom LeftTable BottomTable Bottom Right
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




Home | Site Map | Contact | Archive | Top

 

Copyright © 2007 - 2010 iTalkWebs Network

Search Engine Optimization by vBSEO 3.1.0