成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

php圖像裁剪服務(wù)器搭建的方法

這篇文章給大家分享的是有關(guān)php圖像裁剪服務(wù)器搭建的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

為辛集等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及辛集網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、辛集網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

在我們的工作的項(xiàng)目中,有時(shí)候我們需要顯示規(guī)定尺寸的圖片,雖然可以通過(guò)css來(lái)控制顯示大小。但是如果圖片過(guò)大,會(huì)造成加載的延遲,影響網(wǎng)站整體性能。因此,我們需要一個(gè)服務(wù)器來(lái)幫助我們進(jìn)行圖片的裁剪。流程大致是,首先我們傳給服務(wù)器原圖像和裁剪的尺寸,然后服務(wù)器進(jìn)行裁剪,生成對(duì)應(yīng)的裁剪圖片,下次我們?cè)僭L問(wèn)相同圖像和相同的裁剪尺寸的時(shí)候,我們就不需要裁剪,直接進(jìn)行圖片的訪問(wèn)就行。

Talk is cheap, show me the code.

<?php
// ①構(gòu)建圖片請(qǐng)求地址比如  /upload/otherpic71/35720.jpg
// ②配置nginx重寫規(guī)則  rewrite /s/(.*)/(\d+)x(\d+)-(\d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last;
//③進(jìn)行裁剪圖片的處理
$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;
//原圖像對(duì)應(yīng)縮放裁剪,會(huì)拉伸圖片
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'];
    
    //根據(jù)參數(shù)$width和$height值,換算出等比例縮放的高度和寬度
    if ($width && ($width_orig<$height_orig)){
        $width = ($height/$height_orig)*$width_orig;
    }else{
        $height = ($width / $width_orig)*$height_orig;
    }
    //將原圖縮放到這個(gè)新創(chuàng)建的圖片資源中
    $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()函數(shù)進(jìn)行縮放設(shè)置
    imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
   
    //將縮放后的圖片$image_p保存,100(質(zhì)量最佳,文件最大)
    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);
    }
    
}
//進(jìn)行比例保存裁剪,會(huì)丟失圖像部分像素
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;
    // 源圖過(guò)高
    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;
    }
    // 源圖過(guò)寬
    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);
}
// 循環(huán)生成目錄
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);
}

通過(guò)上面的處理,我們就將圖片按照我們?cè)O(shè)置的尺寸進(jìn)行了裁剪。我們還可以定期對(duì)裁剪圖片進(jìn)行清理,這樣就不需要占用太多服務(wù)器空間。只有經(jīng)常訪問(wèn)的圖片才會(huì)一直保存。

感謝各位的閱讀!關(guān)于php圖像裁剪服務(wù)器搭建的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:php圖像裁剪服務(wù)器搭建的方法
文章網(wǎng)址:http://jinyejixie.com/article0/psidio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)App設(shè)計(jì)、微信小程序虛擬主機(jī)、做網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
营山县| 五原县| 个旧市| 神木县| 武穴市| 梅河口市| 兰西县| 兰溪市| 金秀| 筠连县| 贵港市| 台安县| 安宁市| 苍南县| 钦州市| 麟游县| 阳泉市| 固始县| 河北区| 安图县| 闽侯县| 泸溪县| 北辰区| 沧源| 新余市| 剑阁县| 宜川县| 织金县| 兴和县| 中超| 拜城县| 兴海县| 万源市| 台前县| 张家界市| 桐柏县| 开原市| 昔阳县| 河津市| 夹江县| 嘉荫县|