当前位置:网站首页>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 !
边栏推荐
- Blue sky drawing bed Apple quick instructions
- Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
- Hiengine: comparable to the local cloud native memory database engine
- What are the reliable domestic low code development platforms?
- 尚硅谷尚优选项目教程发布
- Windows Oracle open remote connection Windows Server Oracle open remote connection
- 618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
- Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
- 跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
- Password reset of MariaDB root user and ordinary user
猜你喜欢

Fuzor 2020軟件安裝包下載及安裝教程
![[performance test] jmeter+grafana+influxdb deployment practice](/img/32/f07792734d040829398a90a2040146.png)
[performance test] jmeter+grafana+influxdb deployment practice

HiEngine:可媲美本地的云原生内存数据库引擎

14、用户、组和权限(14)

Hiengine: comparable to the local cloud native memory database engine

Talking about fake demand from takeout order

数据库 逻辑处理功能

微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知

国内低代码开发平台靠谱的都有哪些?

#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
随机推荐
Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
R language uses lubridate package to process date and time data
HAC集群修改管理员用户密码
Startup and shutdown of CDB instances
The easycvr authorization expiration page cannot be logged in. How to solve it?
Ultrasonic ranging based on FPGA
Redhat7.4 configure Yum software warehouse (rhel7.4)
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
A cloud opens a new future of smart transportation
The monthly list of Tencent cloud developer community videos was released in May 2022
MySql中的longtext字段的返回问题及解决
国内低代码开发平台靠谱的都有哪些?
Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
5. Data access - entityframework integration
HAC cluster modifying administrator user password
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
The problem of returning the longtext field in MySQL and its solution
Mysql database indexing tutorial (super detailed)