欢迎您来到,李雷博客 | PHP博客        登录  |  注册

PHP用imagerotate旋转图片和等比缩放压缩、添加水印

更新:2023-02-28 10:08:20
人气:746
来源:本站原创
A+

本文介绍PHP用imagerotate旋转图片和缩放压缩、添加水印方法。思路就是我要实现,先判断一下图片的宽和高的比例,如果是高大于宽,则逆时针旋转90度,同时再判断一下是否超过我的限制宽度或高度,如果超过就等比压缩,再添加水印。

下面是具体应用PHP代码:

include_once("new_image.class.php");

$article_suofang='1';
$article_shuiyin='1';

//超宽尺寸自动缩放 $var_article_suofang='1';//发布文章是否将内容中的图片自动缩放到指定宽度以内,1是,0否
if ($article_suofang=='1'){
    
    @list($width,$height)=@getimagesize($filePath);
    
    //判断是竖版时才做横向调整
    if ($height>$width){
        $imageSize = getimagesize($filePath);
        $imageSize = explode('/', $imageSize['mime']);
        $type = $imageSize[1];

        switch ($type) {
            case "png":
                $image = imagecreatefrompng($filePath);
                break;
            case "jpeg":
                $image = imagecreatefromjpeg($filePath);
                break;
            case "jpg":
                $image = imagecreatefromjpeg($filePath);
                break;
            case "gif":
                $image = imagecreatefromgif($filePath);
                break;
        }
        $rotateImage = imagerotate($image, 90, 0); //逆时90针旋转
        //获取旋转后的宽高
        $width = imagesx($rotateImage);
        $height = imagesy($rotateImage);
        
        imagejpeg($rotateImage, $filePath);
        imagedestroy($rotateImage);
    }
        
    //图片存档结束,准备判断是否要压缩图片尺寸
    if ($width>1100 || $height>700){  //宽度超过1100或,高度超过700,才按指定的宽高比例缩放
        $image=new image($filePath,1, "1100", "700" ,$filePath);
        $image->outimage();
    }
    
}

//水印,图片处理完最后执行 $var_article_shuiyin='1';//发布文章是否将内容中的图片自动添加水印,1是,0否
if ($article_shuiyin=='1'){
    @list($width,$height)=@getimagesize($filePath);
    if ($width>396 && $height>249){  //宽度超过396且,高度超过249,才添加水印,0为水印随机,大小判断与水印图片大小有关
        $image=new image($filePath, 3, "fapiao_img/shuiyin.png", "0",$filePath);
        $image->outimage();
    }
}

new_image.class.php文件代码:

<?php
/*已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明。用imagecreate函数创建画布可以解决这个问题,但是缩放出来的图片色数太少了
 *
 *
 *type值:
 * (1):代表使用图片缩放功能,此时,$value1代表缩放后图片的宽度,$value2代表缩放后图片的高度
 * (2):代表使用图片裁剪功能,此时,$value1代表裁剪开始点的坐标,例:从原点开始即是“0,0”前面是x轴后面是y轴,中间用,分隔,$value2代表裁剪的宽度和高度,同样也是“20,20”的形式使用
 * (3):代表使用加图片水印功能,此时,$value1代表水印图片的文件名,$value2代表水印在图片中的位置,有10值个可以选,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表随机位置
 
 
 	$image=new image("1.jpg", 1, "300", "400", "11.jpg");   //使用图片缩放功能    原图,1缩放,最宽,最高,新图位置  (其中,最高或最宽任一超过即等比缩放)
	$image=new image("1.jpg", 2, "0,0", "200,200", "12.jpg"); //使用图片裁剪功能   原图,2裁剪,起始坐标XY(左上角开始),大小XY,新图位置
	$image=new image("1.jpg", 3, "12.jpg", "9", "13.jpg");   //使用加图片水印功能 原图,3水印,水印图片(水印大小取决此图大小),位置9右下0随机,新图位置
	$image->outimage();
	
	
 *
 */

class image{
	private $types;	//使用的功能编号,1为图片缩放功能  2为图片裁剪功能   3,为图片加图片水印功能
	private $imgtype;//图片的格式
	private $image;	//图片资源
	private $width;//图片宽度
	private $height;//图片高度
	private $value1;//根据所传type值的不同,$value1分别代表不同的值
	private $value2;//根据所传type值的不同,$value2分别代表不同的值
	private $endaddress;//输出后的地址+文件名
	

