php 圖片的水印效果

這裏我們實現一種水印的效果,微博,淘寶挺多這樣的 於是就嘗試寫了一個方法

這裏我們寫代碼:(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');
發佈了42 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章