「PHP」GD库实现图片水印及缩略图

类的封装

<?php
class Image{
	private $image;		//内存中的图片
	private $info;		//图片的基本信息

//打开图片
	public function __construct($filepath){
		$info = getimagesize($filepath);
		$this->info=array(
			'width'=>$info[0],
			'height'=>$info[1],
			'type'=>image_type_to_extension($info['2'],false),
			'mime'=>$info['mime']
		);
		$fun = "imagecreatefrom{$this->info['type']}";
		$this->image=$fun($filepath);
	}

//操作图片
	/**
	 * thumbnail 缩略图
	 * @param   $xmax 设置缩放的宽
	 * @param   $ymax 设置缩放的高
	 */
	public function thumbnail($xmax,$ymax){
		//获取原图的高度和宽度
		$x = imagesx($this->image);
		$y = imagesy($this->image);
        
        //限制只能缩小
		if($x <= $xmax && $y <= $ymax){
			return $this->image;
		}

		//按比例缩小
		if($x >= $y) {
				$newx = $xmax;
				$newy = $newx * $y / $x;
		}else {
				$newy = $ymax;
				$newx = $x / $y * $newy;
		}
		//创建新图像
		$newimage = imagecreatetruecolor($newx, $newy);
		imagecopyresized($newimage, $this->image, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
		$this->image = $newimage;
	}

  /**
   * waterMark 添加图片水印
   * @param   $waterMark 	水印图片路径
   * @param   $alpha      透明度
   */
	public function waterMark($waterMark, $alpha = 0){
		$info = getimagesize($waterMark);
		$type = image_type_to_extension($info[2],false);
		$funMark = "imagecreatefrom{$type}";
		$water = $funMark($waterMark);

		//获取image和water的宽高
    $image_x = imagesx($this->image);
    $image_y = imagesy($this->image);
    $water_x = imagesx($water);
    $water_y = imagesy($water);
    //水印定位到右下角
    $x = $image_x - $water_x;
    $y = $image_y - $water_y;
			$this->imagecopymerge_alpha($this->image, $water, $x, $y, 0, 0, $info[0], $info[1], $alpha);
		// }
		imagedestroy($water);
	}
// 输出图片
	/**
	 * show 在浏览器中输出图片
	 */
	public function show(){
		header("Content-type:".$this->info['mime']);
		$funs = "image{$this->info['type']}";
		$funs($this->image);
	}
	/**
	 * save 把图片保存在硬盘里
	 * @param   $newname 图片命名
	 */
	public function save($newname){
		$funs = "image{$this->info['type']}";
		$funs($this->image,$newname.'.'.$this->info['type']);
	}

	public function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
			$opacity=$pct;
			// getting the watermark width
			$w = imagesx($src_im);
			// getting the watermark height
			$h = imagesy($src_im);

			// creating a cut resource
			$cut = imagecreatetruecolor($src_w, $src_h);
			// copying that section of the background to the cut
			imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
			// inverting the opacity
			$opacity = 100 - $opacity;

			// placing the watermark now
			imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
			imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);
	}
//销毁图片
	public function __destruct(){
		imagedestroy($this->image);
	}

 }
 ?>

实现图片水印

<?php
//引入Iamge类文件
include 'image.class.php';

$filepath = 'image.jpg';
$image = new Image($filepath);
//60是透明度参数默认设置为0
$image->waterMark('shuiyin.png', 60);
$image->show();
$image->save('sc_shuiyintu');

注释掉上面代码,然后实现缩略图

<?php
//引入Iamge类文件
include 'image.class.php';
//图片水印
    // $filepath = 'image.jpg';
    // $image = new Image($filepath);
    // $image->waterMark('shuiyin.png', 60);
    // $image->show();
    // $image->save('sc_shuiyintu');

//缩略图
    $filepath = 'image.jpg';
    $image = new Image($filepath);
    $image->thumbnail(600, 300);
    $image->show();
    $image->save('sc_suoluetu');

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值