473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Jul 17 '05 #1
4 2842
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
["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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Abs | last post by:
Hi! I need to put an image inside a box, centering and resizing it to fit the box. The box is positioned and sized in the page using percent values. The problem is that I don't know how can I do...
4
by: no-spam | last post by:
Hello, I have an HTML question that I'm not sure can be solved. I want to restrict the maximum size of an inline image. For example, I can force the image to be 200x200 if I do this: <img...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
5
by: Arthur Hsu | last post by:
Hello, I have an ImageButton that refers to an external image. How can I keep that image's aspect ratio when I set the ImageButton's size to 120x120? TIA, Arthur
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
2
by: Tina | last post by:
I have a System.Drawing.Bitmap myImage; I also have a webcontrols.Image on my web page. I want to put the bitmap into the image but the image can only get it's bitmap from ImageURL which can be a...
0
by: Saaima | last post by:
Hi All when I store a picture of size 800*600 pixels in database (SqlServer) field Photo (Image) and load it into crystal report through query by dragging and dropping the field on report it...
3
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.