PHP Image Thumbnailer (Source)
Posted February 15, 2011 by Nick Vogt in Programming
Many of the sites I build require an image thumbnailer, so that users can upload an image and it will automatically be thumbnailed. I decided to write a function that I could incorporate into these sites quickly and easily. This function takes a number of arguments, and will create any number of thumbnails in any sizes, and also has the option of moving the original, unedited file into a folder. It handles jpg, png, and gif, and creates jpg thumbnails.
Download it here, name it and put it somewhere. Use php "include" or "require" where it is needed, then call the createthumbs function, which takes four arguments:
The first argument is the $_FILES[] variable that points to the file that was uploaded.
The second argument is the filename of the image.
The third argument is an array that contains the different sizes and locations to save the images. It is formatted like so: array('imgdir/thumb' => array(360,240), 'imgdir/large' => array(800,600)). Each key in the main array is the image folder, and each value is an array that contains the width and height. You can have as many as you want.
The fourth argument is optional, and is a string that points to the directory you want the original image to be saved in, if you want it saved.
Note that all directories should be listed without the trailing forward slash /, though you can easily modify the source code to change that. If you need to save the file extension into a database, you can also modify the source code to run a MYSQL query.
The function returns FALSE if the file mime type was not correct, and TRUE if everything worked. You must have the corresponding folders set to read/write permission, otherwise the function won't do anything.
Here is the source code:
And an example of its usage:
Download it here, name it and put it somewhere. Use php "include" or "require" where it is needed, then call the createthumbs function, which takes four arguments:
The first argument is the $_FILES[] variable that points to the file that was uploaded.
The second argument is the filename of the image.
The third argument is an array that contains the different sizes and locations to save the images. It is formatted like so: array('imgdir/thumb' => array(360,240), 'imgdir/large' => array(800,600)). Each key in the main array is the image folder, and each value is an array that contains the width and height. You can have as many as you want.
The fourth argument is optional, and is a string that points to the directory you want the original image to be saved in, if you want it saved.
Note that all directories should be listed without the trailing forward slash /, though you can easily modify the source code to change that. If you need to save the file extension into a database, you can also modify the source code to run a MYSQL query.
The function returns FALSE if the file mime type was not correct, and TRUE if everything worked. You must have the corresponding folders set to read/write permission, otherwise the function won't do anything.
Here is the source code:
<?php function createthumbs($file, $id, $arr, $orig = '')
{
if($file[type] == 'image/jpeg' || $file[type] == 'image/pjpeg') $ext = 'jpg';
elseif($file[type] == 'image/png' || $file[type] == 'image/x-png') $ext = 'png';
elseif($file[type] == 'image/gif') $ext = 'gif';
else return FALSE;
$dim = getimagesize($file[tmp_name]);
foreach($arr as $key => $val) {
if($ext == 'jpg') $img = imagecreatefromjpeg($file[tmp_name]);
if($ext == 'png') $img = imagecreatefrompng($file[tmp_name]);
if($ext == 'gif') $img = imagecreatefromgif($file[tmp_name]);
if($dim[0] > $val[0] || $dim[1] > $val[1]) {
$rx = $dim[0] / $val[0];
$ry = $dim[1] / $val[1];
if($rx >= $ry) {
$w = $val[0];
$rat = $dim[0] / $w;
$h = round($dim[1] / $rat, 0);
} else {
$h = $val[1];
$rat = $dim[1] / $h;
$w = round($dim[0] / $rat, 0);
}
} else {
$w = $dim[0];
$h = $dim[1];
}
$new = imagecreatetruecolor($w, $h);
imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $dim[0], $dim[1]);
imagejpeg($new, $key . '/' . $id . '.jpg', 85);
}
if($orig != '') rename($file[tmp_name], $orig . '/' . $id . '.' . $ext);
return TRUE;
} ?>
{
if($file[type] == 'image/jpeg' || $file[type] == 'image/pjpeg') $ext = 'jpg';
elseif($file[type] == 'image/png' || $file[type] == 'image/x-png') $ext = 'png';
elseif($file[type] == 'image/gif') $ext = 'gif';
else return FALSE;
$dim = getimagesize($file[tmp_name]);
foreach($arr as $key => $val) {
if($ext == 'jpg') $img = imagecreatefromjpeg($file[tmp_name]);
if($ext == 'png') $img = imagecreatefrompng($file[tmp_name]);
if($ext == 'gif') $img = imagecreatefromgif($file[tmp_name]);
if($dim[0] > $val[0] || $dim[1] > $val[1]) {
$rx = $dim[0] / $val[0];
$ry = $dim[1] / $val[1];
if($rx >= $ry) {
$w = $val[0];
$rat = $dim[0] / $w;
$h = round($dim[1] / $rat, 0);
} else {
$h = $val[1];
$rat = $dim[1] / $h;
$w = round($dim[0] / $rat, 0);
}
} else {
$w = $dim[0];
$h = $dim[1];
}
$new = imagecreatetruecolor($w, $h);
imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $dim[0], $dim[1]);
imagejpeg($new, $key . '/' . $id . '.jpg', 85);
}
if($orig != '') rename($file[tmp_name], $orig . '/' . $id . '.' . $ext);
return TRUE;
} ?>
And an example of its usage:
<?php
$img = $_FILES['img'];
$id = 001;
$array = array('img/med' => array(200,160), 'img/lrg' => array(800,600));
$orig = 'img/orig';
include('thumb.php');
$run = createthumbs($img, $id, $array, $orig);
if($run == true) $message = 'Upload successful!';
else $message = 'Invalid file!';
?>
$img = $_FILES['img'];
$id = 001;
$array = array('img/med' => array(200,160), 'img/lrg' => array(800,600));
$orig = 'img/orig';
include('thumb.php');
$run = createthumbs($img, $id, $array, $orig);
if($run == true) $message = 'Upload successful!';
else $message = 'Invalid file!';
?>
| Series | PHP Tutorials |
|---|---|
| Tags | php |
