uploads.php file
class image_upload{
private $srcimg; //Original picture
private $destimg; // target image
private $width; //Width of the original image
private $height; //The height of the original image
private $type; //Image type of original file
private $thumb_width; //Width of thumbnail
private $thumb_height; //Height of thumbnail
private $cut; //Whether to cut the image to the specified height
private $tmp; //Temporary address for uploading images
private $error;
private $im; //Create a temporary image handle
private $new_name; //New name of uploaded file
function __construct($srcimg,$t_width,$t_height,$cut,$tmp,$error){
$this->srcimg=$srcimg;
$this->thumb_width=$t_width;
$this->thumb_height=$t_height;
$this->cut=$cut;
$this->tmp=$tmp;
$this->error=$error;
$this->get_srcimg_type();
$this->get_new_upload_name();
}
function img_upload(){
//How to upload files
$this->check_error($this->error);
$this->in_type();
$dst_dir='./images';
if(!is_dir($dst_dir)){
mkdir($dst_dir);
echo "%%% ";
}
if(is_uploaded_file($this->tmp)){
If(move_uploaded_file($this->tmp, $this->new_name)){
echo "File uploaded successfully ";
Return true;
}else{
echo 'File cannot be moved, upload failed';
exit;
}
}else{
echo 'File upload may be attacked';
exit;
}
}
function make_thumbnail(){
//Method to generate thumbnails
$this->get_dest_imgpath();
$this->make_im();
$this->width=imagesx($this->im);
$this->height=imagesy($this->im);
$thumb_ratio=$this->thumb_width/$this->thumb_height;
$ratio=$this->width/$this->height;
if($this->cut==1){ //Whether to cut
If($ratio>=$thumb_ratio){
$img=imagecreatetruecolor($this->thumb_width, $this->thumb_height);
imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height, $this->height*$thumb_ratio, $this->height );
Imagejpeg($img,$this->destimg);
echo "Thumbnail generated successfully";
}else{
$img=imagecreatetruecolor($this->thumb_width, $this->thumb_height);
imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height, $this->width, $this->width/$thumb_ratio );
Imagejpeg($img,$this->destimg);
echo "Thumbnail generated successfully";
}
}else{
if($ratio>=$thumb_ratio){
$img=imagecreatetruecolor($this->thumb_height*$thumb_ratio, $this->thumb_height);
imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_height*$thumb_ratio, $this->thumb_height, $this->width, $this->height );
Imagejpeg($img,$this->destimg);
echo "Thumbnail generated successfully";
}else{
$img=imagecreatetruecolor($this->thumb_width, $this->thumb_width/$thumb_ratio);
imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_width/$thumb_ratio, $this->width, $this->height );
Imagejpeg($img,$this->destimg);
echo "Thumbnail generated successfully";
}
}
imagedestroy($this->im);
imagedestroy($img);
}
private function check_error($error){
//Check for file upload errors;
if($error>0){
switch($error){
case 1:
echo "The size of the uploaded file exceeds the configuration in the PHP.INI file ";
Break;
Case 2:
echo "The size of the uploaded file exceeds the limit in the form ";
Break;
case 3:
echo "Only some files were uploaded ";
Break; case 4:
echo "No files uploaded ";
Break;
case 6:
echo "The temporary unknown for image storage is not set in php.ini ";
Break;
case 7:
echo "The hard disk cannot be written to, and the upload failed ";
Break;
Default:
echo "Unknown error";
Break;
}
}
}
private function get_srcimg_type(){
//Determine the image type of the source file
$this->type=substr(strrchr($this->srcimg, '.'),'1');
}
private function in_type(){
//Check whether the file matches the type
$type_arr=array('gif','jpg','png');
if(!in_array($this->type, $type_arr)){
echo "Only supports three types of file formats: PNG, GIF, and JPG... Please re-upload the correct format";
exit;
}
}
private function get_new_upload_name(){
//Generate a new name for the uploaded file
$this->new_name='images/'.date('YmdHis').'.'.$this->type;
}
private function make_im(){
//Create a new image from the original file
switch($this->type){
case 'jpg':
$this->im=imagecreatefromjpeg($this->new_name);
Break;
case 'gif':
$this->im=imagecreatefromgif($this->new_name);
Break;
case 'png':
$this->im=imagecreatefrompng($this->new_name);
Break;
}
}
private function get_dest_imgpath(){
//Get the storage path of the thumbnail
$len1=strlen($this->new_name);
$len2=strlen(strrchr($this->new_name,'.'));
$len3=$len1-$len2;
$this->destimg=substr($this->new_name,0,$len3).'_small.'.$this->type;
}
}
print_r($_FILES);
$file=$_FILES['image'];
echo $file['name'];
$uploads=new image_upload($file['name'], 120, 160, 1, $file['tmp_name'],$file['error'] );
if($uploads->img_upload()){
$uploads->make_thumbnail();
}
?>
|