| This article describes a PHP class for uploading images based on an extension of the previous Upload_class class. If you need a friend, you can refer to it below. Share a PHP extension class that uploads image files, based on the previous article: an extension of the PHP file upload class and instance. Mainly used for uploading image files. Code:
Upload_dir. $this->file_copy; $this->check_dir ($this->thumb_folder); Run these checks to create not existing directories $this->check_dir ($this->foto_folder); The upload dir is created during the file upload (if not already exists) $thumb = $this->thumb_folder. $this- >file_copy; $foto = $this->foto_folder. $this->file_copy; if ($landscape _only) {$this->get_img_size ($filename); if ($this->y_size > $this->x_size) {$this->img_rotate ($filename, $compression); }} $this->check_dimensions ($filename); Check which size is longer then the max value if ($this->larger_curr_value > $this->larger_dim_value) {$this->thumbs ($filename, $foto, $this->larger_dim_value, $compression); } else {copy ($filename, $foto); } if ($create _thumb) {if ($this->larger_curR_value > $this->larger_dim_thumb_value) {$this->thumbs ($filename, $thumb, $this->larger_dim_ Thumb_value, $compression); Finally resize the image} else {copy ($filename, $thumb); }} if ($delete _tmp_file) $this->del_temp_file ($filename); Note If you delete the TMP file the check if a file exists won't work} function get_img_size ($file) { $img _size = getimagesize ($file); $this->x_size = $img _size[0]; $this->y_size = $img _size[1]; } function Check_dimensions ($filename) {$this->get_img_size ($filename); $x _check = $this->x_size-$this->x_max_size; $y _check = $this->y_size-$this->y_max_size; if ($x _check < $y _check) {$this->larger_dim = "Y"; $this->larger_curr_value = $this->y_size; $this->larger_dim_value = $this->y_max_size; $this->larger_dim_thumb_value = $this->y_max_thumb_size; } else {$this->larger_dim = "x"; $this->larger_curr_value = $this->x_size; $this->larger_dim_value = $this->x_max_size; $this->larger_dim_thumb_value = $this->x_max_thumb_size; }} function Img_rotate ($WR _file, $comp) {$new _x = $this->y_size; $new _y = $this->x_size; if ($this->use_image_magick) {exec (sprintf ("Mogrify-rotate 90-quality%d%s", $comp, $WR _file)); } else {$src _img = imagecreatefromjpeg ($wr _file); $rot _img = imagerotate ($src _img, 90, 0); $new _img = Imagecreatetruecolor ($new _x, $new _y); Imageantialias ($new _img, TRUE); Imagecopyresampled ($new _img, $rot _img, 0, 0, 0, 0, $new _x, $new _y, $new _x, $new _y); Imagejpeg ($new _img, $this->upload_dir $this->file_copy, $comp); }} function Thumbs ($file _name_src, $file _name_dest, $target _size, $quality = +) {//print_r (Func_get_args ()); $size = getimagesize ($file _name_src); if ($this->larger_dim = = "X") {$w = Number_format ($target _size, 0, ', ', '); $h = Number_format (($size [1]/$size [0]) * $target _size,0, ', ', '); } else {$h = Number_format ($target _size, 0, ', ', '); $w = Number_format (($size [0]/$size [1]) * $target _size,0, ', ', '); } if ($this->use_image_magick) {exec (sprintf ("Convert%s-resize%dx%d-quality%d%s", $file _name _SRC, $w, $h, $quality, $file _name_dest)); } else {$dest = Imagecreatetruecolor ($w, $h); Imageantialias ($dest, TRUE); $SRC = Imagecreatefromjpeg ($file _name_src); Imagecopyresampled ($dest, $src, 0, 0, 0, 0, $w, $h, $size [0], $size [1]); Imagejpeg ($dest, $file _name_dest, $quality); }}} $max _size = 1024*1024;The max. Size for uploading (~1MB) define ("Max_size", $max _size); $foto _upload = new Foto_upload; $foto _upload->upload_dir = $_server[' Document_root ']. " /files/"; "Files" is the folder for the uploaded files (which you had to create these folder) $foto _upload->foto_folder = $_server[ ' Document_root ']. " /files/photo/"; $foto _upload->thumb_folder = $_server[' Document_root ']. " /files/thumb/"; $foto _upload->extensions = Array (". jpg"); Specify the allowed extension (s) here $foto _upload->language = "en"; $foto _upload->x_max_size = 300; $foto _upload->y_max_size = 200; $foto _upload->x_max_thumb_size = 120; $foto _upload->y_max_thumb_size = 150; if (Isset ($_post[' submit ')) && $_post[' submit '] = = "Upload") {$foto _upload->the_temp_file = $_files[' Uploa d ' [' Tmp_name ']; $foto _upload->the_file = $_files[' upload ' [' name ']; $foto _upload->http_error = $_files[' upload ' [' Error ']; $foto _upload->replace = (isset ($_post[' replace '))?$_post[' replace ']: "n"; Because only a checked checkboxes is true $foto _upload->do_filename_check = "n"; if ($foto _upload->upload ()) {$foto _upload->process_image (false, True, 80); $foto _upload->message[] = "processed foto:". $foto _upload->file_copy. "!"; "File_copy is the name of the Foto"}} $error = $foto _upload->show_error_string ();?>2. Example of a call to upload a picture
PHP image upload Class invocation example-bbs.it-home.org Upload image
|