php 图片的水印效果

来源:互联网 发布:闹钟备忘录软件 编辑:程序博客网 时间:2024/05/16 12:37

这里我们实现一种水印的效果,微博,淘宝挺多这样的 于是就尝试写了一个方法

这里我们写代码:(postion是水印的位置,9个位置,不在之间随机存在)

<?php/**@prame resource $bigImage*@prame resource $smallImage*@prame string $type;*@prame string $path;*@prame bool $isfileName*@prame int $postion *@prame int $apahle***/function shuiyin($bigImage,$smallImage,$type ='png',$path = "path",$isfileName = true,$postion =5,$apahle=100){    //打开两个图片    $bigImageRes = open($bigImage);    $smallImageRes = open($smallImage);    //获取大图和小图的信息来为水印的位置做准备    $bigImageInfo = getimagesize($bigImage);    $smallImageInfo = getimagesize($smallImage);    //位置    switch($postion){        case 1:            $x = 0;            $y = 0;            break;        case 2:            $x = ($bigImageInfo[0]-$smallImageInfo[0])/2;            $y = 0;            break;        case 3:            $x = $bigImageInfo[0]-$smallImageInfo[0];            $y = 0;            break;        case 4:            $x = 0;            $y = ($bigImageInfo[1]-$smallImageInfo[1])/2;            break;        case 5:            $x = ($bigImageInfo[0]-$smallImageInfo[0])/2;            $y = ($bigImageInfo[1]-$smallImageInfo[1])/2;            break;        case 6:            $x = $bigImageInfo[0]-$smallImageInfo[0];            $y = ($bigImageInfo[1]-$smallImageInfo[1])/2;            break;        case 7:            $x = 0;            $y= $bigImageInfo[1]-$smallImageInfo[1];            break;        case 8:            $x = ($bigImageInfo[0]-$smallImageInfo[0])/2;            $y = $bigImageInfo[1]-$smallImageInfo[1];            break;        case 9:            $x = $bigImageInfo[0]-$smallImageInfo[0];            $y = $bigImageInfo[1]-$smallImageInfo[1];            break;        default :            $x = mt_rand(0,$bigImageInfo[0]-$smallImageInfo[0]);            $y = mt_rand(0,$bigImageInfo[1]-$smallImageInfo[1]);            break;    }    //合并图片    imagecopymerge($bigImageRes,$smallImageRes,$x,$y,0,0,$smallImageInfo[0],$smallImageInfo[1],$apahle);    //文件随机    if($isfileName){        $name = uniqid().'.'.$type;    }else{        $info = pathinfo($path);        $name = $info['filename'].'.'.$type;    }    $path = rtrim($path,'/').'/'.$name;    //输出图片    $func = 'image'.$type;    if(!function_exists($func)){        return false;    }    $func($bigImageRes,$path);    imagedestroy($bigImageRes);    imagedestroy($smallImageRes);    return $path;}function open($path){    //判断文件是否存在    if(!file_exists($path)){        return '不存在';    }    $info = getimagesize($path);    switch($info['mime']){        case 'image/jpg':        case 'image/jpeg':        case 'image/jpe':        case 'image/pjpeg':            $res = imagecreatefromjpeg($path);        break;        case 'image/png':            $res = imagecreatefrompng($path);            break;        case 'image/gif':            $res = imagecreatefromgif($path);            break;    }    return $res;}shuiyin('big.jpg','small.jpg');
原创粉丝点击