Page 1 of 1
PHP Image size problem
Posted: Tue Jul 20, 2010 7:25 am
by Vaughanjb
Hi Guys,
I have a database that users can upload images to. So have a set of images with random dimensions and varying size.
I want to display them at there full size but if the width is larger then 525px restrain them. (but also if smaller don't stretch them)
Is this possible?
I have tried with "auto" "100%" and even "pixels" approach but with no joy.
Code: Select all
<a href="image_prof.php?id=<? echo $rowG['id']; ?>" class="highlightit"><img border="0" src="<? echo $rowG['path'] ?>" width="100%" height="auto" /></a>
With the 100% it stretches all images to the width of the DIV 525px.
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 7:40 am
by LuaMadman
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 7:52 am
by Vaughanjb
Thanks for the reply LuaMadman,
How would I lay this out in my code?

Re: PHP Image size problem
Posted: Tue Jul 20, 2010 8:23 am
by LuaMadman
Well, I'm not to familiar with html's img tag but here is the php code I belive should do the trick
Code: Select all
<?php
$array = getimagesize('/path to file');
if ($array[0] > 525) { // width is 526 or higher
?>
HTML IMG TAG with 525 width
<?php
}
else // width is 525 or less
?>
HTML IMG TAG full size image with
<?php
}
?>
and for future info, $array[1] is height
$array[3] is height="yyy" width="xxx" that can be used directly in img tag
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 8:45 am
by Vaughanjb
Thanks LuaMadman so far you have really helped me.
I have changed my code to this. I noticed you missed a semi colon after the path. For the images smaller then 525 the code works perfectly, but for the images above that width they just go full size on my screen as if they have bypassed the first the IF statement.
Code: Select all
<? $path = $rowG['path']; ?>
<? $array = getimagesize('$path');
if ($array[0] < 525) { ?>
<a href="image_prof.php?id=<? echo $rowG['id']; ?>" class="highlightit"><img border="0" src="<? echo $rowG['path'] ?>" width="auto" height="auto" /></a>
<? } else { ?>
<a href="image_prof.php?id=<? echo $rowG['id']; ?>" class="highlightit"><img border="0" src="<? echo $rowG['path'] ?>" width="100%" height="auto" /></a>
<? } ?>
Any ideas. It all looks correct to me.
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 8:52 am
by LuaMadman
WRONG!
auto tells the html to use the size image is
width="auto" height="auto" should be width="525" height="auto" or width=525 height="auto" depending on how you choose to code...
*edit*
Thanks for noticeing the missing ;
i'll edit my post.
*edit2*
my bad.
missed that you did some changes in the code.
Above is wrong.
width="auto" height="auto" is correct
it's the 100% that should be change 100% means 100% of the webpage or the table it's in.
so change 100% to 525
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 9:02 am
by Vaughanjb
But thats what I want.
I want the images SMALLER then 525px to use their actual width. SO width="auto" height="auto"
But the images with a BIGGER width then 525px to be restrained to 525px. width="525" height="auto" or width="100%" height="auto"
But for some reason, both sizes seem to run on the first statement with the auto width. Am I missing something
Thanks again LuaMadman
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 9:04 am
by Vaughanjb
my bad.
missed that you did some changes in the code.
Above is wrong.
width="auto" height="auto" is correct
it's the 100% that should be change 100% means 100% of the webpage or the table it's in.
so change 100% to 525
I knew what you meant

, I tried both. They still dont work. they just run the first statement.
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 9:30 am
by LuaMadman
Code: Select all
<?php
$curdir = dirname(__file__);
$path = 'simpleLaptop.jpg';
$array = getimagesize($path);
if ($array[0] < 525) {
?>
<img border="0" src="<? echo $path ?>" width="auto" height="auto">
<?
} else {
?>
<img border="0" src="<? echo $path ?>" width="525" height="auto">
<?
}
?>
This is the code i tested on my computer. It works for me.
i noticed that you use getimagesize('$path'); that failed for me, getimagesize($path); works.
Well, I'm off work now. If you don't get it working, I can't help you untill tomorrow
Good luck!
Re: PHP Image size problem
Posted: Tue Jul 20, 2010 9:36 am
by Vaughanjb
Amazing, that was the problem, I have taken them out now. And works exactly how I need it too.
THANKS LuaMadman EXCELLENT HELP. Credit to forums
Re: PHP Image size problem
Posted: Wed Jul 21, 2010 1:10 am
by LuaMadman
You are welcome. Happy you finnaly solved it =)