1.
|
|
|
First of all we will create a simple form in HTML so that we will be able to upload images.
<form method="post" action="index.php" enctype="multipart/form-data"> <input type="file" name="uploaded_file" /> <input type="submit" value="Upload" /> </form>
Please note that we have used enctype="multipart/form-data" to correctly support file uploads.
|
|
|
2.
|
|
|
Now we will write some PHP code to handle file upload.
We must first check if there was an upload and if the file was successfully stored to the temporary location on the server.
if ( isset($_FILES['uploaded_file']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
|
|
|
3.
|
|
|
Next we save the location of the temporary filename to variable $f
$f = $_FILES['uploaded_file']['tmp_name'];
and in variable $fname we save the name of original file
$fname=$_FILES['uploaded_file']['name'];
|
|
|
4.
|
|
|
We will also need original image width and height.
$x = getimagesize($f); $sw = $x[0]; $sh = $x[1];
|
|
|
5.
|
|
|
Based on the format of the image uploaded we now load the image into $im variable.
if (strpos($fname,'.jpg')>0) $im=ImageCreateFromJPEG($f); elseif (strpos($fname,'.gif')>0) $im=ImageCreateFromGIF($f); elseif (strpos($fname,'.png')>0) $im=ImageCreateFromPNG($f);
|
|
|
6.
|
|
|
We create a new empty image $thumb where we specify the new size of the image.
$thumb = ImageCreateTrueColor(100, 100);
In this example the size of the image will be 100px x 100px.
Now we copy the original image stored in variable $im to the newly created image $thumb.
ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, 100, 100, $sw, $sh);
|
|
|
7.
|
|
|
And in the end we must of course save the image to a file.
if (strpos($fname,'.jpg')>0) imagejpeg($thumb,$fname); elseif (strpos($fname,'.gif')>0) imagegif($thumb,$fname); elseif (strpos($fname,'.png')>0) imagepng($thumb,$fname);
We have used IF statement so that output format of the image will be the same as the original format.
|
|
|
8.
|
|
|
For verification purposes we also include code to show the newly created image in the browser.
echo '<img src="'.$fname.'"></img>';
|
|
|
9.
|
|
|
You can now open your page in browser and if you upload an image, your image will be resized to 100px x 100px.
Complete code from this lesson can be found below.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP image upload</title> </head> <body> <p>Upload picture: </p> <form method="post" action="index.php" enctype="multipart/form-data"> <input type="file" name="uploaded_file" /> <input type="submit" value="Upload" /> </form>
<?php if ( isset($_FILES['uploaded_file']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
$f=$_FILES['uploaded_file']['tmp_name']; $fname=$_FILES['uploaded_file']['name']; $x = getimagesize($f); $sw = $x[0]; $sh = $x[1]; if (strpos($fname,'.jpg')>0) $im=ImageCreateFromJPEG($f); elseif (strpos($fname,'.gif')>0) $im=ImageCreateFromGIF($f); elseif (strpos($fname,'.png')>0) $im=ImageCreateFromPNG($f); else echo "False"; $thumb = ImageCreateTrueColor(100, 100); ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, 100, 100, $sw, $sh); if (strpos($fname,'.jpg')>0) imagejpeg($thumb,$fname); elseif (strpos($fname,'.gif')>0) imagegif($thumb,$fname); elseif (strpos($fname,'.png')>0) imagepng($thumb,$fname); echo '<img src="'.$fname.'"></img>'; } ?> </body> </html>
|
|
|
|