Tokyoahead

News
News
Architecture
Architecture
Asian Cooking
Asian Cooking
Finance
Finance
Galapagos
Galapagos
Technology
Technology
Trips & Sights
Trips & Sights
  
   

PHP: Upload pictures for webpages easily

Technology If you have a web page, it can take some time to upload larger pictures series, make the miniature views and links etc. Here is a script that does it all for you and all you have to do is to cut & paste the HTML into your page.

GENERAL INFO / Requirements
This Script is currently version 1.0
It will only accept jpeg compressed images of a maximum of 2 MB size.
The Script contains two files, which split the settings from the functionality, so you can run it in several directories with each different settings.
You need PHP and NetPBM installed on the server. If you do not have it, get it from netpbm at sourceforge.

SETUP
The first file, pic_upload.php in the archive has to go to any folder on your web page where you want the function to be reachable from the outside. You might want to protect this folder form unauthorized access with a .htaccess-file os not anyone can upload pictures to your drive.

You have to edit only the first file, to match the settings that you want:

$rootpath: This is the path where the pictures will be uploaded, as your server sees it. This path can be relative ("./xx/"), but I would recommend to make it absolute, so you dont get errors once you move the script to a different path. The path has to end in a trailing slash. Also, you have to set write permissions to the flder you choose (777).
$httppath: This should point to the same path, but as you would reach it from the browser. This also has to have a trailing slash.
$th_side: This is a number of pixels that describes your maximum side length the miniature images will have. If you upload a picture landscape format 640x480 and the $th_side is 160, you will get a thumbnail 160x120, and if the source is portrait 480x640, you will get a thumbnail 120x160. The proportions are kept.
$pic_side: The same as above, only this one sets how the original picture will be scaled.
$target: If set to FALSE, the form for uploading pictures will not have the target-fields preselected. If set to TRUE, the fields WILL be preselected. You can however still change the settings when uploading pictures.
$inc_path: This is the path where you will upload the second file, func_inc.php. You would ideally copy this file to a separate folder outside your public_html.
$netpbm: This is the path to you netpbm bin-files.

USAGE
When you open pic_upload.php in your browser, you will see the fields for the pictures with a "Browse"-Button, the fields for the name and the check boxes for the target-option.
Click on the first "Browse"-button and select the first picture for the upload. You might want to check the size of the pictures before. If the pictures are too large, the upload will fail or take a long time. If they are smaller than the $pic_side setting, the will be enlarged to the given dimension.
The Name-field is per default "[same]". This means that the name will not be changed from its original. However, the name will be changed to lowercase and spaces will be filled with "_". If you replace the "[same]" with something else, this will be the new name. ATTENTION: The script will attach ".jpg" to the name.
The Target=Blank-Field will cause the link to the larger picture to open in a separate window if checked.
Once you press "Upload", the pictures will be uploaded and renamed & resized. Then, you will see a list of the pictures and their html-links so you can copy them into your HTML-page. The smaller picture will be called name_th.jpg and the larger one name.jpg, both will be located in the same directory as you set in $rootpath.

DOWNLOAD
Download the file now.

Future features
- an option not to link but just to make the thumbnail
- size-per-picture-settings
- multiple formats (not only jpeg)
- multiple image-manipulation engines

Questions & Answers, Troubleshooting

Q: I get the following error when running the script:
Warning: main(/backoffice/pic_upload/func_inc.php): failed to open stream: No such file or directory in c:program filesapache groupapachehtdocsuploaderspublic_htmlpic_upload.php on line 40
A: You have to make sure that the file "func_inc.php" is actually in the directory where it is searched here. What we can see from above's example, first of all the user just unpacked the file into the "htdocs". Note that "public_html" can be the same as "htdocs". So you can move the file pic_upload.php in the example above directly to the directory "uploaders". The func_inc.php can be in the same directory, or ideally somewhere outside the htdocs, for example c:program filesapache groupapachebackendfunctionsuploaders, so that a web-user will not be able to access it. Whereever you put it, you have tochange the path accordingly in in the pic_upload.php , using the line starting with $inc_path as described above.

Q: How do I set a directory writable?
A: Well first of all, under Windows, you do not need to do that. Under Unix & linux etc, you use your FTP clients CHMOD command.

If you want to help improving this script, or if you have questions, comments, complaints, please post a comment here.

Trackback

Trackback URL for this entry: http://tokyoahead.com/main/trackback.php/uploadpictureswithphp

No trackback comments for this entry.