	function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
		$this->types=$types;
		$this->image=$this->imagesources($imageaddress);//获取不到
		$this->width=$this->imagesizex();
		$this->height=$this->imagesizey();
		$this->value1=$value1;
		$this->value2=$value2;
		$this->endaddress=$endaddress;
	}
	

	function outimage(){	//根据传入type值的不同,输出不同的功能
		switch($this->types){
			case 1:
				$this->scaling();
				break;
			case 2:
				$this->clipping();
				break;
			case 3:
				$this->imagewater();
				break;
			default:
				return false;

		}
	}

	private function imagewater(){	//加图片水印功能
		//用函数获取水印文件的长和宽
		$imagearrs=$this->getimagearr($this->value1);
		//调用函数计算出水印加载的位置
		$positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]);
		//加水印
		imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]);
		//调用输出方法保存
		$this->output($this->image);
	}

	private function clipping(){	//图片裁剪功能
		//将传进来的值分别赋给变量
		list($src_x, $src_y)=explode(",", $this->value1);
		list($dst_w, $dst_h)=explode(",", $this->value2);
		if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){	//这个判断就是限制不能截取到图片外面去
			return false;
		}		
		//创建新的画布资源
		$newimg=imagecreatetruecolor($dst_w, $dst_h);
		//进行裁剪
		imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
		//调用输出方法保存
		$this->output($newimg);			
	}

	private function scaling(){	//图片缩放功能
		//获取等比缩放的宽和高
		$this-> proimagesize();
		//根据参数进行缩放,并调用输出函数保存处理后的文件
		$this->output($this->imagescaling());
	}

	private function imagesources($imgad){	//获取图片类型并打开图像资源
	
		
		
		$imagearray=$this->getimagearr($imgad);
		
		
		
		switch($imagearray[2]){
			case 1://gif
				$this->imgtype=1;
				$img=imagecreatefromgif($imgad);
				break;
			case 2://jpeg
				$this->imgtype=2;
				$img=imagecreatefromjpeg($imgad);
				break;
			case 3://png
				$this->imgtype=3;
				$img=imagecreatefrompng($imgad);
				break;
			default:
				return false;
		}
		
		return $img;
		
		
	}

	private function imagesizex(){	//获得图片宽度
		return imagesx($this->image);
	}

	private function imagesizey(){	//获取图片高度
		return imagesy($this->image);
	}

	private function proimagesize(){	//计算等比缩放的图片的宽和高
		if($this->value1 && ($this->width < $this->height)) {	//等比缩放算法
			$this->value1=round(($this->value2/ $this->height)*$this->width);
		}else{
			$this->value2=round(($this->value1/ $this->width) * $this->height);
		}
	}

	private function imagescaling(){//图像缩放功能,返回处理后的图像资源
		$newimg=imagecreatetruecolor($this->value1, $this->value2);
		
		$tran=imagecolortransparent($this->image);//处理透明算法
		if($tran >= 0 && $tran < imagecolorstotal($this->image)){
			$tranarr=imagecolorsforindex($this->image, $tran);
			$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
			imagefill($newimg, 0, 0, $newcolor);
			imagecolortransparent($newimg, $newcolor);
		}
   
		imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height);
		return $newimg;
	}

	private function output($image){//输出图像
	
		echo $this->imgtype;
	

		switch($this->imgtype){
			case 1:
				imagegif($image, $this->endaddress);//参数可能是两个 ($image, $this->endaddress,99)
				break;
			case 2:
				imagejpeg($image, $this->endaddress);
				break;
			case 3:
				imagepng($image, $this->endaddress);//$quality参数取值范围0-99 在php 5.1.2之后变更为0-9
				break;
			default:
				return false;
		}
	}

	private function getimagearr($imagesou){//返回图像属性数组方法
		return getimagesize($imagesou);
	}

	private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高
		switch($num){
			case 1:
				$positionarr[0]=0;
				$positionarr[1]=0;
				break;
			case 2:
				$positionarr[0]=($this->width-$width)/2;
				$positionarr[1]=0;
				break;
			case 3:
				$positionarr[0]=$this->width-$width;
				$positionarr[1]=0;
				break;
			case 4:
				$positionarr[0]=0;
				$positionarr[1]=($this->height-$height)/2;
				break;
			case 5:
				$positionarr[0]=($this->width-$width)/2;
				$positionarr[1]=($this->height-$height)/2;
				break;
			case 6:
				$positionarr[0]=$this->width-$width;
				$positionarr[1]=($this->height-$height)/2;
				break;
			case 7:
				$positionarr[0]=0;
				$positionarr[1]=$this->height-$height;
				break;
			case 8:
				$positionarr[0]=($this->width-$width)/2;
				$positionarr[1]=$this->height-$height;
				break;
			case 9:
				$positionarr[0]=$this->width-$width;
				$positionarr[1]=$this->height-$height;
				break;
			case 0:
				$positionarr[0]=rand(0, $this->width-$width);
				$positionarr[1]=rand(0, $this->height-$height);
				break;
		}
		return $positionarr;
	}

	function __destruct(){
		imagedestroy($this->image);
	}
	

}

?>

$filePath就是图片的路径,我是做的压缩、旋转和添加水印都是原图覆盖。

推荐的文章
# 发表我的评论
  /     /  
# 最近评论
暂时还没有评论,要不要说点什么?
  Ads by Google
  联系博主
Hello,本博客系统采用PHP和MySql开发,程序开发完全是因为个人爱好,是自己纯手写PHP源代码,未采用任何PHP框架!
QQ:858353007   微信号:lileihot123
网站地图
会员服务
关于我们
QQ:858353007
 
广告服务
加我微信
移动端访问
 
 
Copyright © 2014- 2024 www.mdaima.com All Rights Reserved.
李雷博客,专注PHP经验、PHP教程及PHP源代码开源下载分享的PHP博客!   ICP备案号:京ICP备10202169号-4