Ask a Question related to PHP Development, Design and Development.
-
Ruby Tuesday #1
Image resizing with aspect ration maintained....
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
Ruby Tuesday Guest
-
Resizing a image!
Hello, I just started with PHP. And I have made a code for uploading images. Now I wil resize the image also to a tumbnail, but when I search... -
resizing grouped text without losing point size & aspect ratio
If I resize an individual area type object by dragging the bounding box handles, its text rewraps, but the point sizes and aspect ratios of its... -
Resizing Movies to fit res...but keeping aspect ratio.
OK...I've looked for this soultion around but I havent found it. Basically I have a page with flash (www.madisyn.com , click on "knight" image,... -
Controlling aspect ration of browser window
Does anybody know of a piece of code that will allow the visitor to resize their browser window but control the aspect ratio of the resize? -
Resizing high res image smaller results in blurred image
Hi there, I have a high res logo in PSD format (around 1500px x 1500px) but when I resize it to around 300px x 300px the resulting image is not... -
Paul 'piz' Wellner Bou #2
Re: Image resizing with aspect ration maintained....
Ruby Tuesday wrote:
Hi,> 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.
I just wrote a function doing this yesterday.
--> [url]http://www.hightech-board.de/thread.php?postid=18101#post18101[/url]
(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;
}
?>
================================================== ============
Paul 'piz' Wellner Bou Guest
-
Pedro Graca #3
Re: Image resizing with aspect ration maintained....
["Followup-To:" header set to comp.lang.php.]
Ruby Tuesday wrote:<?php> 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.
define('AVAILABLE_WIDTH', 185);
define('AVALIABLE_WIDTH', 185);
// Get image size: [url]http://www.php.net/getimagesize[/url]
// into $width and $height
$reducex = AVAILABLE_WIDTH / $width;
$reducey = AVAILABLE_HEIGHT / $height;
$reduce = min($reducex, $reducey);
if ($reduce < 1) {
// resize: [url]http://www.php.net/imagecopyresized[/url]
// or resample: [url]http://www.php.net/imagecopyresampled[/url]
// 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 =--
Pedro Graca Guest
-
CountScubula #4
Re: Image resizing with aspect ration maintained....
this should help you:
[url]http://www.gzentools.com/gzimg.php[/url]
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
[url]http://www.gzentools.com[/url] -- free online php tools
"Ruby Tuesday" <rubytuzdayz@yahoo.com> wrote in message
news:c108kj$1cel2p$1@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
>
>
>
CountScubula Guest



Reply With Quote

