PHP's built in system for uploading files allows us to create an HTML form that uses the "file" element. This is a type of HTML input tag that tells a user's web browser to display a file selection dialogue, and to include any selected file with the data sent along with a form.
Code: Select all
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000">";
<input type="file" name="imagefile">
<input type="submit" name="upload" value="Upload Image">
</form>When PHP receives the submitted form it takes the file, or files, that have been uploaded and places details of them in the $_FILES array. This array gives us plenty of information about the file that was uploaded: it's type, it's filename, and where PHP has temporarily stored it. Using the file type information that PHP puts in the Array array we can determine what sort of file the user has uploaded and open it using the correct PHP function. It might be tempting to use the file extension to determine the image type, .gif for example, but this can be dangerous. Some non-Windows computers do not use an extension, so you cannot be certain that an upload will have one either. All uploaded files have to include their MIME type.
Once we have determined the file type and opened the image file we can then delete the file from PHP's temporary storage. This isn't terribly important, but it's good practise to tidy up when we have the opportunity.
Code: Select all
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
switch($mimetype) {
case "image/jpg":
case "image/jpeg":
case "image/pjpeg": //IE's weird jpeg MIME type
$i = imagecreatefromjpeg($temporary_name);
break;
case "image/gif":
$i = imagecreatefromgif($temporary_name);
break;
case "image/png":
$i = imagecreatefrompng($temporary_name);
break;
}
unlink($temporary_name);Code: Select all
imagejpeg($i,"images/uploadedfile.jpg",80);Now that we have the image as open as a GD resource we can go about the process of creating a resized thumbnail version of it. The key processes involved are creating a new image that is proportionally smaller, and then copying the original image data to it.
To create the smaller image we need to calculate the width and height based upon the dimensions of the original. In order to do this we need to know the maximum width and height that a thumbnail should be.
Code: Select all
$dest_x = 150;
$dest_y = 150;Code: Select all
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}Now that the dimensions of the thumbnail are known we can create a new image based on them. There are two options for creating a new image with GD: imagecreate() and imagecreatetruecolor(). Imagecreate() generates a palette based 256 color image while imagecreatetruecolor() gives us a true color image capable of storing 16.8 million colors. Resizing to a 256 color image often looks dirty or washed out due to the nature of color replacement, so we'll use imagecreatetruecolor().
Code: Select all
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);Code: Select all
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));$thumb now contains a smaller version of . At this stage it is sensible to save the thumbnail image so that we don't have to create it again.
Code: Select all
imagejpeg($thumb, "images/thumbnail.jpg", 80);Code: Select all
<?php
// Get the details of "imagefile"
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
//Open the image using the imagecreatefrom..() command based on the MIME type.
switch($mimetype) {
case "image/jpg":
case "image/jpeg":
$i = imagecreatefromjpeg($temporary_name);
break;
case "image/gif":
$i = imagecreatefromgif($temporary_name);
break;
case "image/png":
$i = imagecreatefrompng($temporary_name);
break;
}
//Delete the uploaded file
unlink($temporary_name);
//Save a copy of the original
imagejpeg($i,"images/uploadedfile.jpg",80);
//Specify the size of the thumbnail
$dest_x = 150;
$dest_y = 150;
//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
//Is the width of the original bigger than the height?
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
//Using the original dimensions
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}
//Generate a new image at the size of the thumbnail
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
//Copy the original image data to it using resampling
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
//Save the thumbnail
imagejpeg($thumb, "images/thumbnail.jpg", 80);
?>