Forum Moderators: coopster
class resizer{
...
function resize($filename,$width,$height){
$img=$filename;
...
$thumb = 'the resized pic';
ImageJPEG($thumb,$img);
}}
require_once("resizer.php");
$resizeImage = new resizer();
$resizeImage->resize($_FILES['mypicture']['tmp_name'],250,250);
$resizeImage2 = new resizer();
$resizeImage2->resize($_FILES['mypicture']['tmp_name'],100,'100');
require_once("resizer.php");
//
$ex=$thumbname=$thumbpath=$large_path=NULL; // Initialize
$big_width = 250; // See notes . . . this has problems
$big_height = 250;
$t_width = 100;
$t_height = 100;
$name = $_FILES['mypicture']['name'];
// To create a thumb, we need to split on the extension. Watch...
list ($nm, $ex) = preg_split("/\./",$name,-1,PREG_SPLIT_NO_EMPTY);
$type = $_FILES['photo']['type'];
$size = $_FILES['photo']['size'];
$tmp_name = $_FILES['photo']['tmp_name'];
$error = $_FILES['photo']['error'];
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . '/your-upload-directory';
//
// error trapping is your friend. Here I'm just echoing and exiting,
// not best but with the info provided, best I can show you.
if (! ($size > 0)) { echo "<p>The data upload failed with a zero byte image file.</p>\n"; exit; }
if ($error > 0) { echo "<p>An unknown error occurred on upload.</p>\n"; exit; }
if (! preg_match('/jpe*g|gif|bmp|png/i',$type)) {
echo '<p>You can only upload images in jpg, gif, or png format.</p>'; exit;
}
//
// If no extension found (err, Macintosh) assume jpg and pray.
if (! isset($ex)){
$ex = (preg_match("/(jpe*g|gif|png|bmp)/i",$type))?".$1":'.jpg';
$name .= $ex;
}
$thumbname = $nm . '_tn' . $ex;
// You need to think in terms of full server path for uploads.
$thumbpath = "$uploads_dir/" . $thumbname;
$large_path = "$uploads_dir/" . $name;
//
// Okay . . . NOW . . . note we're passing the full path to
// where we want the files, and the TYPE so we can support other types.
// If we move it first, no need to reference $tmp_name in the function.
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$resizeImage = new resizer();
$resizeImage->resize($type,$large_path,$thumbpath,
$big_width,$big_height,$t_width,$t_height);
class resizer{
//
function resize($img_type,$img,$thumbname,$width,$height,$twidth,$theight){
//
// GD Function List, with our *allowed* upload types
$gfunctions = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
//
$img_info = GetImageSize($img);
if (! (($img_info[0]>0) and ($img_info[1]>0))) { echo "<p>Error occured getting image size.</p>"; exit; }
$w = $img_info[0];
$h = $img_info[1];
$func = 'imagecreatefrom' . $gfunctions[$type];
$write = 'image' . $gfunctions[$type];
// Note "$func" = imagecreatefromjpg, imagecreatefromgif, etc.
$src = $tn = $func($img); // so we resize from the original both times
// big image
$tmp = imagecreatetruecolor($width,$height);
imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$w,$h);
// Note "$write" = imagejpg, imagegif, etc.
$write($tmp,$img,100); // last param = quality
// thumb
$tmp=imagecreatetruecolor($twidth,$theight);
imagecopyresampled($tmp,$tn,0,0,0,0,$twidth,$theight,$w,$h);
$write($tmp,$tn,75);
}
}
// Allowed file types, note NOT the same as the functions
$ptypes = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
$ext = $known_photo_types[$type];
$name = $name . '.' . $ext;
$thumbname = $name . '_tn.' . $ext;