pbootcms縮略圖裁剪優化居中裁剪
pbootcms默認的圖片裁剪是從左上角開始裁剪,使用width,height,maxwidth,maxheight等參數進行調節。
但是裁剪出來的圖片始終感覺不是那么令人滿意。(也許是我不會用)
比如多人物合影,PB默認的裁剪是左上,那么這個裁剪就會丟掉C位,那怎么能行。
今天來優化一下裁剪圖片,讓他能簡單快捷的裁剪出理想的圖片。
我想要的效果是不論橫圖豎圖都是居中裁剪。
翠花,上干貨!
解決方案
首先找到裁剪縮略圖的方法,
文件位置:/core/function/file.php
搜索:function cut_img,大約在447行
對cut_img方法進行優化,直接上代碼:
01
// 剪切圖片
02
function cut_img($src_image, $out_image = null, int $new_width = null, int $new_height = null, $img_quality = 90)
03
{
04
// 輸出地址
05
if (! $out_image)
06
$out_image = $src_image;
07
08
// 讀取配置文件設置
09
if (! $new_width && ! $new_height)
10
return;
11
12
// 獲取圖片屬性
13
list ($width, $height, $type, $attr) = getimagesize($src_image);
14
switch ($type) {
15
case 1:
16
$img = imagecreatefromgif($src_image);
17
break;
18
case 2:
19
$img = imagecreatefromjpeg($src_image);
20
break;
21
case 3:
22
$img = imagecreatefrompng($src_image);
23
break;
24
}
25
26
// 不限定是等比例縮放
27
if (! $new_width) {
28
$new_width = floor($width * ($new_height / $height));
29
}
30
if (! $new_height) {
31
$new_height = floor($height * ($new_width / $width));
32
}
33
// 創建畫布
34
$new_img = imagecreatetruecolor($new_width, $new_height);
35
36
// 創建透明畫布,避免黑色
37
if ($type == 1 || $type == 3) {
38
$color = imagecolorallocate($new_img, 255, 255, 255);
39
imagefill($new_img, 0, 0, $color);
40
imagecolortransparent($new_img, $color);
41
}
42
43
44
// 先縮放
45
$scale = max($new_width / $width, $new_height / $height);
46
$scale_width = floor($scale * $width);
47
$scale_height = floor($scale * $height);
48
$scale_img = imagecreatetruecolor($scale_width, $scale_height); // 創建畫布
49
if(function_exists("ImageCopyResampled")) {
50
imagecopyresampled($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
51
} else {
52
imagecopyresized($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
53
}
54
//再裁剪
55
$start_x = ($scale_width - $new_width) / 2;
56
$start_y = ($scale_height - $new_height) / 2;
57
58
//拷貝剪切的圖像數據到畫板,生成剪切圖像
59
imagecopy($new_img, $scale_img, 0, 0, $start_x, $start_y, $scale_width, $scale_height);
60
61
check_dir(dirname($out_image), true); // 檢查輸出目錄
62
63
switch ($type) {
64
case 1:
65
imagegif($new_img, $out_image, $img_quality);
66
break;
67
case 2:
68
imagejpeg($new_img, $out_image, $img_quality);
69
break;
70
case 3:
71
imagepng($new_img, $out_image, $img_quality / 10); // $quality參數取值范圍0-99 在php 5.1.2之后變更為0-9
72
break;
73
default:
74
imagejpeg($new_img, $out_image, $img_quality);
75
}
76
imagedestroy($new_img);
77
imagedestroy($img);
78
return true;
79
}
小伙伴可以直接復制替換該方法。
使用方法
1
{pboot:list scode=*}
2
"[list:link]">"[list:ico width=600 height=400]" />
3
{/pboot:list}
如上代碼,直接生成600*400像素的居中裁剪過的圖片。
收工。
舉個例子:列表中輸出圖片
標題:pbootcms縮略圖裁剪優化居中裁剪
網址:http://www.hemadj.cn/504.html
作者:合肥良馬網絡科技有限公司 版權所有,轉載請注明出處,并以鏈接形式注明。
- 合肥小程序開發制作需要多少錢
- 合肥注冊微信公眾號多少錢
- 合肥小程序開發公司這么多應該怎···
- 合肥微信公眾號(訂閱號和服務號···
- 合肥網絡公司告訴你軟文外鏈該怎···
- 合肥良馬網絡公司簽約安徽潤恩特···
- 合肥網站SEO優化推廣是怎么收···
- 想找一家靠譜的本地合肥網站制作···
- 合肥網站推廣軟文,怎么通過軟文···
- 合肥企業百家號運營服務之基礎套···
- 合肥公眾號代運營需要多少錢
- 合肥微信公眾號運營多少錢
- 怎么樣通過微信公眾號代運營進行···
- 合肥微信服務號運營多少錢
- 合肥小程序開發公司應該怎么樣來···
- 合肥小程序開發做個花店小程序要···
- 打開網站絕對路徑出現404錯誤···
- 揚子電熱水器
- 延旭燒烤
- 肥西縣肥西大光明眼鏡店
- 合肥微信公眾號(訂閱號和服務號···
- 合肥網絡公司告訴你軟文外鏈該怎···
- 合肥良馬網絡公司簽約安徽潤恩特···
- 合肥網站SEO優化推廣是怎么收···
- 想找一家靠譜的本地合肥網站制作···
- 合肥網站推廣軟文,怎么通過軟文···
- 合肥企業百家號運營服務之基礎套···
- 合肥注冊微信公眾號多少錢
- 合肥小程序開發制作需要多少錢
- 合肥小程序開發公司這么多應該怎···
- 打開網站絕對路徑出現404錯誤···
- 企業網絡運營推廣應該選擇哪些渠···
- 當下互聯網到底在缺少什么?網絡···
- 保險行業的網絡推廣營銷應該怎么···
- 中小企業的網絡推廣應該怎么做
- 網站分類目錄還有用嗎
- 網站分類目錄可以有效的提高網站···
- 網站SEO優化中外鏈應該怎么做···
- 合肥百度愛采購一年多少錢
- 合肥企業網站網絡營銷推廣應該怎···