当前位置:网站首页>PHP uses ueditor to upload pictures and add watermarks
PHP uses ueditor to upload pictures and add watermarks
2022-07-05 19:16:00 【1024 questions】
When the blog uploads pictures , I hope to put a watermark on my blog link , Nuggets ,csdn It's all done , I have to learn about it .
It's OK to upload pictures at ordinary times , When editing the article , Use ueditor Uploading pictures with watermark needs to be modified ueditor part PHP Source code , Let me make a general record here .
Start by opening php Under folder Uploader.class.php
find private function upFile(), This is the main processing method for uploading files ,
find 122 That's ok : $this->stateInfo = $this->stateMap[0];
Add :$this->imageWaterMark($this->filePath,9,'logo.png');
imageWaterMark It's a custom function ,
I'll talk about ,$this->filePath This is the path to upload pictures ,9 Indicates the location of the watermark , In the lower right corner ,logo.png This is the watermark image you want to add , This is in the same directory /php/, If you want to put it in another path, please use a relative path .
The following is the custom function , Add to Uploader Class All parameters have been described in the function comments , You need to pay attention when calling
/* * function :PHP Image watermarking ( Watermarks support pictures or text ) * Parameters : *$groundImage Background image , That is, the image that needs to be watermarked , Only support temporarily GIF,JPG,PNG Format ; *$waterPos Watermark location , Yes 10 States ,0 Random location ; *1 Left for the top ,2 Center the top ,3 For the top right ; *4 Left for the middle ,5 Center for the middle ,6 To the right of the middle ; *7 For the bottom left ,8 Center the bottom end ,9 To the right of the bottom ; *$waterImage Image watermarking , That is, the image as a watermark , Only support temporarily GIF,JPG,PNG Format ; *$waterText Text watermarking , That is, take the text as a watermark , Support ASCII code , No Chinese support ; *$textFont Text size , The value is 1、2、3、4 or 5, The default is 5; *$textColor Text color , Value is hexadecimal color value , The default is #FF0000( Red ); * Be careful :Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG *$waterImage and $waterText It's better not to use , Choose one of them , priority of use $waterImage. * When $waterImage Is valid , Parameters $waterString、$stringFont、$stringColor None of them are effective . * The file name and $groundImage equally . */ private function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000") { $isWaterImage = FALSE; $formatMsg = " The file format is not supported at this time , Please use image processing software to convert the image into GIF、JPG、PNG Format ."; // Read watermark file if(!empty($waterImage) && file_exists($waterImage)) { $isWaterImage = TRUE; $water_info = getimagesize($waterImage); $water_w = $water_info[0];// Get the width of the watermark image $water_h = $water_info[1];// Get the height of the watermark image switch($water_info[2])// Get the format of the watermark image { case 1:$water_im = imagecreatefromgif($waterImage);break; case 2:$water_im = imagecreatefromjpeg($waterImage);break; case 3:$water_im = imagecreatefrompng($waterImage);break; default:die($formatMsg); } } // Read the background picture if(!empty($groundImage) && file_exists($groundImage)) { $ground_info = getimagesize($groundImage); $ground_w = $ground_info[0];// Get the width of the background image $ground_h = $ground_info[1];// Get the height of the background picture switch($ground_info[2])// Get the format of the background picture { case 1:$ground_im = imagecreatefromgif($groundImage);break; case 2:$ground_im = imagecreatefromjpeg($groundImage);break; case 3:$ground_im = imagecreatefrompng($groundImage);break; default:die($formatMsg); } } else { die(" The image that needs to be watermarked does not exist !"); } // Watermark location if($isWaterImage)// Image watermarking { $w = $water_w; $h = $water_h; $label = " The image "; } else// Text watermarking { $temp = imagettfbbox(ceil($textFont*5),0,"./cour.ttf",$waterText);// Get used TrueType The range of text for the font $w = $temp[2] - $temp[6]; $h = $temp[3] - $temp[7]; unset($temp); $label = " Text area "; } if( ($ground_w<$w) || ($ground_h<$h) ) { echo " The length or width of the image to be watermarked is larger than the watermark ".$label." Still small , Unable to generate watermark !"; return; } switch($waterPos) { case 0:// Random $posX = rand(0,($ground_w - $w)); $posY = rand(0,($ground_h - $h)); break; case 1://1 Left for the top $posX = 0; $posY = 0; break; case 2://2 Center the top $posX = ($ground_w - $w) / 2; $posY = 0; break; case 3://3 For the top right $posX = $ground_w - $w; $posY = 0; break; case 4://4 Left for the middle $posX = 0; $posY = ($ground_h - $h) / 2; break; case 5://5 Center for the middle $posX = ($ground_w - $w) / 2; $posY = ($ground_h - $h) / 2; break; case 6://6 To the right of the middle $posX = $ground_w - $w; $posY = ($ground_h - $h) / 2; break; case 7://7 For the bottom left $posX = 0; $posY = $ground_h - $h; break; case 8://8 Center the bottom end $posX = ($ground_w - $w) / 2; $posY = $ground_h - $h; break; case 9://9 To the right of the bottom $posX = $ground_w - $w - 10; // -10 It's the distance to the right 10px You can adjust yourself $posY = $ground_h - $h - 10; // -10 It's the distance from the bottom 10px You can adjust yourself break; default:// Random $posX = rand(0,($ground_w - $w)); $posY = rand(0,($ground_h - $h)); break; } // Set the color mixing mode of the image imagealphablending($ground_im, true); if($isWaterImage)// Image watermarking { imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);// Copy watermark to target file } else// Text watermarking { if( !emptyempty($textColor) && (strlen($textColor)==7) ) { $R = hexdec(substr($textColor,1,2)); $G = hexdec(substr($textColor,3,2)); $B = hexdec(substr($textColor,5)); } else { die(" Watermark text color format is incorrect !"); } imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B)); } // Image after watermark generation @unlink($groundImage); switch($ground_info[2])// Get the format of the background picture { case 1:imagegif($ground_im,$groundImage);break; case 2:imagejpeg($ground_im,$groundImage);break; case 3:imagepng($ground_im,$groundImage);break; default:die($errorMsg); } // Free memory if(isset($water_info)) unset($water_info); if(isset($water_im)) imagedestroy($water_im); unset($ground_info); imagedestroy($ground_im); }
All right. , This is easy to use . It's easy to upload pictures with watermarks .
Please note that : The watermark function requires GD Library module , Please check php Is... Installed GD Library module .
This is about PHP utilize ueditor That's all for the article about uploading pictures and adding watermarks , More about PHP Upload pictures and add watermark content. Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Tupu software digital twin | visual management system based on BIM Technology
- Fuzor 2020软件安装包下载及安装教程
- Go deep into the underlying C source code and explain the core design principles of redis
- vagrant2.2.6支持virtualbox6.1版本
- Talking about fake demand from takeout order
- 机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
- The monthly list of Tencent cloud developer community videos was released in May 2022
- HiEngine:可媲美本地的云原生内存数据库引擎
- Oracle 中文排序 Oracle 中文字段排序
- 【AI 框架基础技术】自动求导机制 (Autograd)
猜你喜欢
Fuzor 2020软件安装包下载及安装教程
决策树与随机森林
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
After the company went bankrupt, the blackstones came
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
UDF implementation of Dameng database
块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
MMO项目学习一:预热
Shang Silicon Valley Shang preferred project tutorial release
随机推荐
关于 Notion-Like 工具的反思和畅想
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
Teach you to deal with JS reverse picture camouflage hand in hand
MySql中的longtext字段的返回问题及解决
Ten years at sea: old and new relay, dark horse rising
14、用户、组和权限(14)
从外卖点单浅谈伪需求
Mysql如何对json数据进行查询及修改
数学分析_笔记_第9章:曲线积分与曲面积分
shell编程基础(第9篇:循环)
国内低代码开发平台靠谱的都有哪些?
R语言使用lubridate包处理日期和时间数据实战
100million single men and women supported an IPO with a valuation of 13billion
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
PHP利用ueditor实现上传图片添加水印
China law network joins hands to observe the cloud, and the online system is a full link observable platform
The monthly list of Tencent cloud developer community videos was released in May 2022
Hiengine: comparable to the local cloud native memory database engine
Windows Oracle open remote connection Windows Server Oracle open remote connection
Word finds red text word finds color font word finds highlighted formatted text