+ 收藏我們

網(wǎng)站模板

網(wǎng)站模板搜索
網(wǎng)站模板 > cms教程 > pbootcms教程 >

PbootCms縮略圖裁剪優(yōu)化居中裁剪(直接上干貨)

AB模板網(wǎng) / 2021-11-02
PbootCms模板默認(rèn)的圖片裁剪是從左上角開始裁剪,使用width,height,maxwidth,maxheight等參數(shù)進(jìn)行調(diào)節(jié)。
 
但是裁剪出來的圖片始終感覺不是那么令人滿意。(也許是我不會(huì)用)
 
比如多人物合影,PB默認(rèn)的裁剪是左上,那么這個(gè)裁剪就會(huì)丟掉C位,那怎么能行。
 
今天來優(yōu)化一下裁剪圖片,讓他能簡單快捷的裁剪出理想的圖片。
 
我想要的效果是不論橫圖豎圖都是居中裁剪。
 
翠花,上干貨!
 
解決方案
 
首先找到裁剪縮略圖的方法,
 
文件位置:/core/function/file.php
 
搜索:function cut_img,大約在447行
 
對cut_img方法進(jìn)行優(yōu)化,直接上代碼:

// 剪切圖片
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;
    
    // 讀取配置文件設(shè)置
    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));
    }
    // 創(chuàng)建畫布
    $new_img = imagecreatetruecolor($new_width, $new_height);
    
    // 創(chuàng)建透明畫布,避免黑色
    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); // 創(chuàng)建畫布
    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;

    //拷貝剪切的圖像數(shù)據(jù)到畫板,生成剪切圖像
    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參數(shù)取值范圍0-99 在php 5.1.2之后變更為0-9
            break;
        default:
            imagejpeg($new_img, $out_image, $img_quality);
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return true;
}
小伙伴可以直接復(fù)制替換該方法。
 
使用方法

{pboot:list scode=*}
<a href= "[list:link]"><img src="[list:ico width=600 height=400]" /></a>
{/pboot:list}

如上代碼,直接生成600*400像素的居中裁剪過的圖片。
 
收工。
舉個(gè)例子:列表中輸出圖片
文章搜集與網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系本站,轉(zhuǎn)載請說明出處:http://www.warewashingadvisors.com/pbootcmsjs/9603.html
首頁 pbootcms模板 html模板 會(huì)員中心
 
咨詢
QQ在線咨詢

如無回復(fù)
請加下面的微信

添加微信客

微信客服
wwwadminbuycn