PHP使用GD函数库创建水印图片+保存本地

需求:做一个函数,要求可以生成水印图片,水印图片上的水印可以按照9个方向自定义摆放,透明度可以随意的调节,要求默认传入两个参数生成水印图,传入四个参数(主图1,主图2,位置,透明度)生成水印并保存本地。

1.php

<?php
	function imgshuiyin($dname,$wname,$pos=7,$tou=50){
		//img(主图1,主图2,位置,透明度)
		function imgclass($name){
			$img_info=getimagesize($name);
			//getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等

			$img_width=$img_info[0];
			$img_height=$img_info[1];
			$img_mime=$img_info['mime'];
			//根据类型来判断资源
			switch ($img_mime) {
				case 'image/jpeg':
				$img=imagecreatefromjpeg($name);
				break;
				case 'image/gif':
				$img=imagecreatefromgif($name);
				break;
				case 'image/png':
				$img=imagecreatefrompng($name);
				break;
				case 'image/wbmp':
				$img=imagecreatefromwbmp($name);
				break;
			}
			return array('width'=>$img_width,'height'=>$img_height,'res'=>$img);
		}
				$dst_info=imgclass($dname);
				$water_info=imgclass($wname);
				//getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等

				$dst_width=$dst_info['width'];
				$dst_height=$dst_info['height'];
				
				$water_width=$water_info['width'];
				$water_height=$water_info['height'];
				$water=$water_info['res'];
				$dst=$dst_info['res'];

	/*
	输出:array(3) { ["width"]=> int(3104) ["height"]=> int(4192) ["mime"]=> string(10) "image/jpeg" }
		  array(3) { ["width"]=> int(1470) ["height"]=> int(1391) ["mime"]=> string(10) "image/jpeg" }
	 */
	switch ($pos) {
		case 1:
			$x=0;
			$y=0;
			break;
		
		case 2:
			$x=$dst_width/2-$water_width/2;
			$y=0;
			break;

		case 3:
			$x=$dst_width-$water_width;
			$y=0;
			break;		

		case 4:
			$x=0;
			$y=$dst_height/2-$water_height/2;
			break;		

		case 5:
			$x=$dst_width/2-$water_width/2;
			$y=$dst_height/2-$water_height/2;
			break;
		case 6:
			$x=$dst_width-$water_height;
			$y=$dst_height/2-$water_height/2;
			break;
		case 7:
			$x=0;
			$y=$dst_height-$water_height;
			break;
		case 8:
			$x=$dst_width/2-$water_height/2;
			$y=$dst_height-$water_height;
			break;
		case 9:
			$x=$dst_width-$water_height;
			$y=$dst_height-$water_height;
			break;

	}
	imagecopymerge($dst,$water,$x,$y,0,0,$water_width,$water_height,$tou);

	header("content-type:image/png");

	imagepng($dst,'1.png');//可以自定义参数imagepng($dst,1.png);会自动生成1.png图像,最好随机生成
	imagedestroy($dst);
	imagedestroy($water);
	}

2.php 

<?php
	include "./1.php";
	imgshuiyin("./img/1.jpg",'./img/2.jpg',100,100);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值