Wednesday, January 12, 2011

Image resize without distortion and keeping proportions

Image resize without distortion and keeping proportions.



Image distortion is the normal problem with the image resize in
any programming language. You need to have good knowledge of math to
resizing the image.
I have implemented image resize code without distortion in PHP.
You can easily convert it in the any other programming language
for ex: ASP.Net, JSP etc:

Here is the listed below code:
====================================

img_resize.php

$filename = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQOT3CzejzFpMcWAqDQekKgdvDT8f4Ot1gFRIG3UDWN8N8y7kIMAagI85rho803ugEzu5IgQDK9k8FgDxsjKe8xgJ8zpaW8JaejckiKU3tnhYy48q0m2eavUhP9yn9x8cU-t7e4YPepPg/s1600/auto-resume-broken-uploader.jpg";
$nw = 170; // new width
$nh = 128; // new height
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
if($width<= $nw) {
   $nw=$width;
}
$newHeight = $height*$nw/$width; // calculate new height here
if($newHeight > $nh) {
   $nw = $width*$nh/$height;
   $newHeight=$nh;
}
$image_s = imagecreatetruecolor($nw,$newHeight);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_s, $image, 0, 0, 0, 0, $nw,$newHeight,$width,$height);
imagejpeg($image_p, null, 100);   


use it as listed below:
<img src="img_resize.php" />

Note that you can also make the parameter for height and width.







No comments:

Post a Comment