473,663 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image resizing with aspect ration maintained....

I have a section(185pixe lsx 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 2856
Ruby Tuesday wrote:
I have a section(185pixe lsx 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($imgP ath, $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($i mgPath);

// 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?$maxW idth:$size[0],
$maxHeight?$max Width:$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((($newSiz e[0] > $maxSize[0]) || ($newSize[1] >
$maxSize[1])) && ($i < 2))
{
$newSize[$i] = intval(min($new Size[$i], $maxSize[$i]));
$newSize[(($i+1)%2)] = intval($relatio n[$i]*$newSize[$i]);
$i++;
}
// need to resize?
if(!$i){
if($verbose && !$directOutput) echo "<br />No need to resize.<br
/>";
if(!$directOutp ut)return true;
}

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

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

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

// output or save
if($directOutpu t){
imagejpeg($resi zedImage);
}else{
imagejpeg($resi zedImage,
preg_replace("/\.([a-zA-Z]{3,4})$/",".jpg",$imgPa th), $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(185pixe lsx 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('AVAILAB LE_WIDTH', 185);
define('AVALIAB LE_WIDTH', 185);

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

$reducex = AVAILABLE_WIDTH / $width;
$reducey = AVAILABLE_HEIGH T / $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('AVAILAB LE_WIDTH', 185);
define('AVALIAB LE_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*********@ya hoo.com> wrote in message
news:c1******** *****@ID-205437.news.uni-berlin.de...
I have a section(185pixe lsx 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
3724
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 it exactly. This is the style code for the box: ..box { top: 20%;
4
16660
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 src=blah.gif width=200 height=200> But, that messes up the aspect ratio, and could size the image larger than it's original size.
10
4144
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 resolution (ie. large). I have a database that contains references to the pictures. Currently I have to resize the jpgs manually, and then point the ImageUrl property at that jpg using databinding. This works fine. I would like to avoid the resizing step...
5
3859
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
4411
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 an image can take up but not stretch the image if it happens to be 150px by 40. I am doing commands such as: <asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px" Height="50px" runat="server"/>
2
2797
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 fileName or a webpage that writes. But I don't want to have to write my bitmap to a file, I just want to put it into the image. Can this be done? It seams that an image control should be designed to that it could accept a bitmap (duh) but I...
0
2203
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 appears well but as its size is too large and when i resize it its resolution become so wrong to can not see image. such as in Employee photo in Employee record Actually i need a passport size photo on report. So i save a picture of this size and get...
3
14100
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 frame due to differnt aspect ratio. Some graphic designer suggests me to keep the aspect ratio of the original graphic and pad the graphic with empty space to fit into the frame. One example, the fixed frame is 100x60 and the image is 120x120, I...
10
7056
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 only small sized iamages.. for eg. resizing takes place for 70 kb sized images but fails for 600kb or more.. my code is below..
0
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.