适合自己的 ● 才是最好的 新闻动态,洞悉互联网前沿资讯,探寻网站营销规律
当前位置:网站首页 >> 网站制作
pbootcms缩略图裁剪优化居中裁剪
日期:2021-11-03 17:26:39 来源:合肥良马网络科技有限公司 官网:https://www.hflmwl.com/ 阅读:1251次
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 <a href= "[list:link]"><img src="[list:ico width=600 height=400]" /></a>
3 {/pboot:list}

如上代码,直接生成600*400像素的居中裁剪过的图片。
 
收工。
举个例子:列表中输出图片


本文标题:pbootcms缩略图裁剪优化居中裁剪
本文网址:https://www.hflmwl.com/news/1483.html
原创作者:合肥良马网络科技有限公司 版权所有,转载请注明出处,并以链接形式注明。
声明:本页内容由合肥良马网络科技有限公司通过网络收集编辑和原创所得,所有资料仅供用户参考;本站不拥有所有权,也不承认相关法律责任。如您认为本网页中有涉嫌抄写的内容,请及时与我们联系进行举报,并提供相关证据,工作人员会在5个工作日内联系您,一经查实,本站将立刻删除涉嫌侵权内容。如果您对SEO优化核心技术文章感兴趣,请点击查看网络推广网站制作的相关文章,请关注良马科技官网(www.hflmwl.com)

推荐文章

相关文章