以下是【千手觀音】分享的內容全文:
php添加水印的函數,收集ueditor編輯器添加水印的代碼,然后整合在一起,相關的操作如下:
1.打開ueditor目錄下的php目錄下的config.json
1.1
/* 前后端通信相關的配置,注釋只允許使用多行方式 */
{
/* 上傳圖片配置項 */
"iswatermark": "true",/*******************新增圖片水印設置 這里是新增*/
"imageActionName": "uploadsimage", /* 執(zhí)行上傳圖片的action名稱 */
"imageFieldName": "upfile", /* 提交的圖片表單名稱 */
"imageMaxSize": 2048000, /* 上傳大小限制,單位B */
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上傳圖片格式顯示 */
"imageCompressEnable": true, /* 是否壓縮圖片,默認是true */
"imageCompressBorder": 1600, /* 圖片壓縮最長邊限制 */
"imageInsertAlign": "none", /* 插入的圖片浮動方式 */
"imageUrlPrefix": "", /* 圖片訪問路徑前綴 */
"imagePathFormat": "/uploads/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */
//....后面代碼省略
2.打開ueditor目錄下的php目錄下的action_upload.php文件,搜索代碼:
2.1
在“break;”前添加:$watermark =
<?php
/**
* 上傳附件和上傳視頻
* User: Jinqn
* Date: 14-04-09
* Time: 上午10:17
*/
include "uploadser.class.php";
/* 上傳配置 */
$base64 = "uploads";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadsimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize'],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$watermark = $CONFIG['iswatermark']; //************************新增讀取參數
$fieldName = $CONFIG['imageFieldName'];
break;
case 'uploadsscrawl':
// 后面代碼省略2.2
$up = new Uploader($fieldName, $config, $base64);
把它改成:$up = new Uploader($fieldName, $config, $base64, $watermark);
/* 生成上傳實例對象并完成上傳 */ $up = new uploadser($fieldName, $config, $base64,$watermark); // 最后的參數是新增
3.這是最后一步,也是最重要的一步。打開ueditor目錄下的php目錄下的Uploader.class.php文件。
3.1
class uploadser
{
private $water; //是否添加水印(屬性) *******************新增3.2
public function __construct($fileField, $config, $type = "uploads",$watermark = false)
{
$this->water = $watermark;
$this->fileField = $fileField;
$this->config = $config;
$this->type = $type;
if ($type == "remote") {
$this->saveRemote();
} else if($type == "base64") {
$this->upBase64();
} else {
$this->upFile();
}
$this->stateMap['ERROR_TYPE_NOT_ALLOWED'] = iconv('unicode', 'utf-8', $this->stateMap['ERROR_TYPE_NOT_ALLOWED']);
}3.3
public function __construct($fileField, $config, $type = "uploads",$watermark = false)
{
$this->water = $watermark;
$this->fileField = $fileField;
$this->config = $config;
$this->type = $type;3.4
if( $this->water ){
//移動文件
if (!(move_uploadsed_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
//********************新增
if( $this->water ){
$this->watermark($this->filePath,$this->filePath);
}3.5
/*
* 圖片加水印
* $source string 圖片資源
* $target string 添加水印后的名字
* $w_pos int 水印位置 具體看代碼
* $w_img string 水印圖片路徑
* $w_text string 顯示的文字
* $w_font int 字體大小
* $w_color string 字體顏色
*/
private function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = 'byyc.net',$w_font = 10, $w_color = '#CC0000') {
$this->w_img = '../watermark.png';//水印圖片
$this->w_pos = 9;
$this->w_minwidth = 400;//最少寬度
$this->w_minheight = 200;//最少高度
$this->w_quality = 80;//圖像質量
$this->w_pct = 85;//透明度
$w_pos = $w_pos ? $w_pos : $this->w_pos;
$w_img = $w_img ? $w_img : $this->w_img;
if(!$this->check($source)) return false;
if(!$target) $target = $source;
$source_info = getimagesize($source);//圖片信息
$source_w = $source_info[0];//圖片寬度
$source_h = $source_info[1];//圖片高度
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
switch($source_info[2]) { //圖片類型
case 1 : //GIF格式
$source_img = imagecreatefromgif($source);
break;
case 2 : //JPG格式
$source_img = imagecreatefromjpeg($source);
break;
case 3 : //PNG格式
$source_img = imagecreatefrompng($source);
//imagealphablending($source_img,false); //關閉混色模式
imagesavealpha($source_img,true); //設置標記以在保存 PNG 圖像時保存完整的 alpha 通道信息(與單一透明色相反)
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) { //水印圖片有效
$ifwaterimage = 1; //標記
$water_info = getimagesize($w_img);
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2]) {
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
imagealphablending($w_img,false);
imagesavealpha($w_img,true);
break;
default :
return;
}
}else{
$ifwaterimage = 0;
$temp = imagettfbbox(ceil($w_font*2.5), 0, '../../texb.ttf', $w_text); //imagettfbbox返回一個含有 8 個單元的數組表示了文本外框的四個角
$width = $temp[2] - $temp[6];
$height = $temp[3] - $temp[7];
unset($temp);
}
switch($w_pos) {
case 1:
$wx = 5;
$wy = 5;
break;
case 2:
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3:
$wx = $source_w - $width;
$wy = 0;
break;
case 4:
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5:
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6:
$wx = $source_w - $width;
$wy = ($source_h - $height) / 2;
break;
case 7:
$wx = 0;
$wy = $source_h - $height;
break;
case 8:
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 9:
$wx = $source_w - ($width+5);
$wy = $source_h - ($height+5);
break;
case 10:
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
default:
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
............................................................
4.別以為這就完事了,做程序員不但要聰明,而且還要
4.1
遠程抓取圖片時,自動添加水印的方法如下:
在以上的基礎上需要修改三個文件
源文件可以下研究
如果只需要本地上傳添加水印功能,可以查看:http://m.kelkj.com/Technology/install/1633.html
1.軟件源碼推廣展示:目的展示軟件相關功能,接收技術學習者測試、測評;
2.教程課程信息展示:展示課程信息,傳授課程各階段內容;
3.設計素材圖片展示:展示素材設計理念、思維方式、傳播設計理念;
4.福利優(yōu)惠信息展示:分享各類最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學習、參考使用,請勿用于其他用途,如果想商業(yè)使用或者代理,請自行聯(lián)系版權方獲取授權。任何未獲取授權的商業(yè)使用與本站無關,請自行承擔相應責任。
本站不存儲任何資源文件,敬請周知!
如果您認為本頁信息內容侵犯了您的相關權益(包含但不限于:著作權、首發(fā)權、隱私權等權利),或者您認為自己是此信息的權利人但是此信息不是自己發(fā)布的,可以直接版權舉報投訴,我們會根據網站注冊協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護您的合法權益。
本網站采用 BY-NC-SA 協(xié)議進行授權 轉載請注明原文鏈接:ueditor編輯器本地上傳圖片添加水印 遠程抓取圖片添加水印
上一篇:返回列表

侵權舉報/版權申訴


