Forum Moderators: coopster

Message Too Old, No Replies

how do i change the size of an image to an maximum dimension in php

how do i change the size of an image to an maximum dimension in php

         

MrGecko

4:50 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



I'm making a photo album in php
and i'm having a problem with the image size it stretches the image
can you tell me why

here's the script i have made
list($width, $height) = getimagesize(./image/myimage.jpg);
while($width >= $maxw or $height >= $maxh) {
*$width -= 1;
*$height -= 1;
}

*= tab

$maxw and $maxh = 500

Mr. Gecko

Moosetick

5:00 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



if both are over 500 you end up having a 500x500 image that looks distorted.

You need to determine both, then resize. I haven't tested it but something like below should do what you need.

list($width, $height) = getimagesize(./image/myimage.jpg);
$max = 500;

if ($width>500) {
$temp = $width;
$width = $max;
$height = ($max/ $height) * $height ;
}

if ($height >$max) {
$temp = $height ;
$height = $max;
$width= ($max/ $height) * $width;
}

Moosetick

5:06 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



One more thing...

Most programmers frown on the use of "while" statements due to the fact that they create neverending loops that never meet the required conditions to exit. Also, the code you provided can use a lot of processor cycles if you start with a 3000x3000 image since it will have to countdown to 500. That isn't a big deal for 1 image, but may be if you get a busy site.

eeek

8:05 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Most programmers frown on the use of "while" statements

Since when?

eelixduppy

3:17 am on Jun 22, 2006 (gmt 0)



Generally speaking, any loop has the potential to become infinite if programmed incorrectly (or correctly for that matter;))

mcavic

3:27 am on Jun 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not so much that while loops are frowned upon. If it's coded correctly, even a while(1) loop is safe. But the loop in the first post is unnecessary and wasteful. Instead of decreasing the size one pixel at a time, it can calculate the correct size immediately.

MrGecko

6:51 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I would like to make a thumbnail of a
image that is 1659 x 1589 px

Vali

7:52 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



Hey

Check the php manual
[us3.php.net...]

They have examples there.

Vali

MrGecko

8:42 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I got it to work with this script

list($width, $height) = getimagesize($row[5]);
while($width >= 500 or $height >= 500)
{
*$width /= 1.2;
*$height /= 1.2;
}
echo "<a href=\"$PHP_SELF?do=display&img=$row[5]\" TARGET=\"_blank\">
<img src=\"$row[5]\" width=$width height=$height></a>";

* = tab

Mr. Gecko

eeek

6:30 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't really need a loop for this. Just calculate a scaling factor ($sf in this example) that brings the image down to your maximum size:

$x=imagesx($img);
$y=imagesy($img);
if ($x>200 ¦¦ $y>200) {
$sf=200/$x;
if ($y*$sf > 200)
$sf=200/$y;
}

RogueDogg

6:48 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



I wish I would have paid attention in math class more...that is some confusing stuff

[edited by: coopster at 1:31 am (utc) on June 28, 2006]
[edit reason] language [/edit]

eeek

7:25 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why is it confusing. I'm simply caculating the ratio needed to bring the image down to 200 pixels. The only complication is in picking which axis to base the ration on.

eeek

7:26 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Of course having the forum software destroy the indenting doesn't help ;)