简单的php图片上传类_php

本文介绍了一个简单的PHP图片上传类,该类支持图片上传、自动创建目录、文件类型和大小检查等功能,并提供了添加水印的选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

某天突然做一个图片上传的功能,然后就萌发了写一个简单的php图片上传类的想法,呵呵,代码还有很多不足的地方,希望各位大侠批评指出。。

<?php 

/*
 * {by}D.n.sky
 * {description}图片上传类
 */
class UnloadImage{
	/* 文件域 */
	public $file;
	/* 上传文件保存路径 */
	public $img_path = '../image-data-all';
	/* 是否器启用时间文件夹命名来储存图片 */
	public $is_fun_datefile = true;
	/* 时候覆盖相同文件名文件 */
	public $is_cover = true;
	/* 是否自动命名文件 */
    public $is_auto_sove = false;
	/* 是否增加图片水印 */
	public $is_watermark = true;
	/* 放缩图像 */
	public $is_brevimage = true;
	/* 允许上传图片大小 */
	public $img_size = 102400;
	/* 允许上传图片格式 */
	public $img_type = array(
	        'image/jpg',
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/bmp',
			'image/x-png');
	/* 最终图片保存路径 */
	public $sove_new_name;
	/* 图片信息 */
	public $info;
	public $info_type;
	public $info_size;
	public $info_name;
	public $info_tmp;
	/* 构造函数 */
	function __construct(){
		}
	/* 析构函数 */
	function __destruct(){
		}
	/* 读取文件信息 */
	public function set_image_info($file, $img_path){
		if($file != ''){
			$file = $_FILES[$file];
			$this -> info_name = $file['name']; #1.jpg
			$this -> info_size = $file['size']; #13456
			$this -> info_type = $file['type']; #image/jpeg
			$this -> info_tmp = $file['tmp_name']; #C:\Windows\Temp\php3CFC.tmp
			if($img_path != '') $this -> img_path = $img_path;
			/* 获取原始图像尺寸 */
			$this -> info = getimagesize($this -> info_tmp);
		    }
		}
	/* 检测和创建目录 */
	public function auto_path(){
		if($this -> is_fun_datefile){
			/* 生成日期文件夹 */
			$d = date('Ymd');
			/* 生成日期文件夹 */
			$this -> img_path = $this -> img_path .'/'. $d ;
		    }
		if(!file_exists($this -> img_path)){
			mkdir($this -> img_path);
			}
		}
	/* 判断文件类型 */
	public function check_type(){
		if(!in_array($this-> info_type, $this->img_type )){
			$this -> set_msg(array(0, 'image type is not this.', ''));
			}
		}
	/* 判断图片大小 */
	public function check_size(){
		if($this -> info_size > $this -> img_size){
			$this -> set_msg(array(0, 'image size is >' .$this -> img_size. '.', ''));
			}
		}
	/* 上传图片 */
	public function start_upload_image(){
		$img_sove_path = $this -> img_path .'/'. $this -> info_name;
		/* 自动重命名 */
		if($this -> is_auto_sove){
			 $img_sove_path = $this -> img_path .'/'. date('YmdHis') . $this -> file_sign();
		    }
		/* 文件是否存在 */
		$fi = file_exists($img_sove_path);
		if(!$fi || ($fi && $this -> is_cover)){
			/* 上传文件 */
			if(!move_uploaded_file($this -> info_tmp, $img_sove_path)){
				$this -> set_msg(array(0, 'error', null));
				}else{
					$this -> set_msg(array(1, 'ok', 'date' => array(
					     'imageurl' => $img_sove_path,
						 'width' => $this -> info[0],
						 'height' =>$this -> info[1]
					)));
					}
		    }
			$this-> sove_new_name = $img_sove_path;
		}
	/* 水印 */
	public function makewater(){
		/* 获取上传完成的图片 */
	    $success_image = $this -> create();
		$str = 'www.desetang.com';
		/* 创建一个等宽的图像 */
        $water_image = imagecreatetruecolor($this -> info[1], 40);
		
	    $alpha = imagecolorallocatealpha($water_image, 255, 255, 255, 90);
	    $color = imagecolorallocate($water_image, 255, 255, 0);
		imagefill($water_image, 0, 0, $alpha);
	    imagettftext($water_image, 16, 4, 0, 40, $color, 'msyh.ttf', $str);
		imagecopy($success_image, $water_image, 0, $this -> info[1]-40, 0, 0, $this -> info[0], 40);
		/* 重新保存水印后的图片 */
	    $this -> copyimage($success_image);
		/* 释放缓存 */
		imagedestroy($success_image);
		imagedestroy($water_image);
		}
	/* 输出后缀 */
	private function file_sign(){
		return strrchr($this -> info_name, '.');
		}
	/* 创建/保存水印图片 */
	private function create(){
		switch($this -> info[2]){
			 case 1: # GIF image
			     $timg = imagecreatefromgif($this-> sove_new_name);
				 break;
		     case 2: # JPEG image
                 $timg = imagecreatefromjpeg($this-> sove_new_name);
                 break;
			 case 3: # PNG image
                 $timg = imagecreatefrompng($this-> sove_new_name);
                 break;
		    }
		    return $timg;
		}
	/* 输出/保存图片 */
	private function copyimage($simage){
		switch($this -> info[2]){
			 case 1: # GIF image
			     imagegif($simage, $this-> sove_new_name);
				 break;
		     case 2: # JPEG image
                 imagejpeg($simage, $this-> sove_new_name);
                 break;
			 case 3: # PNG image
                 imagepng($simage, $this-> sove_new_name);
                 break;
		    }
		}
	/* 上传文件 */
	public function save_image(){
		/* 检测图片保存路径 */
		$this -> auto_path();
		/* 检测文件类型 */
		$this -> check_type();
		/* 检测图片大小 */
		$this -> check_size();
		/* 上传 */
		$this -> start_upload_image();
		/* 执行水印 */
		if($this -> is_watermark) $this-> makewater();
		
		return 1;
		}
	/* error */
	public function set_msg($error){
		/* 如果是错误则停止代码 */
		if($error[0] == 0) exit();
		}
}
?>

1.增加水印选项

2.功能还在完善。。。

 

转载于:https://my.oschina.net/desetang/blog/69319

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值