Constraining pictures uploaded sizes...

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
invision
Forum Newbie
Posts: 12
Joined: Wed Feb 01, 2006 4:29 am
Location: Scotland

Constraining pictures uploaded sizes...

Post by invision »

Hello,

I need a slight help with something I have planned to complete my web site project.

Currently I am allowing users to upload PNG, GIF and JPG to my site.

However, I have noticed when uploading wide(anything over 650px) and high(anything over 800px) pics that it makes the web page look pretty messy.

Is there anything I can do to constrain the pictures width and height. In a perfect world, have code in the picture display file to reduce them to fit on the page, then have a link to view it in a new window?

Any help would be greatly appreciated.


Mike.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

getimagesize()
There are many many threads about creating thumbnails here. There are many threads containing thumbnailing code here. Look in Code Snippets, which has several of these.

Alternately, you could just tell the image to adjust its size displayed but not actually resize it (however it may look "bad") using the width and height attributes found in <img> tags.
invision
Forum Newbie
Posts: 12
Joined: Wed Feb 01, 2006 4:29 am
Location: Scotland

Post by invision »

It's not so much a thumbnail I want to make, but just to rescale it for display so its underneath a specific horizontal and width that makes it look presentable on screen.

My preferred maximum width is 650px and preferred maximum height is 800px.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

most of the thumbnailing scripts can be told what size to scale down to, if scaling is needed. Thumbnailing is being used in a generic sense here where it refers to the script resizing an image; that's all.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

invision wrote:It's not so much a thumbnail I want to make, but just to rescale it for display so its underneath a specific horizontal and width that makes it look presentable on screen.

My preferred maximum width is 650px and preferred maximum height is 800px.
Still a thumbnail. Just a really big one.
invision
Forum Newbie
Posts: 12
Joined: Wed Feb 01, 2006 4:29 am
Location: Scotland

Post by invision »

Here's how I did it :

Output File, I echoed :

Code: Select all

<a href="show_picture.php?id='. $_GET['id'] .'" target="_blank"><img src="show_picture.php?id='. $_GET['id'] .'" alt="' . $row['image_name'] . '" width="' . $row['width'] . '" height="' . $row['height'] . '" border="1"></a>
Upload File :

Code: Select all

$pic = getimagesize($filename);
	$width = $pic[0];
	$height = $pic[1];
	if ($width > 600 || $height > 800) {
	$widthdiff = 600/$width;
	$heightdiff = 800/$height;
	$biggest = min($widthdiff,$heightdiff);
	$newwidth = $biggest*$width;
	$newheight = $biggest*$height;
	}
I just added a 'width' and 'height' to the table of the database.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

So you're making the user's browser rescale the image? I hope image quality isn't important to you. Nor bandwidth for that matter.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

invision: that is a great example of how not to achive what you want

edit: as onion2k says...image quality and bandwidth would be a concern
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Indeed. That is very bad practice. Imagine if a user uploaded a 5mb image with 2000 width and 3000 height.

Every user wanting to view the image would have to download ~5mb whereas if done properly they would see a better quality image and have to download about a fifth of the data.

Read some of the previous comments and rethink your code. :)
Post Reply