PbootCMS缩略图居中裁剪优化设置

发布期:2023-10-02 | 来源处:本站 | 编辑者:站长助手 | 浏览量:0
02
10
2023


PbootCMS原模板默认的图片裁剪是从左上角开始裁剪,使用width,height,maxwidth,maxheight等参数进行调节。但是裁剪出来的图片,始终感觉不是那么令人满意。比如多人物合影,Pboot默认的裁剪是左上,那么这个裁剪就会丢掉C位,这样就达不到我们的要求了。

在这里讲述,优化一下裁剪图片,让它能简单快捷的裁剪出我们需要的图片。需要的效果是不论横图竖图都是居中裁剪。


PbootCMS缩略图居中裁剪优化设置


解决方案,首先找到裁剪缩略图的方法,

 文件位置:/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=640 height=420]" /></a>
{/pboot:list}


如上代码,直接生成640*420像素的居中裁剪过的图片。



TAG 标签:

网站优化专区

本月热点资讯
热线(微信同号):
137-6042-7131
在线客服:
售前
售前
售前
售后
微信图标
联系微信:
微信联系二维码
Hi,Are you ready?
准备好开始了吗?
那就与我们取得联系吧

咨询送礼现在提交,将获得全宝鑫公司策划专家免费为您设计
创意LOGO,免费设计您满意的名片。
下单送礼感恩多年,新老用户下单即送创业型空间+域名等大礼
8:30-18:00小时免费咨询热线137-6042-7131
建站留言合作意向表

您需要的服务

您关注的地方

您大概的预算

直接咨询