
|
Image Uploading and Resizing with PHP
With the ever-increasing resolutions of today's digital cameras,
web site owners and web designers are forced to consider resizing
any images that are uploaded to their web site. With PHP, this can be
done rather easily, and the required code is published on this page
in a little bit.
Some Background
Digital Images taken with a typical 4 megapixel camera can be as
large as 3 megabytes. To display a large version of this image with
very high quality on a website, the file can be as small as
80-150 kilobytes. Here
is an example large image file that is about 113k. There are
very few applications with a need to store and/or serve very large,
high resolution images over the web. These resolutions are great
for printing on photo paper, but provide no additional quality on
a modern computer monitor. As a result, most images should be sized properly
for delivery on the web. For a website owner or web designer who allows users to
upload their own images to a server, the task of resizing those
images is upon you.
There are a few different approaches to this problem. Many of the
scripts I have found around the internet take a "realtime"
approach. In this case, the original, large size image is stored
on the server and resized when the page is requested. While this
approach works fine, it suffers a number of drawbacks.
- The large image(s) are actually stored on the server. This takes
up more server space than is required, and could become a problem
if you allow users to add their own images to your website
- Resizing an image is computationally intensive. The operation
will consume server memory and processor resources with every
page request. For web sites with a large number of page views this
can seriously impact the capacity of the server.
- Image resizing with each page view will cause the page to load
slower while the image is resized, making the user wait longer
to actually see something on their screen.
We believe that the best approach is to resize the images when
they are uploaded to the server. For most applications, images are
uploaded one time, and viewed many hundreds or thousands of times.
It simply makes good sense to do all the really "hard"
work up front, and one time, rather than with every page request.
The 4word systems Solution
Here is our very small code sample that will resize images as they
are uploaded from a web form. Please read the comments in detail.
They explain exactly what is going on, and where you should make
changes for the destination directory and output sizes.
This script only supports the JPG file format. The compression
algorithm for GIF images is protected by a restrictive licensing
agreement that prevents the developers of PHP from including support
for GIF images in the base platform.
Part I: The HTML Form
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="uploadfile"/>
<input type="submit"/>
</form>
Part II: Getting at the file, resizing the image, and saving to
the server. (upload.php)
<?php
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the
original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk. I have assumed that you
want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// NOTE: PHP will clean up the temp file it created
when the request
// has completed.
?>
Best of Luck,
The 4word systems team. |