当前位置:网站首页>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 !
边栏推荐
- Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
- Golang through pointer for Range implements the change of the value of the element in the slice
- Word finds red text word finds color font word finds highlighted formatted text
- Go语言 | 03 数组、指针、切片用法
- Fuzor 2020 software installation package download and installation tutorial
- 5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
- 【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
- 数学分析_笔记_第9章:曲线积分与曲面积分
- 在线协作产品哪家强?微软 Loop 、Notion、FlowUs
- 自动化测试的好处
猜你喜欢

The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

5年经验Android程序员面试27天,2022程序员进阶宝典

Windows Oracle open remote connection Windows Server Oracle open remote connection

决策树与随机森林

Why can't Bi software do correlation analysis? Take you to analyze

如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
![2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]](/img/4b/90f07cd681b1e0595fc06c9429b338.jpg)
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]

C# 语言的基本语法结构

Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered

数据库 逻辑处理功能
随机推荐
word如何转换成pdf?word转pdf简单的方法分享!
14、用户、组和权限(14)
Fuzor 2020 software installation package download and installation tutorial
中国银河证券开户安全吗 证券开户
The road of enterprise digital transformation starts from here
5. Data access - entityframework integration
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
Technology sharing | common interface protocol analysis
Tupu software digital twin | visual management system based on BIM Technology
uniapp获取微信头像和昵称
Django uses mysqlclient service to connect and write to the database
Shang Silicon Valley Shang preferred project tutorial release
Hiengine: comparable to the local cloud native memory database engine
HiEngine:可媲美本地的云原生内存数据库引擎
JAD installation, configuration and integration idea
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
Blue sky drawing bed Apple quick instructions
如何在2022年更明智地应用智能合约?
Debezium系列之:修改源码支持drop foreign key if exists fk
UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术