Reply
Need help with image upload and manipulation.
Old 04-19-2008, 01:39 PM Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
I need to be able to add a feature to a page that I'm creating. The page will allow the user to upload 5 images. The images then need to be resized to a specific height. The box that I'm fitting the images into is smaller than the images will be so I need to be able to center the picture in the box and crop off the left and right sides.

None of the manipulation needs to be saved as a new image file, it just needs to be able to display on the screen like this and the user will then print out the page.

Can anyone tell me how to do this please or point me in the right direction?
zincoxide is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-19-2008, 02:12 PM Re: Need help with image upload and manipulation.
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Sounds like a job for gd

http://us.php.net/gd
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-19-2008, 02:18 PM Re: Need help with image upload and manipulation.
Ultra Talker

Posts: 408
If you're doing 5 images simultaneously then you'll need to save them to new (at least temporary) image files.
Lucas3677 is offline
Reply With Quote
View Public Profile Visit Lucas3677's homepage!
 
Old 04-20-2008, 03:39 AM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
Thanks... I looked into GD and got it going perfectly. It took a little fiddling with to get it right but that's great because I learned something new!

Now how can I set it up that the user can upload the 5 images. And I guess most importantly, how do I do that while maintaining my server integrity. I'm not too worried about malicious files being uploaded as I will probably make this page password protected with only a couple of very trusted users, however, for added safety, I think I should do it right -- not to mention that they could do something inadvertently.
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-20-2008, 10:18 AM Re: Need help with image upload and manipulation.
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Glad you got it going!

The HTML code to allow a user to upload a file can be found at http://www.w3.org/TR/html401/interac...ml#h-17.13.4.2 - Note the enctype is very important. Check out http://us2.php.net/features.file-upload if your curious how to get and handle file uploads from your PHP script.

