需求:做一个函数,要求可以生成水印图片,水印图片上的水印可以按照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);