当前位置:网站首页>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 !
边栏推荐
- 华律网牵手观测云,上线系统全链路可观测平台
- 手把手教你处理 JS 逆向之图片伪装
- 如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
- #夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
- 彻底理解为什么网络 I/O 会被阻塞?
- Go语言学习教程(十五)
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- PG基础篇--逻辑结构管理(用户及权限管理)
- Go语言学习教程(十六)
- The easycvr authorization expiration page cannot be logged in. How to solve it?
猜你喜欢
618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
Mysql database indexing tutorial (super detailed)
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
手把手教你处理 JS 逆向之图片伪装
UDF implementation of Dameng database
随机推荐
出海十年:新旧接力,黑马崛起
从零实现深度学习框架——LSTM从理论到实战【实战】
彻底理解为什么网络 I/O 会被阻塞?
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
After the company went bankrupt, the blackstones came
Debezium系列之:修改源码支持drop foreign key if exists fk
Go语言 | 03 数组、指针、切片用法
Redhat7.4 configure Yum software warehouse (rhel7.4)
2022最新大厂Android面试真题解析,Android开发必会技术
JAD installation, configuration and integration idea
JMeter 常用的几种断言方法,你会了吗?
什么是面上项目
Go语言 | 01 WSL+VSCode环境搭建避坑指南
Ultrasonic ranging based on FPGA
MMO项目学习一:预热
How to realize the Online timer and offline timer in the game
华律网牵手观测云,上线系统全链路可观测平台
公司破产后,黑石们来了
uniapp获取微信头像和昵称
UDF implementation of Dameng database