php影象裁剪伺服器搭建

2020-07-16 10:05:47
在我們的工作的專案中,有時候我們需要顯示規定尺寸的圖片,雖然可以通過css來控制顯示大小。但是如果圖片過大,會造成載入的延遲,影響網站整體效能。因此,我們需要一個伺服器來幫助我們進行圖片的裁剪。流程大致是,首先我們傳給伺服器原影象和裁剪的尺寸,然後伺服器進行裁剪,生成對應的裁剪圖片,下次我們再存取相同影象和相同的裁剪尺寸的時候,我們就不需要裁剪,直接進行圖片的存取就行。

Talk is cheap, show me the code.

<?php
// ①構建圖片請求地址比如  https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202007/helloworldwnvblxj5qtm.png
// ②設定nginx重寫規則  rewrite /s/(.*)/(d+)x(d+)-(d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last;
//③進行裁剪圖片的處理
$path = trim($_GET['path']);
$mode = intval($_GET['mode']);
$site = trim($_GET['site']);
$width = intval($_GET['width']);
$height = intval($_GET['height']);
$site_list = array('crop' => '.');
$orig_dir = dirname(__FILE__);
if (!array_key_exists($site, $site_list)) {
    header('HTTP/1.1 400 Bad Request');
    exit();
}
if ($mode > 3 || $mode < 0) {
    header('HTTP/1.1 400 Bad Request');
    exit();
}
$orig_file = $site_list[$site] . $path;
if (!file_exists($orig_file)) {
    header('HTTP/1.1 404 Not Found');
    exit();
}
$file_ext = '.' . pathinfo($path, PATHINFO_EXTENSION);
$file_name = basename($path, $file_ext);
$save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}{$path}";
$save_dir = dirname($save_path);
if (!file_exists($save_dir)) {
    wpx_mkdir($save_dir);
}
$target_width = $width;
$target_height = $height;
$save_image = $save_dir . '/' . $file_name . '.jpg';
if (file_exists($save_image)) {
    header('Content-Type: image/jpeg');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    echo file_get_contents($save_image);
}
imagecropper2($orig_file, $target_width, $target_height, $save_image);
die;
//原影象對應縮放裁剪,會拉伸圖片
function imagecropper2($source_path, $width, $height, $save_image)
{
    
    //獲取原影象$filename的寬度$width_orig和高度$height_orig
    $info =  getimagesize($source_path);
    
    $width_orig = $info[0];
    $height_orig = $info[1];
    $mime = $info['mime'];
    
    //根據引數$width和$height值,換算出等比例縮放的高度和寬度
    if ($width && ($width_orig<$height_orig)){
        $width = ($height/$height_orig)*$width_orig;
    }else{
        $height = ($width / $width_orig)*$height_orig;
    }
    //將原圖縮放到這個新建立的圖片資源中
    $image_p = imagecreatetruecolor($width, $height);
    //獲取原圖的影象資源
    if($mime=='image/jpeg'){
        $image = imagecreatefromjpeg($source_path);
    }elseif($mime=='image/png'){
        $image = imagecreatefrompng($source_path);
    }elseif($mime=='image/gif'){
        $image = imagecreatefromgif($source_path);
    }
    
    //使用imagecopyresampled()函數進行縮放設定
    imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
   
    //將縮放後的圖片$image_p儲存,100(品質最佳,檔案最大)
    if($mime=='image/jpeg'){
        imagejpeg($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagejpeg($image_p);
    }elseif($mime=='image/png'){
        imagepng($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagepng($image_p);
    }else{
        imagegif($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagegif($image_p);
    }
    
}
//進行比例儲存裁剪,會丟失影象部分畫素
function imagecropper($source_path, $target_width, $target_height, $save_image)
{
    $source_info = getimagesize($source_path);
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    $source_mime = $source_info['mime'];
    $source_ratio = $source_height / $source_width;
    $target_ratio = $target_height / $target_width;
    // 源圖過高
    if ($source_ratio > $target_ratio) {
        $cropped_width = $source_width;
        $cropped_height = $source_width * $target_ratio;
        $source_x = 0;
        $source_y = ($source_height – $cropped_height) / 2;
    }
    // 源圖過寬
    elseif ($source_ratio < $target_ratio) {
        $cropped_width = $source_height / $target_ratio;
        $cropped_height = $source_height;
        $source_x = ($source_width – $cropped_width) / 2;
        $source_y = 0;
    }
    // 源圖適中
    else {
        $cropped_width = $source_width;
        $cropped_height = $source_height;
        $source_x = 0;
        $source_y = 0;
    }
    switch ($source_mime) {
        case 'image/gif':
            $source_image = imagecreatefromgif($source_path);
            break;
        case 'image/jpeg':
            $source_image = imagecreatefromjpeg($source_path);
            break;
        case 'image/png':
            $source_image = imagecreatefrompng($source_path);
            break;
        default:
            return false;
            break;
    }
    $target_image = imagecreatetruecolor($target_width, $target_height);
    $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
    // 裁剪
    $bool = imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
    // 縮放
    $bool = imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
    imagejpeg($target_image, $save_image);
    header('Content-Type: image/jpeg');
    imagejpeg($target_image);
    imagedestroy($source_image);
    imagedestroy($target_image);
    imagedestroy($cropped_image);
}
// 迴圈生成目錄
function wpx_mkdir($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) {
        return true;
    }
    if (!wpx_mkdir(dirname($dir), $mode)) {
        return false;
    }
    return @mkdir($dir, $mode);
}

通過上面的處理,我們就將圖片按照我們設定的尺寸進行了裁剪。我們還可以定期對裁剪圖片進行清理,這樣就不需要佔用太多伺服器空間。只有經常存取的圖片才會一直儲存。

以上就是php影象裁剪伺服器搭建的詳細內容,更多請關注TW511.COM其它相關文章!