RMcLeod
03-18-2008, 04:45 PM
Okay, so this class has two static functions.
upload_image: Takes an uploaded file ensures it's an allowed type and file size, then names the file with the specified name or a default value and moves it to the required directory
resize_image: Takes an uploaded file ensures it's an allowed type and file size then creates a scaled version with the width and height set by the user, this is then saved with a specified or default name in the specified directory.
HINT: To keep the same aspect ratio as the original
list($src_x, $src_y) = getimagesize($_FILES['img']['tmp_name']);
$ratio = $src_x / $src_y;
//fixed width
$width = 200;
$height = $width / $ratio;
//fixed height
$height = 200
$width = $height * $ratio;
Now call the image_resize function with the width and height attributes set accordingly.
Class
class Image_Handler extends System_Config {
private static $default_img_name;
private static function set_name($type) {
$t = date('Y-m-d-H-i-s') . '.';
$t .= (substr($type, 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($type, 6);
self::$default_img_name = $t;
}
public static function upload_image($img, $dst_dir, $name = '') {
if($name == '') {
self::set_name($img['type']);
$name = self::$default_img_name;
} else {
$name = $name . '.';
$name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($img['type'], 6);
}
$destination = $dst_dir . '/' . $name;
if($img['error'] == 0) {
if(!in_array($img['type'], self::$allowed_image_types)) {
throw new mailException('Not a valid image type ' . $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
} elseif($img['size'] > self::$max_upload_size) {
throw new mailException('File size exceeds limit', basename($_SERVER['PHP_SELF']), get_class($this));
} elseif(!@move_uploaded_file($img['tmp_name'], $destination)) {
throw new mailException('Upload failed moving' . $img['tmp_name'] . ' to ' . $destination, basename($_SERVER['PHP_SELF']), get_class($this));
} else {
return $destination;
}
} else {
switch($img['error']) {
case 1:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 2:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 3:
throw new mailException('The file was only partially uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 4:
throw new mailException('No file was uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
default:
throw new mailException('An unspecified error has occured', basename($_SERVER['PHP_SELF']), get_class($this));
}
}
}
public static function resize_image($img, $dst_dir, $width, $height, $name = '', $pngbkg = '') {
if($name == '') {
self::set_name($img['type']);
$name = self::$default_img_name;
} else {
$name = $name . '.';
$name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($img['type'], 6);
}
$destination = $dst_dir . '/' . $name;
if($img['error'] != 0) {
switch($img['error']) {
case 1:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 2:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 3:
throw new mailException('The file was only partially uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 4:
throw new mailException('No file was uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
default:
throw new mailException('An unspecified error has occured', basename($_SERVER['PHP_SELF']), get_class($this));
}
} else {
if(!in_array($img['type'], self::$allowed_image_types)) {
throw new mailException('Not a valid image type ' . $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
} elseif($img['size'] > self::$max_upload_size) {
throw new mailException('File size exceeds limit', basename($_SERVER['PHP_SELF']), get_class($this));
} else {
$ext = substr($img['type'], 6);
switch($ext) {
case 'png':
$create = 'imagecreatefrompng';
$save = 'imagepng';
break;
case 'gif':
$create = 'imagecreatefromgif';
$save = 'imagegif';
break;
default:
$create = 'imagecreatefromjpeg';
$save = 'imagejpeg';
break;
}
$src_img = $create($img['tmp_name']);
list($src_width, $src_height) = getimagesize($img['tmp_name']);
$dest_x = $width;
$dest_y = $height;
if($pngbkg == '') {
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
} else {
$dst_img = imagecreatefrompng($pngbkg);
}
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $src_width, $src_height);
if(!@$save($dst_img, $destination)) {
throw new mailException('The image could not be resized', basename($_SERVER['PHP_SELF']), get_class($this));
} else {
return $name;
}
}
}
}
}
Note: PHP 5 required
Note: This class extends a system_config class to load some values such as allowed image types and max file sizes, these can be set as private member variables instead.
Note: mailException is a custom exception I wrote which mails me the exception whilst displaying a user friendly error to the user. Use Exception instead if you like.
If you have any questions or suggestions regarding functionality I'll be happy to help
upload_image: Takes an uploaded file ensures it's an allowed type and file size, then names the file with the specified name or a default value and moves it to the required directory
resize_image: Takes an uploaded file ensures it's an allowed type and file size then creates a scaled version with the width and height set by the user, this is then saved with a specified or default name in the specified directory.
HINT: To keep the same aspect ratio as the original
list($src_x, $src_y) = getimagesize($_FILES['img']['tmp_name']);
$ratio = $src_x / $src_y;
//fixed width
$width = 200;
$height = $width / $ratio;
//fixed height
$height = 200
$width = $height * $ratio;
Now call the image_resize function with the width and height attributes set accordingly.
Class
class Image_Handler extends System_Config {
private static $default_img_name;
private static function set_name($type) {
$t = date('Y-m-d-H-i-s') . '.';
$t .= (substr($type, 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($type, 6);
self::$default_img_name = $t;
}
public static function upload_image($img, $dst_dir, $name = '') {
if($name == '') {
self::set_name($img['type']);
$name = self::$default_img_name;
} else {
$name = $name . '.';
$name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($img['type'], 6);
}
$destination = $dst_dir . '/' . $name;
if($img['error'] == 0) {
if(!in_array($img['type'], self::$allowed_image_types)) {
throw new mailException('Not a valid image type ' . $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
} elseif($img['size'] > self::$max_upload_size) {
throw new mailException('File size exceeds limit', basename($_SERVER['PHP_SELF']), get_class($this));
} elseif(!@move_uploaded_file($img['tmp_name'], $destination)) {
throw new mailException('Upload failed moving' . $img['tmp_name'] . ' to ' . $destination, basename($_SERVER['PHP_SELF']), get_class($this));
} else {
return $destination;
}
} else {
switch($img['error']) {
case 1:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 2:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 3:
throw new mailException('The file was only partially uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 4:
throw new mailException('No file was uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
default:
throw new mailException('An unspecified error has occured', basename($_SERVER['PHP_SELF']), get_class($this));
}
}
}
public static function resize_image($img, $dst_dir, $width, $height, $name = '', $pngbkg = '') {
if($name == '') {
self::set_name($img['type']);
$name = self::$default_img_name;
} else {
$name = $name . '.';
$name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' : substr($img['type'], 6);
}
$destination = $dst_dir . '/' . $name;
if($img['error'] != 0) {
switch($img['error']) {
case 1:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 2:
throw new mailException('The file is too large', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 3:
throw new mailException('The file was only partially uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
case 4:
throw new mailException('No file was uploaded', basename($_SERVER['PHP_SELF']), get_class($this));
break;
default:
throw new mailException('An unspecified error has occured', basename($_SERVER['PHP_SELF']), get_class($this));
}
} else {
if(!in_array($img['type'], self::$allowed_image_types)) {
throw new mailException('Not a valid image type ' . $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
} elseif($img['size'] > self::$max_upload_size) {
throw new mailException('File size exceeds limit', basename($_SERVER['PHP_SELF']), get_class($this));
} else {
$ext = substr($img['type'], 6);
switch($ext) {
case 'png':
$create = 'imagecreatefrompng';
$save = 'imagepng';
break;
case 'gif':
$create = 'imagecreatefromgif';
$save = 'imagegif';
break;
default:
$create = 'imagecreatefromjpeg';
$save = 'imagejpeg';
break;
}
$src_img = $create($img['tmp_name']);
list($src_width, $src_height) = getimagesize($img['tmp_name']);
$dest_x = $width;
$dest_y = $height;
if($pngbkg == '') {
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
} else {
$dst_img = imagecreatefrompng($pngbkg);
}
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $src_width, $src_height);
if(!@$save($dst_img, $destination)) {
throw new mailException('The image could not be resized', basename($_SERVER['PHP_SELF']), get_class($this));
} else {
return $name;
}
}
}
}
}
Note: PHP 5 required
Note: This class extends a system_config class to load some values such as allowed image types and max file sizes, these can be set as private member variables instead.
Note: mailException is a custom exception I wrote which mails me the exception whilst displaying a user friendly error to the user. Use Exception instead if you like.
If you have any questions or suggestions regarding functionality I'll be happy to help