Class to handle image uploading
<?php
/**
* Class Image upload
*/
class Upload {
private $output_folder;
private $input_file;
private $input_img;
private $src_img;
private $file_name; // basename($file['name'][0]);
private $max_file_size; // 2097152;
private $file_size; // filesize($file['tmp_name'][0]);
private $authorized_extensions;
private $file_extension; // strrchr($file['name'][0], '.');
function __construct() {
if ( !isset($output_folder) || empty($output_folder) )
throw new Exception("Error Processing Request : output folder not specified.", 1);
else {
$this->output_folder = $folder;
$this->max_file_size = parse_size(ini_get('post_max_size'));
$this->file_extensions = array(
'.png',
'.gif',
'.jpg',
'.jpeg'
);
}
}
/**
* Search and replace non authorized characters in filename
*/
private static sanitize_filename($file_name) {
$file_name = strtr($file_name,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$file_name = preg_replace('/([^.a-z0-9]+)/i', '-', $file_name)
return $filename;
}
/**
* Check before upload
*/
public static function check_file($file) {
if( !isset($file) || empty($file) ) {
throw new Exception("Error Processing Request : file not found or empty.", 1);
}
else {
$this->file_name = basename($file['photo']['name'][0]);
$this->file_size = filesize($file['photo']['tmp_name'][0]);
$this->file_extension = strrchr($file['photo']['name'][0], '.');
//vérifications de sécurité
if(!in_array($this->file_extension, $this->file_extensions)) {
throw new Exception("Error Processing Request : file not of type png, gif, jpg nor jpeg", 1);
}
if($this->file_size > $this->max_file_size) {
throw new Exception("Error Processing Request : file of size $this->file_size. Max filesize is $this->max_file_size.", 1);
}
else {
$this->file_name = self::sanitize_filename($this->file_name);
}
}
return $file;
}
/**
* Upload the file
*/
public function upload_file ($file) {
$this->input_file = self::check_file($file);
if( !move_uploaded_file($this->file_name, $this->output_folder . $this->file_name) ) {
throw new Exception("Error Processing Request : invalid filename or destination folder.", 1);
}
else {
$this->input_img = $this->output_folder . $this->file_name;
$sizes = getimagesize($this->input_img);
$thumb_width = '110';
$thumb_height = (int)($thumb_width/$sizes[0]*$sizes[1]);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
switch ($this->input_file_extension) {
case '.jpeg':
$this->src_img = imagecreatefromjpeg($this->input_img);
imagecopyresampled($thumb, $this->src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $sizes[0], $sizes[1]);
imagejpeg($thumb, '../img/photos/thumbs/'.$this->file_name.'');
imagedestroy($thumb);
break;
case '.jpg':
$this->src_img = imagecreatefromjpeg($this->input_img);
imagecopyresampled($thumb, $this->src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $sizes[0], $sizes[1]);
imagejpeg($thumb, '../img/photos/thumbs/'.$this->file_name.'');
imagedestroy($thumb);
break;
case '.png':
$this->src_img = imagecreatefrompng($this->input_img);
imagecopyresampled($thumb, $this->src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $sizes[0], $sizes[1]);
imagepng($thumb, '../img/photos/thumbs/'.$this->file_name.'');
imagedestroy($thumb);
break;
case '.gif':
$this->src_img = imagecreatefromgif($this->input_img);
imagecopyresampled($thumb, $this->src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $sizes[0], $sizes[1]);
imagegif($thumb, '../img/photos/thumbs/'.$this->file_name.'');
imagedestroy($thumb);
break;
}
echo $this->file_name.' successfully uploaded !';
}
}
}
}
}
?>