当前位置:网站首页>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 !
边栏推荐
- How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
- [detailed explanation of AUTOSAR 14 startup process]
- #夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
- Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- 在线协作产品哪家强?微软 Loop 、Notion、FlowUs
- Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- 5. Data access - entityframework integration
- Django uses mysqlclient service to connect and write to the database
猜你喜欢
Mariadb root用户及普通用户的密码 重置
HiEngine:可媲美本地的云原生内存数据库引擎
Reflection and imagination on the notation like tool
Go deep into the underlying C source code and explain the core design principles of redis
【AI 框架基础技术】自动求导机制 (Autograd)
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Hiengine: comparable to the local cloud native memory database engine
Hiengine: comparable to the local cloud native memory database engine
国内低代码开发平台靠谱的都有哪些?
决策树与随机森林
随机推荐
国内低代码开发平台靠谱的都有哪些?
HiEngine:可媲美本地的云原生内存数据库引擎
关于 Notion-Like 工具的反思和畅想
Is it safe for China Galaxy Securities to open an account? Securities account opening
android中常见的面试题,2022金九银十Android大厂面试题来袭
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
How to convert word into PDF? Word to PDF simple way to share!
Talking about fake demand from takeout order
泰山OFFICE技术讲座:由行的布局高度,谈绘制高度的高度溢出、高度缩水(全网首发)
尚硅谷尚优选项目教程发布
Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
A cloud opens a new future of smart transportation
Go语言 | 02 for循环及常用函数的使用
golang通过指针for...range实现切片中元素的值的更改
14、用户、组和权限(14)
Ultrasonic ranging based on FPGA
Fundamentals of machine learning (III) -- KNN / naive Bayes / cross validation / grid search
如何在2022年更明智地应用智能合约?
Fuzor 2020軟件安裝包下載及安裝教程