您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

一个完整的图片上传php类

2024/4/19 22:24:25发布5次查看
这个php图片上传类功能非常完善,完全可以满足各种图片上传需求
max_size = (1024/2)*1000; // 750kb $this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' ); $this->extensions = array( 'image/jpeg' => '.jpg', 'image/gif' => '.gif', 'image/png' => '.png', 'image/x-png' => '.png', 'image/pjpeg' => '.jpg' ); $this->dest_dir = $destdir; $this->max_height = $maxheightthumb; $this->max_main_height = $maxheightmain; } function putimage( $formname, $newname ){ $this->image_field = $formname; if( $this->getimage() ){ // check for errors if ( !$this->checktype() ) return $this->throwerror(2); if ( !$this->checkfilesize() ) return $this->throwerror(1); // get image $img = $this->image_file; // check seize if ( !$this->checkimagesize() ) return $this->throwerror(3); // get image dimensinos $size = $this->getimagesize(); $size['width'] = $size[0]; $size['height'] = $size[1]; $ratio = $this->max_height/$size['height']; $maxheight = $this->max_height; $maxwidth = $size['width'] * $ratio; // create thumbnail $s_t = $this->resizeimage( $size, $img, $maxwidth, $maxheight,$newname,'s' ); if( $size['height'] > $this->max_main_height ){ $ratio = $this->max_main_height/$size['height']; $maxheight = $this->max_main_height; $maxwidth = $size['width'] * $ratio; }else{ $maxheight = $size['height']; $maxwidth = $size['width']; } // create large rescaled $s_l = $this->resizeimage( $size, $img, $maxwidth, $maxheight,$newname,'l' ); // remove temporary file unlink($img['tmp_name']); if( $s_t && $s_l ){ $nm = split('_',$newname); $this->last_ext = $this->extensions[$size['mime']]; $this->last_pid = $nm[0]; $this->last_uid = $nm[1]; return 'ok'; }else{ return $this->throwerror( 4 ); } }else{ return $this->throwerror( 2 ); } } function resizeimage($size,$img,$maxwidth,$maxheight,$newname,$ext){ // create a thumbnail if($size['mime'] == image/pjpeg || $size['mime'] == image/jpeg){ $new_img = imagecreatefromjpeg($img['tmp_name']); }elseif($size['mime'] == image/x-png || $size['mime'] == image/png){ $new_img = imagecreatefrompng($img['tmp_name']); }elseif($size['mime'] == image/gif){ $new_img = imagecreatefromgif($img['tmp_name']); } if (function_exists('imagecreatetruecolor')){ $resized_img = imagecreatetruecolor($maxwidth,$maxheight); }else{ return(error: please make sure your server has gd library ver 2+); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']); if($size['mime'] == image/pjpeg || $size['mime'] == image/jpeg){ $success = imagejpeg ($resized_img,$this->dest_dir.$newname.'_'.$ext.$this->extensions[$size['mime']]); }elseif($size['mime'] == image/x-png || $size['mime'] == image/png){ $success = imagepng ($resized_img,$this->dest_dir.$newname.'_'.$ext.$this->extensions[$size['mime']]); }elseif($size['mime'] == image/gif){ $success = imagegif ($resized_img,$this->dest_dir.$newname.'_'.$ext.$this->extensions[$size['mime']]); } // remove temporary images imagedestroy ($resized_img); imagedestroy ($new_img); return $success; } function getimage(){ if( isset($_files[$this->image_field]) && is_uploaded_file($_files[$this->image_field]['tmp_name']) ){ $this->image_file = $_files[$this->image_field]; return true; } return false; } function returnimg(){ return $this->image_file; } function getimagesize(){ $img = $this->returnimg(); return getimagesize($img['tmp_name']); } function checkimagesize(){ $size = $this->getimagesize(); if( $size[1] max_height ) return false; return true; } function checkfilesize(){ $img = $this->returnimg(); if( $img['size'] > $this->max_size ) return false; return true; } function checktype(){ $img = $this->returnimg(); $type = split('/',$img['type']); if( !in_array( $type[1], $this->allowed_types ) ) return false; return true; } function throwerror($val){ switch($val){ case 1: return 'error: file size is too large'; break; case 2: return 'error: improper file format'; break; case 3: return 'error: your image is too small'; break; case 4: return 'error: there was an error creating the images'; break; } } } ?>
复制代码
图片上传, php
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录