Discussions: 3,781 | Messages: 17,530 | Members: 5,533 | Online: 8 | Newest : RazzStar (Welcome!)

Go Back   Qdoos Webmaster Forum Marketplace > Design Forum


Reply
 
LinkBack Tools Display
Old 08-21-2008, 01:13 PM   #1 (permalink)
Im new :)
Feedback Score: 0 reviews
 
Join Date: Jul 2008
Posts: 28
Rep Power: 1 sujal is on a distinguished road
iTrader: (0)
Recent Blog:
sujal has no status.

Default Resizing an image in PHP and maintaining its aspect ratio

Hi all some tutorials are here..


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:
Quote:
/**
* 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  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply

Bookmarks

Tools
Display

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Designed by VB Skin Studio - Sitemap

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83