PbootCMS 缩略图裁剪优化教程:居中裁剪设置与 SEO 适配技巧
发布人:鲸弘科技
发布时间:2026-01-08
浏览量:97 次
本文由广东鲸弘科技有限公司提供惠州小程序开发 / 网站建设专业分享。
PbootCms默认的图片裁剪是从左上角开始裁剪,使用width,height,maxwidth,maxheight等参数进行调节。
但是裁剪出来的图片始终感觉不是那么令人满意。(也许是我不会用)
比如多人物合影,PB默认的裁剪是左上,那么这个裁剪就会丢掉C位,那怎么能行。
今天来优化一下裁剪图片,让他能简单快捷的裁剪出理想的图片。
我想要的效果是不论横图竖图都是居中裁剪。
首先找到裁剪缩略图的方法,
文件位置:/core/function/file.php
搜索:function cut_img,大约在447行
对cut_img方法进行优化,直接上代码:
// 剪切图片
function cut_img($src_image, $out_image = null, int $new_width = null, int $new_height = null, $img_quality = 90)
{
// 输出地址
if (! $out_image)
$out_image = $src_image;
// 读取配置文件设置
if (! $new_width && ! $new_height)
return;
// 获取图片属性
list ($width, $height, $type, $attr) = getimagesize($src_image);
switch ($type) {
case 1:
$img = imagecreatefromgif($src_image);
break;
case 2:
$img = imagecreatefromjpeg($src_image);
break;
case 3:
$img = imagecreatefrompng($src_image);
break;
}
// 不限定是等比例缩放
if (! $new_width) {
$new_width = floor($width * ($new_height / $height));
}
if (! $new_height) {
$new_height = floor($height * ($new_width / $width));
}
// 创建画布
$new_img = imagecreatetruecolor($new_width, $new_height);
// 创建透明画布,避免黑色
if ($type == 1 || $type == 3) {
$color = imagecolorallocate($new_img, 255, 255, 255);
imagefill($new_img, 0, 0, $color);
imagecolortransparent($new_img, $color);
}
// 先缩放
$scale = max($new_width / $width, $new_height / $height);
$scale_width = floor($scale * $width);
$scale_height = floor($scale * $height);
$scale_img = imagecreatetruecolor($scale_width, $scale_height); // 创建画布
if(function_exists("ImageCopyResampled")) {
imagecopyresampled($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
} else {
imagecopyresized($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
}
//再裁剪
$start_x = ($scale_width - $new_width) / 2;
$start_y = ($scale_height - $new_height) / 2;
//拷贝剪切的图像数据到画板,生成剪切图像
imagecopy($new_img, $scale_img, 0, 0, $start_x, $start_y, $scale_width, $scale_height);
check_dir(dirname($out_image), true); // 检查输出目录
switch ($type) {
case 1:
imagegif($new_img, $out_image, $img_quality);
break;
case 2:
imagejpeg($new_img, $out_image, $img_quality);
break;
case 3:
imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
break;
default:
imagejpeg($new_img, $out_image, $img_quality);
}
imagedestroy($new_img);
imagedestroy($img);
return true;
}小伙伴可以直接复制替换该方法。
举个例子:列表中输出图片
{pboot:list scode=*}
<a href= "[list:link]"><img src="[list:ico width=600 height=400]" /></a>
{/pboot:list}如上代码,直接生成600*400像素的居中裁剪过的图片。
推荐专题
最新阅读
-
惠州企业官网的后台,需要具备哪些SEO功能才算合格?一份技术功能自查表
2026-07-17
35 -
2026 惠州网站建设未来发展趋势
2026-03-20
85 -
惠州小程序开发,为什么我劝你别一上来就问报价?先搞清楚这4件事
2026-06-09
45 -
惠州网站建设哪家好?从小程序到官网建站,本地靠谱公司推荐清单
2025-09-01
189 -
仲恺高新区小程序开发,工厂展示型小程序的3个必备功能
2026-05-22
69 -
惠州汽配零部件制造企业官网建设方案:如何用APQP和IATF 16949认证展示赢得主机厂信赖?
2026-07-24
31 -
广东鲸弘科技王飞:解答惠州企业关于官网和小程序开发最常见的10个问题
2026-06-13
66 -
Picsum Photos 终极指南:简单高效的随机图片API解决方案
2026-02-01
493 -
惠州网站运维如何优化数据库,让网站速度飞起来?(性能优化+速度提升)
2026-04-28
66 -
惠州小程序开发如何低成本撬动微信12亿用户流量?
2026-04-18
70
咨询热线:
联系电话
联系邮箱
联系QQ
方案获取
