web blazonry web development scripts and tutorials Get best price on Unlocked Samsung Galaxy A21
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   




PHP Name Generators
  Wu-Name
  U.S. Name
  Baby Name

Bookmark and Share




PHP: Upload and Resize an Image

You created a cool contact directory and you want to allow people to upload their own photos or you want to create an image repository which you upload images and create thumbnails out of them. Whatever it is, you need to be able to upload images and resize them.

PHP has the ability to upload files such as documents or images using the multipart/form-data protocol, but how do you use this and how do you resize the images after they are uploaded?

What you need:

  • PHP installed properly
  • *NIX OS such as Linux
  • djpeg, cjpeg and pnmscale UNIX utility programs (see below)

This script works only with JPEG images. Also I'll resize the uploaded image only if it is larger than 250x200. Else, I'll leave it as it is.

Create Upload Form
First we need to create the form to upload the image. The MAX_FILE_SIZE variable needs to be set to the maximum allowable file size (in bytes) for upload. This is set using a hidden field and for this example will set to 50,000 bytes (approx. 50 kb).

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="25000"> Upload Image: <input type="file" name="imgfile">
<font size="1">Click browse to upload a local file</font>
<input type="submit" value="Upload Image">
</form>

Process Uploaded Data
Next we need to process the uploaded information when it is submitted. I'll do it all on the same page checking for the REQUEST_METHOD to know if the page was POSTed to.

PHP uploads the file to a temp location on your server (defined in php.ini) It also includes the following:

Variable NameDescription
$imgfiletemporary filename (pointer)
$imgfile_nameoriginal filename
$imgfile_sizesize of uploaded file
$imgfile_typemime-type of uploaded file

NOTE: imgfile is the name given on the form

Before copying the file, we check that a malicious user is not trying to abuse the script by trying to work on files it should not be, such as /etc/passwd. We do this with the PHP function is_uploaded_file(). More detail about this function is at the PHP.net site.

If is_uploaded_file returns TRUE, copy the file from its temp location to where you want it using the PHP copy() function.

if (is_uploaded_file($imgfile))
{
   $newfile = $uploaddir . "/" . $final_filename";
   if (!copy($imgfile, $newfile))
   {
      // if an error occurs the file could not
      // be written, read or possibly does not exist
      print "Error Uploading File.";
      exit();
   }
}

Re-Sizing the Uploaded Image
To resize the uploaded image we use the pnmscale function which scales images in the PNM format. We use djpeg to convert the JPEG images to PNM, and cjpeg to convert them back. Here's the code to convert, scale and write out a scaled JPG image.

/*== where storing tmp img file ==*/
$tmpimg = tempnam("/tmp" "MKPH");
$newfile = "$uploaddir/scaled.jpg";

/*== CONVERT IMAGE TO PNM ==*/
if ($ext == "jpg") { system("djpeg $imgfile >$tmpimg"); }
else { echo("Extension Unknown. Please only upload a JPEG image."); exit(); }

/*== scale image using pnmscale and output using cjpeg ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$newfile");

The above functions are just the basics of the script. There are a few little things added to complete the script. Such as checking the file extension, handling where to copy the file, and changing the upload filename.

Download the complete Script Source
The directory this script runs in (or the upload directory) must be writable by the server


Things to Look Out For

  • UNIX does not like filenames with spaces, though it's fine in Windows and Mac.

  • It would be better to not use the filename submitted but instead use a unique id. For example, "EMPID.jpg" where EMPID is the unique id of the employee's record.

  • Make sure your web server has write access to the upload directory

  • Pay attention to file locations and paths. Most of the functions in the script use the full filename path. So a slash at the front is much different than a slash at the front for your web server.

Relevant PHP Functions

On php.net, there are User Contributed Notes for all PHP fuctions. These are incredibly helpful. I highly recommend them.

is_uploaded_file()

Handling file uploads (a simple example)

Getting Required Software

The djpeg and cjpeg programs are provided in the libjpeg library package.

The pnmscale programs are provided in the libgr-progs library package.

Both of these packages are available as RPMs for RedHat-compatible systems and can be found at your local RPM Repository such as RPM Find.

 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
Baby Name Generator
U.S. Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL
Intro to Web Databases (PHP, MySQL)

Least Popular Pages
iNews Applet (Java)
Java Resources
Site Monitor (Perl)
PHP Resources
 
 

  privacy policy     ||     © 1997-2016. astonishinc.com   All Rights Reserved.