<?php/* by rain */date_default_timezone_set (' asia/chongqing '); Header ("Content-type : Text/html;charset=utf-8 ");//This is the class of uploading pictures class updateclass{public $pathall $picname;p ublic function update ($file, $kaiqi =false, $w =0, $H =0) {//return $file;d Ie;foreach ($file as $ Key=> $val) {$updatename = "{$key}";} $file = $file; $name = $file [$updatename] [' name ']; //file name $size= $file [$updatename] [' Size ']; //file sizes $type=$ file[$updatename] [' type ']; //file type $tmp_name= $file [$updatename] [' tmp_name ']; //file temp file $error= $file [$ updatename][' ERROR ']; //file error if (($error)) {switch ($error) {case 1: $s = "The configured size exceeded"; break;case 2: $s = " Exceeded the size of the form "; Break;case 3: $s =" partial file upload "; Break;case 4: $s =" no file "; Break;case 6: $s =" temporary file not found "; break;case 7: $s = "file not writable"; Break;case 8: $s = "Interrupt file upload due to extended PHP program"; $re = ' uploaded error ';return $s. ' > '. $re;} else{//determines whether the type in the array is $arr=array (' image/jpeg ', ' image/jpg ', ' image/png ', ' image/gif '); if (!in_array ($type, $arr)) {$re = ' upload error prompt '; $s = ' upload format not ';return $s. ' > '. $re; exit;} Determine if Http post commits the IF (!is_uploaded_file ($tmp _name)) {$re = ' upload error prompt '; $s = ' This file is not submitted via post ';return $s. ' > ' . $re; exit;} else{//gets the real name, named $name=date (' Ymd ', Time ()). rand (0,999999999); $hou =explode ('. ', $name); $houzui = $hou [Count ($ HOU)-1]; $p =date (' Ym '); $path = ' uploade/' $p; $this->pathall= $path; $this->picname= $NAME. '. $houzui, if (!is_dir ($path)) {mkdir ($path, 0777,true); $path = $path. ' /'. $NAME. '. $houzui, if (Move_uploaded_file ($tmp _name, $path)) {if ($kaiqi ==true) {$ys = $this->yasuo ($path, $w, $H);} if ($kaiqi ==true) {return $path. ' * '. $ys. ' @";} else{return $path. ' *‘;}} else{return "Upload failed";}} else{$path = $path. ' /'. $NAME. '. $houzui, if (Move_uploaded_file ($tmp _name, $path)) {if ($kaiqi ==true) {$yasuo = $this->yasuo ($path, $w, $H);} echo "<div style=\" width:400px\ "></ Div> ", if ($kaiqi ==true) {return $path. ' * '. $ys. ' @";} else{return $path. ' *‘;}} else{return "Upload failed";}}}} Public function yasuo ($from, $W, $H) {$yasuo _path= $this->pathall. ' /'. Date (' Ym '), if (!is_dir ($yasuo _path)) {mkdir ($yasuo _path,0777,true);} $from = "$from"; list ($width, $height, $imagestype) =getimagesize ($from); //get the original photo width, height, file type $ Get_style=image_type_to_mime_type ($imagestype); // This is the Get picture type image/jpeg//equivalent to $get _style=getimagesize ($from) [mine]; $imagecreatefrom =str_replace ('/' , ' CreateFrom ', $get _style), //replaced by imagecreatefromjpeg$imagejpeg=str_replace ('/', NULL, $get _style); //replaced with imagejpeg$yuanben_image= $imagecreatefrom ($from); // Imagecreatefromjpeg () Create a new image by file or URL original picture $yasuo_image=imagecreatetruecolor ($W, $H); //This is to create a new canvas that holds the target picture imagecopyresampled ($yasuo _image, $yuanben _image,0,0,0,0, $W, $H, $width, $height); //imagecopyresampled resample Copy part image and resize//imagecopyresampled (target image connection resource, source image connection resource, target X coordinate point, target Y coordinate point, source X coordinate point, source Y coordinate point, target width, target height, width of source image, height of source image);//header (' content-type: $get _style '); //the type of the head output image header (' Content-type: image/jpeg $imagejpeg ($yasuo _image, "$yasuo _path/". Yasuo '. $this->picname); //the type of image that is exported from the Web page, the location where the compressed chart is saved return "$yasuo _path/". ' Yasuo '. $this->picname;imagedestroy ($yasuo _image); //destroy the compressed picture Imagedestroy ($yuanben _image);//Destroy the original picture}}?>
PHP background upload pictures and compress pictures, the individual is a novice, if there are errors, please forgive me
This article is from the "11037915" blog, please be sure to keep this source http://11047915.blog.51cto.com/11037915/1874042
PHP Background Upload image