therealwesfoster Posted February 26, 2008 Share Posted February 26, 2008 I'm created an admin panel for uploading images to a site. Now there are 2 ways the user can upload images, and I have questions for each, so please help me out. Method 1. User can upload the images through the admin panel. After the user presses the submit button, the PHP script will resize each image (max 600x600 px) and save it to /gallery/. Also, the script will make thumbnail images for each one and save them to /gallery/thumbs/. And save all instances to the db. Q: 1. I'm only going to show 2 upload fields in the form. But I'm allowing the user to click "Add Another Upload" and it will add another upload field (by using javascript). So the 2 default ones will be named "file1" and "file2", and every field added after that will be "file3", "file4" etc. Get it? So heres the question.. In the PHP script, when I'm receiving all of the $_POST vars, how do I check how many upload fields there were, and how do I know if they were empty or not? Method 2. The user uploads alot of images to /gallery/ via FTP. Then they go into the admin cp and click a button like "Update Database". This script should read the /gallery/ directory and get every image that isn't in the database already. Then it should resize them (max 600x600), create the thumbnails, then save the instances to the database. Q: 1. I'm afraid that this would lag horribly. Any suggestions on anther way to do this? Or does it look good? ----- These are my ideas on how they would work, if there is a better way to do it, please post it. Thanks Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 27, 2008 Share Posted February 27, 2008 Lets say for arguments sake that your user has uploaded a file and it is called 1234.jpg and you want it to be 15px wide and 25px tall... the script below will open that file and resize it without having to make a new copy of it: <?php //Name this file thumb.php $image = imagecreatefromjpeg("uploads/$_GET[src]"); // Uploaded file location $image_width=imagesx($image); // Original Photo width $image_height=imagesy($image); // Original Photo height $thumbnail_width = $_GET['width']; $thumbnail_height = $_GET['height']; $thumbnail_quality = 100; $thumbnail = imagecreatetruecolor($thumbnail_width,$thumbnail_height); imagecopyresized($thumbnail,$image,0,0,0,0,$thumbnail_width,$thumbnail_height,$image_width,$image_height); imagejpeg($thumbnail,NULL,$thumbnail_quality); imagedestroy($thumbnail) ?> Now in whatever page you want the thumbnail: <img src="thumb.php?src=1234.jpg&width=15&height=25"> Note, I did not test this, and you will need some other code to get the image to fit into certain dimensions proportionally, but its a start. I found this method in a tutorial somewhere, and I think it is a pretty elegant solution. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.