PHP圖片添加水印

lpki123 10年前發布 | 5K 次閱讀 PHP jQuery

一共有3個文件:

1、functions.php

2、water.config.php

3、water.class.php

 

代碼如下: 
functions.php

<?php

/*

  • */ //獲得數據庫模型的對象 function M($table){
     return new Model($table);
    
    } //打印信息 function p($msg){ echo "<pre>"; print_r($msg); echo "</pre>"; } //提示錯誤信息 function error($msg){
     echo "<div align='center' style='border:solid 3px #dcdcdc;padding:20px;margin:0 auto;'>
     $msg
     </div>";
     exit;
    
    } //自動加載類文件 function __autoload($className){
     if(strlen($className)>7 && substr($className,-7) == 'Control'){
             $classFile = PATH_APP.'/control/'.$className.'.php';
     }else{
             $classFile = PATH_XL.'/libs/bin/'.$className.'.class.php';
     }
     if(!is_file($classFile)){
             exit($classFile."文件不存在");
     }
     include $classFile;
    
    } //加載、設置配置項 function C($name=null,$value=null){
     static $config =array();
     if(is_array($name)){
             $config = array_merge($config,array_change_key_case($name));
     }
     if(is_string($name)){
             $name = strtolower($name);
             if(!is_null($value)){
                     $congfig[$name]=$value;
                     return true;
             }
             return isset($config[$name])?$config[$name]:null;
     }
     if(is_null($name)){
             return $config;
     }
    
    } //加載、設置字體 function L($name=null,$value=null){
     static $lang = array();
     if(is_array($name)){
             $lang  = array_merge($lang,array_change_key_case($name));
     }
     if(is_string($name)){
             $name = strtolower($name);
             if(!is_null($value)){
                     $lang[$name]=$value;
                     return true;
             }
             return isset($lang[$name])?$lang[$name]:null;
     }
     if(is_null($name)){
             return $lang;
     }
    
    } //獲得文件、文件夾的大小 function getsize($path,$type=null){
     if(is_file($path)) return filesize($path);
     $type = is_null($type)?"*":"*.$type";
     $files = glob($path.'/'.$type);
     $size = 0;
     foreach($files as $f){
             $size+=is_dir($f)?getsize($f):filesize($f);
     }
     return $size;
    
    } ?></pre>
    water.config.php
    <?php
    return array(
     //水印處理配置項
     'water_img' => 'water.jpg',//水印圖片
     'water_pos' => '9',//水印位置  取值范圍:1-9 
     /* 1-9的位置分別為
    
     ↖  ↑  ↗
     ←  ·  →
     ↙  ↓  ↘
    
    */ 'water_pct' => 60,//水印透明度 取值范圍:0-100 (值越大、水印越清晰) ); ?></pre>
    water.class.php
    <?php
    include "functions.php";//加載小工具函數
    C(include "water.config.php");//讀取水印類配置項
    //水印處理類
    class waterd{
     public $water_pos='';//水印位置
     public $water_pct='';//水印透明度
     private $res;//圖像資源
     //構造函數
     function __construct($pos=null,$pct=null){
    
     $this->water_pos = is_null($pos)?C('water_pos'):$pos;
     $this->water_pct = is_null($pct)?C('water_pct'):$pct;
    
    } //添加水印方法 public function water($img){
     //檢測
     if(!$this->check($img)){
         return false;
     }
     //水印圖片資源
     $water_res = $this -> getres(C('water_img'));
     //原始圖片資源
     $img_res = $this -> getres($img);
     //水印位置
     $posArr = $this -> getpos($img_res,$water_res);
     imagecopymerge($img_res,$water_res,$posArr[0],$posArr[1],0,0,imagesx($water_res),imagesy($water_res),$this->water_pct);
     $info = getimagesize($img);
     //打印圖片信息 測試時可開啟當前打印 p($info);
     $func = str_replace('/','',$info['mime']);
     $func($img_res,$img);
    
    } //檢測 private function check($img){
     return is_file($img) && extension_loaded('GD') &&  getimagesize($img);
    
    } //獲得圖片資源 private function getres($img){
     $info = getimagesize($img);
     $type = trim(image_type_to_extension($info[2]),'.');
     $func = 'imagecreatefrom'.$type;
     return $func($img);
    
    } //獲得水印位置 private function getpos($img_res,$water_res){
     $img_x = imagesx($img_res);//原圖寬度
     $img_y = imagesy($img_res);//原圖寬度
     $water_x = imagesx($water_res);//水印寬度
     $water_y = imagesy($water_res);//水印寬度
     $pos = $this -> water_pos;//水印位置
     $x=15;$y=15;
     switch($pos){
         case 1:
             break;
         case 2:
             $x = ($img_x-$water_x)/2;
             break;
         case 3:
             $x = $img_x-$water_x-15;
             break;
         case 4:
             $y = ($img_y-$water_y)/2;
             break;
         case 5:
             $x = ($img_x-$water_x)/2;
             $y = ($img_y-$water_y)/2;
             break;
         case 6:
             $x = $img_x-$water_x-15;
             $y = ($img_y-$water_y)/2;
             break;
         case 7:
             $y = $img_y-$water_y-15;
             break;
         case 8:
             $x = ($img_x-$water_x)/2;
             $y = $img_y-$water_y-15;
             break;
         case 9:
             $x = $img_x-$water_x-15;
             $y = $img_y-$water_y-15;
             break;
     }
     return array($x,$y);
    
    }

} //new一個對象然后 調用 水印的方法 water()即可、傳進要添加水印的圖片即可 $x = new waterd(); $x->water('img.jpg');

?></pre>

 本文由用戶 lpki123 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!