当前位置:网站首页>Kindeditor editor upload image ultra wide automatic compression -php code
Kindeditor editor upload image ultra wide automatic compression -php code
2022-07-03 16:40:00 【Code 8 farmer】
kindeditor It's an open source editor , stay web Website editing is widely used , The built-in editor code can only limit the size , You can't limit the width and height of the picture , Sometimes the picture is too wide and very ugly , This article introduces kindeditor The editor uploads pictures with ultra wide automatic compression , Proportional compression , Directly reduce the size of the picture , Save a space , Speed up image loading .
Find the following file and open ,kindeditor\php\upload_json.php
Find this line of code :
$file_url = $save_url . $new_file_name;
Add the following code after :
compressedImage($_SERVER['DOCUMENT_ROOT'].$file_url,800);// The compressed image
The code above , You can also set the compression quality , See the following functions for specific functions
Then at the bottom of the file , Add the following 2 A function
/**
* desription Proportionally compressed picture author wfuyu.com
* @param sting $imgsrc Picture path
* @param string $imgdst Save path after compression , If it is empty , Coverage
,$zhiliang=90 Picture quality
$wd= If it exceeds this size, it will be compressed proportionally
*/
function compressedImage($imgsrc,$wd=600,$zhiliang=90,$imgdst=''){
if($imgdst==''){$imgdst=$imgsrc;}
list($width, $height, $type) = getimagesize($imgsrc);
$new_width = $width;// The compressed picture is wide
$new_height = $height;// The compressed picture is high
if($width >= $wd){
$per = $wd / $width;// Calculation scale
$new_width = $width * $per;
$new_height = $height * $per;
}
switch ($type) {
case 1:
$giftype = check_gifcartoon($imgsrc);
if ($giftype) {
// header('Content-Type:image/gif');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromgif($imgsrc);
$bg =imagecolorallocate($image_wp, 255, 255, 255);
imagefill($image_wp, 0, 0, $bg);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//90 It represents quality 、 Compressed picture size
imagejpeg($image_wp, $imgdst, $zhiliang);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case 2:
// header('Content-Type:image/jpeg');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($imgsrc);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//90 It represents quality 、 Compressed picture size
imagejpeg($image_wp, $imgdst, $zhiliang);
imagedestroy($image_wp);
imagedestroy($image);
break;
case 3:
//header('Content-Type:image/png');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($imgsrc);
$bg =imagecolorallocate($image_wp, 255, 255, 255);
imagefill($image_wp, 0, 0, $bg);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//90 It represents quality 、 Compressed picture size
imagejpeg($image_wp, $imgdst, $zhiliang);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
function check_gifcartoon($image_file){ $fp = fopen($image_file,'rb');$image_head = fread($fp,1024);fclose($fp);return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;}
边栏推荐
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
- 香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
- Eleven requirements for test management post
- 爱可可AI前沿推介(7.3)
- NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
- Golang 匿名函数使用
- What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
- Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
猜你喜欢
A survey of state of the art on visual slam
Unreal_DataTable 实现Id自增与设置RowName
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
arduino-esp32:LVGL项目(一)整体框架
2022.02.14_ Daily question leetcode five hundred and forty
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
面试官:JVM如何分配和回收堆外内存
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
One article takes you to understand machine learning
What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
随机推荐
[Jianzhi offer] 58 - ii Rotate string left
Cocos Creator 2.x 自动打包(构建 + 编译)
Record windows10 installation tensorflow-gpu2.4.0
Extraction of the same pointcut
ThreeJS 第二篇:顶点概念、几何体结构
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
0214-27100 a day with little fluctuation
2022.02.14_ Daily question leetcode five hundred and forty
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
[Jianzhi offer] 64 Find 1+2+... +n
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
PHP中register_globals参数设置
Deep understanding of grouping sets statements in SQL
Basis of target detection (IOU)
MongoDB 的安装和基本操作
一台服务器最大并发 tcp 连接数多少?65535?
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。