You can use Exif (http://us2.php.net/manual/en/ref.exif.php) to determine the file type.

Other things to consider, to name a few, besides verifying the file type would be the file size, the amount of data over time you'd like users to be able to upload, disk space available, ensuring file extensions match file type, modifying max execution time to ensure upload scripts don't time-out, max allowed memory limit if you're working with very large images/bitmaps, etc..
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-20-2008, 04:59 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
I'm running into a little trouble with this and am hoping someone can help out please?

Here is my HTML (I think this works fine):
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">
<html>
<head>
</head>
<body>
	<form action="getfiles.php"
	       enctype="multipart/form-data"
	       method="post">
	   <p>
	   	<h3><u>Realtor Information</u></h3>
	   	Name of Realtor: <input type="text" name="realtor" /><br/>
		   <br/>
	   	Occupation Title: <input type="text" name="title" value="Sales Representative" /><br/>
		   <br/>
	   	Phone Number (###) ###-####: <input type="text" name="phone" /><br/>
		   <br/>
		   <h3><u>Property Information</u></h3>
		   Address: <input type="text" name="address" /><br/>
		   <br/>
		   Price: <input type="text" name="price" /> &nbsp;&nbsp; Property Taxes: <input type="text" name="taxes" /><br/>
		   <br/>
		   Rate: <input type="text" name="rate" />
			<br/>
		   <h3><u>Pictures</u></h3>
			<input type="hidden" name="MAX_FILE_SIZE" value="3072000" />
			Select a main picture: <input type="file" name="mainpic" /><br/><br/>
		   Feature picture 1: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="file" name="feature1" /><br/><br/>
		   Feature picture 2: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="file" name="feature2" /><br/><br/>
		   Feature picture 3: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="file" name="feature3" /><br/><br/>
		   Feature picture 4: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="file" name="feature4" /><br/><br/>
		   <br/>
		   <input type="checkbox" name="border" checked /> Show borders around images.<br/>
		   <br/>
		   <input type="submit" value="Create Feature Sheet" /> <input type="reset" value="Clear Fields" />
	   </p>
	 </form>
</body>
</html>
Here is my PHP for handling the file:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">
<html>
<head>
</head>
<body>
<?php
    
foreach ($_POST as $key => $val) {
        if(
$val)
            echo 
"<input type=\"hidden\" name=\"$key\" value=\"$val\" />";
    }
    
    foreach (
$_FILES["pics"]["error"] as $key => $error) {
        if (
$error == UPLOAD_ERR_OK) {
            
$tmp_name $_FILES["pics"]["tmp_name"][$key];
            
$name $_FILES["pics"]["name"][$key];
            
move_uploaded_file($tmp_name"data/$name");
        }
    }
?>
</body>
</html>
Here is the error message that I'm getting:
HTML Code:
Warning: Invalid argument supplied for foreach() in /public_html/dir/getfiles.php on line 12
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-20-2008, 06:30 PM Re: Need help with image upload and manipulation.
vectorialpx's Avatar
Super Talker

Posts: 128
Name: irimia octavian
Location: Romania
$_POST is not defined

add before:

if(isset($_POST)&&!empty($_POST)) foreach....
__________________
my photos, my website, creation lab
vectorial pixel web services :: and, sorry for my English !
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 04-20-2008, 09:56 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
shouldn't $_POST be defined because I'm submitting a form via POST?
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-22-2008, 02:53 PM Re: Need help with image upload and manipulation.
Ultra Talker

Posts: 408
You should be using $_FILES['feature1'], $_FILES['feature2'], etc. instead of $_FILES['pics']. The reason you are getting the foreach() error is because $_FILES['pics'] is not defined.
Lucas3677 is offline
Reply With Quote
View Public Profile Visit Lucas3677's homepage!
 
Old 04-24-2008, 05:14 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
Okay... I fixed that and now here is what I'm getting:

Code:
Warning: move_uploaded_file(data/Tiger.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpqdLmfq' to 'data/Tiger.JPG' in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file(data/Tiger.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpe18N9U' to 'data/Tiger.JPG' in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file(data/Tiger.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpIgmZtZ' to 'data/Tiger.JPG' in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file(data/Tiger.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpMf1fZC' to 'data/Tiger.JPG' in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file(data/Tiger.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpTuJIoQ' to 'data/Tiger.JPG' in /home/moneytim/domains/moneytime.ca/public_html/realtors/getfiles.php on line 16
Any suggestions?
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-24-2008, 05:23 PM Re: Need help with image upload and manipulation.
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Could be a permissions issue. Run a system('whoami'); in a simple script to determine who the script is running as, then give that user/group permission to write to that directory.

I'm guessing though that apache+suexec isn't set up, so you may have to chmod a+w the folder. If it's your own server, I'd setup apache+suexec and define the user that your script should run as, or request that your host/service provider do this for you.
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-24-2008, 10:37 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
It is my own server. I have apache 2.2.8 and suphp running (which is the same as suexec).

I will check the permissions tomorrow and get back.

Thanks.
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-26-2008, 10:16 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
Okay... I got the uploader working. The problem was being caused because I didn't have the directory created where it was trying to copy the tmp_file to.

Now... There is a potential that there will be more than one person using this script at a time. What this script does is create feature sheets for realtors that I work with. So... They will upload pictures of the house that they want this on, then it will save the pictures to a fixed file name (ie. mainpic.jpg, featurepic1.jpg, featurepic2.jpg, etc). Then it takes those set .jpg names and resizes them and saves them to tmp0.jpg, tmp1.jpg, tmp2.jpg, etc.

Then, I use the tmp#.jpg pics to create the feature sheet as they are now the sizes that I need for my template.

Heres my concern. Although unlikely, it will be possible that two realtors do this at the same time thereby possibly getting messed up feature sheets because it might overwrite a pic.

So... I'm thinking... Maybe I should somehow use sessions or something to save a file with a temporary name unique to the session that launched it. Then create the feature sheet from that. The problem is that I don't know how, and I will need to figure out a way to delete the file after 24hrs or something to make sure that my HDD doesn't fill up. Although, I probably could create a cronjob to go in and delete any files that are older than 24hrs or something.

I would also like to make it so that if they typed something wrong on the feature sheet that they don't have to re-upload the pics. Right now the way it is set they do.

Any ideas??
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-26-2008, 10:53 PM Re: Need help with image upload and manipulation.
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Have you considered just storing everything in a database table? Something like:

id int unsigned primary key auto_increment
realtor_id int
edit_time timestamp
title varchar(128)
description text
mls varchar(32)
image_type int (or char if you want to store the MIME)
image blob
thumbnail blob

I just gave a simple example showing how you could store any associated data in a single table. Your cron script could remove old entries using the edit_time field, and you could assign each realtor a unique ID as well.

Just a thought
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-26-2008, 11:32 PM Re: Need help with image upload and manipulation.
Webmaster Talker

Posts: 522
I did think of it but then I thought back to another post that I made on this site a while ago and people suggested not to put images into a database due to performance or something. I guess it would be less load on the server though.

How would i save the image to the table though?
zincoxide is offline
Reply With Quote
View Public Profile
 
Old 04-27-2008, 03:18 AM Re: Need help with image upload and manipulation.
solomongaby's Avatar
Webmaster Talker

Posts: 518
Name: Gabe Solomon
Location: Romania
here you can find an article about storing images in mysql
http://www.phpriot.com/articles/images-in-mysql
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-02-2008, 11:34 AM Re: Need help with image upload and manipulation.
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Quote:
Originally Posted by zincoxide View Post
I did think of it but then I thought back to another post that I made on this site a while ago and people suggested not to put images into a database due to performance or something. I guess it would be less load on the server though.

How would i save the image to the table though?
MySQL performance concerns are highly over-exaggerated by many. When your tables and queries are constructed properly, MYSQL is quite impressive. --

No need to through any more code at you, but I'll tell you a couple tricks. First of all, use two tables -- one with just a primary key and the image blob. Use your other table for storing other file-related information, such as a MIME type or description. That way if you're doing searches by anything other than the primary key, it will be much faster especially if you're avoiding variable length fields in your second table.

More importantly, use a caching technique. Typically image files are going to be viewed a lot more than updated/written. Keep a cache folder, and write a new script solely to display the image. Store the last image update time in your description table. When your display script runs, do a simple SQL check against the file timestamp in the cache folder (if it even exists) and determine whether or not you need to query the database (and write the new cache image) or just spit out the cached image. When your user updates (or uploads) an image, update the timestamp stored in the SQL table. This is especially helpful if you're using a database on a separate server as it really cuts down on LAN bandwidth needs.
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help with image upload and manipulation.
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off