Create a Thumbnail
function image_create_thumb($filename, $width, $height)
{
list($image["Width"], $image["Height"], $image["Type"]) = getimagesize($filename);
if($image["Type"] == IMG_JPG || $image["Type"] == IMG_JPEG)
{
$im_image = ImageCreateFromJPEG($filename);
}
if($image["Type"] == IMG_GIF)
{
$im_image = ImageCreateFromGIF($filename);
}
if($image["Type"] == IMG_PNG)
{
$im_image = ImageCreateFromPNG($filename);
}
if($image["Width"] > $image["Height"])
{
$scale = $width / $image["Width"];
$thumb_width = $width;
$thumb_height = floor($image["Height"]*$scale);
}
else
{
$scale = $height / $image["Height"];
$thumb_height = $height;
$thumb_width = floor($image["Width"]*$scale);
}
$im_thumb = @ImageCreateTrueColor($thumb_width, $thumb_height) or die("Cannot Initialize new GD image stream");
ImageCopyResized($im_thumb,$im_image,0,0,0,0,$thumb_width,$thumb_height,$image["Width"],$image["Height"]);
ImageDestroy($im_image);
ImagePNG($im_thumb, $filename);
ImageDestroy($im_thumb);
return true;
}//image_create_thumb($filename, $width, $height)
{
list($image["Width"], $image["Height"], $image["Type"]) = getimagesize($filename);
if($image["Type"] == IMG_JPG || $image["Type"] == IMG_JPEG)
{
$im_image = ImageCreateFromJPEG($filename);
}
if($image["Type"] == IMG_GIF)
{
$im_image = ImageCreateFromGIF($filename);
}
if($image["Type"] == IMG_PNG)
{
$im_image = ImageCreateFromPNG($filename);
}
if($image["Width"] > $image["Height"])
{
$scale = $width / $image["Width"];
$thumb_width = $width;
$thumb_height = floor($image["Height"]*$scale);
}
else
{
$scale = $height / $image["Height"];
$thumb_height = $height;
$thumb_width = floor($image["Width"]*$scale);
}
$im_thumb = @ImageCreateTrueColor($thumb_width, $thumb_height) or die("Cannot Initialize new GD image stream");
ImageCopyResized($im_thumb,$im_image,0,0,0,0,$thumb_width,$thumb_height,$image["Width"],$image["Height"]);
ImageDestroy($im_image);
ImagePNG($im_thumb, $filename);
ImageDestroy($im_thumb);
return true;
}//image_create_thumb($filename, $width, $height)





switch($image["Type"])
{
case IMG_JPG:
case IMG_JPEG:
$im_image = ImageCreateFromJPEG($filename);
break;
case IMG_GIF:
$im_image = ImageCreateFromGIF($filename);
break;
case IMG_PNG:
$im_image = ImageCreateFromPNG($filename);
break;
}
- The correct constants are: IMAGETYPE_JPEG ; IMAGETYPE_GIF ; IMAGETYPE_PNG
- You should NEVER use imagecopyresized() it comes out jaggy .. always use imagecopyresampled()
- The file is overwritten, not creating a thumbnail, you are resizing.
- File is saved as a PNG no matter what the input. you can have 'image.jpg', which is actually a png.
<?php
// dont include an extension
// usage: resizeImage( 'filetoberesized', 'path/and/filename',500)
function resizeImage($uploadfile, $dst_path, $max_size) {
$size = getimagesize($uploadfile); // get the image size, and type
$swidth = $size[0]; // source width
$sheight = $size[1]; // source height
switch($size[2]) { // get the file extension
case IMAGETYPE_GIF:
$ext = '.gif';
break;
case IMAGETYPE_PNG:
$ext = '.png';
break;
case IMAGETYPE_JPEG:
$ext = '.jpg';
break;
case IMAGETYPE_BMP:
$ext = '.jpg';
break;
case IMAGETYPE_WBMP:
$ext = '.jpg';
break;
case IMAGETYPE_XBM:
$ext = '.jpg';
break;
case IMAGETYPE_XPM:
$ext = '.jpg';
break;
default:
$ext = '.jpg';
break;
}
$dst_path .= $ext;
// CHECK IF IMAGE NEEDS TO BE RESIZED
if ($swidth > $max_size || $sheight > $max_size) {
// create the image
switch($size[2]) {
case IMAGETYPE_GIF:
$simg = imagecreatefromgif($uploadfile);
break;
case IMAGETYPE_PNG:
$simg = imagecreatefrompng($uploadfile);
break;
case IMAGETYPE_JPEG:
$simg = imagecreatefromjpeg($uploadfile);
break;
case IMAGETYPE_BMP:
$simg = imagecreatefromwbmp($uploadfile);
break;
case IMAGETYPE_WBMP:
$simg = imagecreatefromwbmp($uploadfile);
break;
case IMAGETYPE_XBM:
$simg = imagecreatefromxbm($uploadfile);
break;
case IMAGETYPE_XPM:
$simg = imagecreatefromxpm($uploadfile);
break;
default:
return FALSE;
break;
}
// do the resize match
if ($swidth > $sheight) {
$dwidth = $max_size;
$dheight = $sheight * ($dwidth / $swidth);
}else{
$dheight = $max_size;
$dwidth = $swidth * ($dheight / $sheight);
}
// CREATE DESTINATION IMAGE
$dst_img = imagecreatetruecolor($dwidth,$dheight);
// IF the image is a png, preserve alpha channels (transparency)
if ($size[2] == IMAGETYPE_PNG)
imagealphablending($dst_img, false);
// COPY IMAGE -- resampling will get rid of jaggies
imagecopyresampled($dst_img, $simg, 0, 0, 0, 0, $dwidth, $dheight, $swidth, $sheight);
// write the image
switch($size[2]) {
case IMAGETYPE_GIF:
imagegif($dst_img, $dst_path);
break;
case IMAGETYPE_PNG:
imagesavealpha($dst_img,true);
imagepng($dst_img, $dst_path, 0);
break;
case IMAGETYPE_JPEG:
imagejpeg($dst_img, $dst_path, 80);
break;
case IMAGETYPE_BMP:
imagejpeg($dst_img, $dst_path, 80);
break;
case IMAGETYPE_WBMP:
imagejpeg($dst_img, $dst_path, 80);
break;
case IMAGETYPE_XBM:
imagejpeg($dst_img, $dst_path, 80);
break;
case IMAGETYPE_XPM:
imagejpeg($dst_img, $dst_path, 80);
break;
default:
return FALSE;
break;
}
// DESTROY SOURCE/TEMP IMAGE
ImageDestroy($simg);
ImageDestroy($dst_img);
// VALIDATE FUNCTIONS
if (!file_exists($dst_path)) {
return FALSE;
}else{
return TRUE
}
}else{
// IMAGE DOES NOT NEED TO BE RESIZED, SO MOVE IT
copy($uploadfile, $dst_path);
return true;
}
// END FUNCTION
}
Thanks
-----------------------
Proud owner of scriptsentials.com and the Scriptsentials network.