Try using PHP's builtin switch() construct for $image['Type']. It's easier to code and maintain, plus if you needed to add more options in the future it is easier and cleaner.
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; }
I am very very new when it comes to php, but I was wondering if anyone could post. I am simply copying and pasting this code but nothing shows, im wondering if I need to give some paths to either my file locations or images directly. Thanks.
This is a HORRIBLE example, no wonder you are all having problems, it will not work. - 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: returnFALSE; break; } // do the resize match if($swidth > $sheight){ $dwidth = $max_size; $dheight = $sheight * ($dwidth / $swidth); }else{ $dheight = $max_size; $dwidth = $swidth * ($dheight / $sheight); }
I am really interested in this code, my only question is if you could possibly include detailed commentary on the code intead of a simple title? I would like to know how this works.
I agree that it would have been better with some comments explaining the PHP image functions and how the width and height are being calculated. It's a nice snippet but without comments, it's not very useful to newbies and already common knowledge for professionals.
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.