• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

一个非常完整的php图片上传类

PHP 水墨上仙 2672次浏览

这个php图片上传类功能非常完善,完全可以满足各种图片上传需求

<?php
/**************************************
 seesaw associates | http://seesaw.net
 
 client: 
 file: 
 description: 
 
 Copyright (C) 2008 Matt Kenefick(.com)
**************************************/
 
class mk_imageUpload{
 
	var $max_size;
	var $allowed_types;
	var $thumb_height;
	var $dest_dir;
	var $extensions;
	var $max_height;
	var $max_main_height;
 
	var $last_ext;
	var $last_pid;
	var $last_uid;
 
	var $image_file;
	var $image_field;
 
	function __construct( $maxHeightMain, $maxHeightThumb, $destDir ){
		$this->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] < $this->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图片上传类
喜欢 (0)
加载中……