PHP Image size problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

PHP Image size problem

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: PHP Image size problem

Post by LuaMadman »

give getimagesize() a try
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: PHP Image size problem

Post by Vaughanjb »

Thanks for the reply LuaMadman,

How would I lay this out in my code? :?
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: PHP Image size problem

Post 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
Last edited by LuaMadman on Tue Jul 20, 2010 8:54 am, edited 1 time in total.
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: PHP Image size problem

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: PHP Image size problem

Post 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
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: PHP Image size problem

Post 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
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: PHP Image size problem

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: PHP Image size problem

Post 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!
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: PHP Image size problem

Post 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
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: PHP Image size problem

Post by LuaMadman »

You are welcome. Happy you finnaly solved it =)
Post Reply