PHP 图片上传,添加字符串水印,添加图片(指定四个角) 图片压缩

php中写了一篇乱糟糟的代码,先贴出来,有时间了一定要整理下

/**
	 * 最好的是要上传两张图片,一张为原图,另一张是想添加的logo, 可以设置参数 
	 *$watermark = 1; //是否附加水印(1为加水印,其他为不加水印);
	 *$watertype = 2; //水印类型(1为文字,2为图片);
	 *$waterposition = 1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角); 
	 *$waterstring = "http://www.120.net/"; //水印字符串;	 
	 */
	public function test() {
		$file = 'logo'; //表单name
		$dirname = 'test'; //
		$upload_url = "./resource/images/" . $dirname . "/"; //上传到根目录,这个./是必须的l。
		$logo = $upload_url . "/log2.jpg";   //要添加的图片
		
		$size = getimagesize ($logo);
		
		$uptypes = array ('image/jpg', 'image/jpeg', 'image/png', 'image/gif', 'image/bmp' );
		
		$max_file_size = 2000000; //上传文件大小 限制, 单位byte
		ini_set('memory_limit', '120M');   //提升添加水印时内存分配,此功能占用内存较多.
		
		$watermark = 1; //是否附加水印(1为加水印,其他为不加水印);
		$watertype = 2; //水印类型(1为文字,2为图片);
		$waterposition = 4; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角); 
		$waterstring = "http://www.120.net/"; //水印字符串;	
		$imgpreview = 1; //是否生成预览图(1为生成,其他为不生成)
		$imgpreviewsize = 1/2; //缩略图比例
		if ($_SERVER ['REQUEST_METHOD'] == "POST") {
			
			$doctor_img = $_FILES [$file];
			//图片是否存在	
			if (! is_uploaded_file ( $doctor_img ['tmp_name'] )) {
				echo "图片不存在";
			}
			//检查文件大小
			if ($max_file_size < $doctor_img ["size"]) {
				echo "文件大";
				exit ();
			}
			//检查文件类型
			if (! in_array ( $doctor_img ["type"], $uptypes )) {
				echo "文件类型不符" . $doctor_img ["type"];
			}
			//创建文件夹
			if (! file_exists ( $upload_url )) {
				mkdir ( $upload_url );
			}
			$filename = $doctor_img ["tmp_name"];
			$image_size = getimagesize ( $filename );
			$pinfo = pathinfo ( $doctor_img ["name"] );
			$ftype = $pinfo ['extension'];
			
			$destination = $upload_url .'big'.time().$doctor_img ['name']; //存储路径,包括文件名
			if (move_uploaded_file ( $filename, $destination )) {
			
			} else {
				echo "移动出错";
			}
			
			$pinfo = pathinfo ( $destination );
			$fname = $pinfo ['basename'];
			
			//图片压缩,添加水印
			if ($watermark == 1) {
				$iinfo = getimagesize ( $destination);
				
				//初始化一个图像;
				$nimage = imagecreatetruecolor ( $image_size [0], $image_size [1] );  //需要gd2库  返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
				//为一幅图像分配颜色				
				$white = imagecolorallocate ( $nimage, 255, 255, 255 );
				$black = imagecolorallocate ( $nimage, 0, 0, 0 );
				$red = imagecolorallocate ( $nimage, 255, 0, 0 );
				$green = imagecolorallocate($nimage, 0, 255, 0);
				
				imagefill ( $nimage, 0, 0, $white );  //填充初始化图片背景颜色
				
				//获取上传的图片,源图片
				$simage = $this->createimage($destination);
				
				//把上传图片添加到初始化图片上.
				//bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
				//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
				//将上传的图片从坐标src_x,src_y开始, 宽度为 src_w,高度为 src_h 的一部分拷贝到初始化图像中坐标为 dst_x 和 dst_y 的位置上。
				imagecopy ( $nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1] );

				//bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
				//imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。 
				
				switch ($watertype) {
					case 1 : // 加水印字符串
						//imagefilledrectangle ( $nimage, 1, $image_size [1] - 15, 180, $image_size [1], $green);   //此行代码可以为字符串添加背景
						imagestring ( $nimage, 2, 3, $image_size [1] - 15, $waterstring, $black );
						break;
					case 2 :
						$simage1 = $this->createimage($logo);
						switch ($waterposition){
							case 1:  //1为左下角,
								$dst_x = 0;
								$dst_y = $image_size[1]-$size[1];
								break;
							case 2:  //2为右下角,
								$dst_x = $image_size[0]-$size[0];
								$dst_y = $image_size[1]-$size[1];
								break;
							case 3: //3为左上角,
								$dst_x = 0;
								$dst_y = 0;
								break;
							case 4: //4为右上角
								$dst_x = $image_size[0]-$size[0];
								$dst_y = 0;
								break;
						}
						imagecopy ( $nimage, $simage1, $dst_x, $dst_y, 0, 0, $size[0], $size[1] );
						imagedestroy ( $simage1 );
						break;
				
				}
				$this->outputimage($destination, $nimage, $destination);
				//覆盖上传的文件
				imagedestroy ( $nimage );
				imagedestroy ( $simage );
			}
 
			if($image_size[0]> 694)  //缩略图宽度为124
			{
				//获取上传的图片,源图片
				$s_image = $this->createimage($destination);

				$new_des = $upload_url . 'small'.time().$doctor_img ['name'];  //缩略图的命名
				$newwidth = 694;
				$newheight = $image_size[1]*$newwidth/$image_size[0];  //等比例压缩
				$newimage = imagecreatetruecolor($newwidth, $newheight);
				//重采样拷贝部分图像并调整大小
				imagecopyresampled($newimage,$s_image,0,0,0,0, $newwidth, $newheight, $image_size[0], $image_size[1]);
				
				$this->outputimage($destination, $newimage, $new_des);
				imagedestroy($newimage);
			
			}
			
		}//end of method post
			$this->load->view ( 'yzrx/test', $data );
	}
	
	function compressimage($source_image, $image_path)
	{
		//获取上传的图片,源图片
		$s_image = $this->createimage($destination);

		$new_des = $upload_url . 'small'.time().$doctor_img ['name'];  //缩略图的命名
		$newwidth = 124;
		$newheight = $image_size[1]*$newwidth/$image_size[0];  //等比例压缩
		$newimage = imagecreatetruecolor($newwidth, $newheight);
		//重采样拷贝部分图像并调整大小
		imagecopyresampled($newimage,$s_image,0,0,0,0, $newwidth, $newheight, $image_size[0], $image_size[1]);
		
		$this->outputimage($destination, $newimage, $new_des);
		imagedestroy($newimage);
	}
	
	/**
	 * 创建图片
	 * @param 图片上传后的路径 $image_path
	 */
	function createimage($image_path)
	{
		$iinfo = getimagesize ( $image_path);
		switch ($iinfo [2]) { //1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,
			case 1 :
				$image_resource = imagecreatefromgif ( $image_path );
				break;
			
			case 2 :
				$image_resource = imagecreatefromjpeg ( $image_path );
				break;
			
			case 3 :
				$image_resource = imagecreatefrompng ( $image_path );
				break;
			
			case 6 :
				$image_resource = imagecreatefromwbmp ( $image_path );
				break;
			default :
				die ( "不支持文件类型" );
				exit ();
		
		}
		return $image_resource;
	}
	/**
	 * 生成图片
	 * @param 图片上传后路径 $image_path
	 * @param 图片 $image_resource
	 * @param 生成图片路径 $output_path
	 */
	function outputimage($image_path, $image_resource, $output_path)
	{
		$iinfo = getimagesize ( $image_path);
		switch ($iinfo [2]) {
				case 1 :
					imagegif ( $image_resource, $output_path ); //以 GIF 格式将图像输出到浏览器或文件
					break;
				
				case 2 :
					imagejpeg ( $image_resource, $output_path );
					break;
				
				case 3 :
					imagepng ( $image_resource, $output_path );
					break;
				
				case 6 :
					imagewbmp ( $image_resource, $output_path );
					break;
			}
	}
	
	function addwaterimg($position, $source_img, $adding_img)
	{
			$simage1 = $this->createimage($logo);
			switch ($position){
				case 1:  //1为左下角,
					$dst_x = 0;
					$dst_y = $image_size[1]-$size[1];
					break;
				case 2:  //2为右下角,
					$dst_x = $image_size[0]-$size[0];
					$dst_y = $image_size[1]-$size[1];
					break;
				case 3: //3为左上角,
					$dst_x = 0;
					$dst_y = 0;
					break;
				case 4: //4为右上角
					$dst_x = $image_size[0]-$size[0];
					$dst_y = 0;
					break;
			}
			imagecopy ( $nimage, $simage1, $dst_x, $dst_y, 0, 0, $size[0], $size[1] );
			imagedestroy ( $simage1 );
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值