By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Image resizing with aspect ration maintained....

P: n/a
I have a section(185pixelsx 185pixels) in my web page to display an image
that is stored in a directory. Using php, how do you resize so if:

the image dimension is smaller(width and height is less than 185pixels),
display it in the center of the section

if the width OR height is larger than 185pixel, resize them with aspect
ratio maintained so both width and height is <= 185pixels. Then display it
in the center of the section.

Thanks

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ruby Tuesday wrote:
I have a section(185pixelsx 185pixels) in my web page to display an image
that is stored in a directory. Using php, how do you resize so if:

the image dimension is smaller(width and height is less than 185pixels),
display it in the center of the section

if the width OR height is larger than 185pixel, resize them with aspect
ratio maintained so both width and height is <= 185pixels. Then display it
in the center of the section.


Hi,

I just wrote a function doing this yesterday.
--> http://www.hightech-board.de/thread....8101#post18101

(Sorry, the explanations are in german, but you'll understand the php
code, I think)

It resizes jpgs and pngs, if GD2 is installed and gifs, too, if there
is gif reading support.

Greetz
Paul.

P.S.: And here the code without Highlighting... ;-)

================================================== ========
<?php
function resizeImg($imgPath, $maxWidth = 100, $maxHeight = 100,
$directOutput = false, $quality = 90, $verbose = false)
{
// get image size infos (0 width and 1 height,
// 2 is (1 = GIF, 2 = JPG, 3 = PNG)
$size = getimagesize($imgPath);

// break and return false if failed to read image infos
if(!$size){
if($verbose && !$directOutput)echo "<br />Not able to read
image infos.<br />";
return false;
}

// relation: width/height
$relation = $size[0]/$size[1];
// maximal size (if parameter == false, no resizing will be made)
$maxSize = array(
$maxWidth?$maxWidth:$size[0],
$maxHeight?$maxWidth:$size[1]
);
// declaring array for new size (initial value = original size)
$newSize = $size;
// width/height relation
$relation = array($size[1]/$size[0], $size[0]/$size[1]);

// get new dimension, if necessary
$i = 0;
while((($newSize[0] > $maxSize[0]) || ($newSize[1] >
$maxSize[1])) && ($i < 2))
{
$newSize[$i] = intval(min($newSize[$i], $maxSize[$i]));
$newSize[(($i+1)%2)] = intval($relation[$i]*$newSize[$i]);
$i++;
}
// need to resize?
if(!$i){
if($verbose && !$directOutput)echo "<br />No need to resize.<br
/>";
if(!$directOutput)return true;
}

// create image
switch($size[2])
{
case 1:
if(function_exists("imagecreatefromgif"))
{
$originalImage = imagecreatefromgif($imgPath);
}else{
if($verbose && !$directOutput)echo "<br />No GIF support
in this php installation, sorry.<br />";
return false;
}
break;
case 2: $originalImage = imagecreatefromjpeg($imgPath); break;
case 3: $originalImage = imagecreatefrompng($imgPath); break;
default:
if($verbose && !$directOutput)echo "<br />No valid image
type.<br />";
return false;
}

// create new image
$resizedImage = imagecreatetruecolor($newSize[0], $newSize[1]);

imagecopyresampled(
$resizedImage, $originalImage,
0, 0, 0, 0,
$newSize[0], $newSize[1], $size[0], $size[1]);

// output or save
if($directOutput){
imagejpeg($resizedImage);
}else{
imagejpeg($resizedImage,
preg_replace("/\.([a-zA-Z]{3,4})$/",".jpg",$imgPath), $quality);
}

// return true if successfull
return true;
}
?>
================================================== ============
Jul 17 '05 #2

P: n/a
["Followup-To:" header set to comp.lang.php.]
Ruby Tuesday wrote:
I have a section(185pixelsx 185pixels) in my web page to display an image
that is stored in a directory. Using php, how do you resize so if:

the image dimension is smaller(width and height is less than 185pixels),
display it in the center of the section

if the width OR height is larger than 185pixel, resize them with aspect
ratio maintained so both width and height is <= 185pixels. Then display it
in the center of the section.


<?php
define('AVAILABLE_WIDTH', 185);
define('AVALIABLE_WIDTH', 185);

// Get image size: http://www.php.net/getimagesize
// into $width and $height

$reducex = AVAILABLE_WIDTH / $width;
$reducey = AVAILABLE_HEIGHT / $height;

$reduce = min($reducex, $reducey);
if ($reduce < 1) {
// resize: http://www.php.net/imagecopyresized
// or resample: http://www.php.net/imagecopyresampled
// with $reduce paying a important role in the process
}

// output the (possibly resized) image
?>
Sorry for the pseudo and incomplete code -- but I think I can make it
clearer with code than with words.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3

P: n/a
Pedro Graca wrote:
<?php
define('AVAILABLE_WIDTH', 185);
define('AVALIABLE_WIDTH', 185); (snip) Sorry for the pseudo and incomplete code

and wrong definition

I'm sure you can figure out what I meant :)

/me is being sloppy -- damn, damn, damn
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

P: n/a
this should help you:
http://www.gzentools.com/gzimg.php

is a free little lib of functions that will easily do what you want.
as far as cenetering top to bottom, is puts thumb on top, so when images are
in a row, thier tops line up.
the function is gzImagePad(). but you can modify it to center middle if you
want.
--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Ruby Tuesday" <ru*********@yahoo.com> wrote in message
news:c1*************@ID-205437.news.uni-berlin.de...
I have a section(185pixelsx 185pixels) in my web page to display an image
that is stored in a directory. Using php, how do you resize so if:

the image dimension is smaller(width and height is less than 185pixels),
display it in the center of the section

if the width OR height is larger than 185pixel, resize them with aspect
ratio maintained so both width and height is <= 185pixels. Then display it
in the center of the section.

Thanks

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.