php 根據url自動產生縮圖並處理高並發問題_PHP教程

來源:互聯網
上載者:User
伺服器產生縮圖的時機一般分為兩種:

1.上傳檔案時產生

優點:上傳時就已經產生需要的縮圖,讀取時不需要再判斷,減少cpu運算。

缺點:當縮圖尺寸變化時或新增尺寸時,需要重建所有的縮圖。

2.訪問時產生

優點:1.當有使用者訪問時才需要產生,沒有訪問的不用產生,節省空間的。

2.當修改縮圖尺寸時,只需要修改設定,無需重建所有縮圖。

缺點:當縮圖不存在需要產生時,高並發訪問會非常耗伺服器資源。

雖然訪問時產生會有高並發問題,但其他優點都比第一種方法好,因此只要解決高並發問題就可以。

關於如何根據url自動產生縮圖的原理及實現,可以參考我之前寫的《php 根據url自動產生縮圖》。

高並發處理原理:

1.當判斷需要產生圖片時,在tmp/目錄建立一個臨時標記檔案,檔案名稱用md5(需要產生的檔案名稱)來命名,處理結束後再將臨時檔案刪除。

2.當判斷要產生的檔案在tmp/目錄有臨時標記檔案,表示檔案正在處理中,則不調用產生縮圖方法,而等待,直到臨時標記檔案被刪除,產生成功輸出。

修改的檔案如下,其他與之前一樣。

createthumb.php
複製代碼 代碼如下:
define('WWW_PATH', dirname(dirname(__FILE__))); // 網站www目錄

require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php
require(WWW_PATH.'/ThumbConfig.php'); // include ThumbConfig.php

$logfile = WWW_PATH.'/createthumb.log'; // 記錄檔
$source_path = WWW_PATH.'/upload/'; // 原路徑
$dest_path = WWW_PATH.'/supload/'; // 目標路徑

$path = isset($_GET['path'])? $_GET['path'] : ''; // 訪問的圖片URL

// 檢查path
if(!$path){
exit();
}

// 擷取圖片URI
$relative_url = str_replace($dest_path, '', WWW_PATH.$path);

// 擷取type
$type = substr($relative_url, 0, strpos($relative_url, '/'));

// 擷取config
$config = isset($thumb_config[$type])? $thumb_config[$type] : '';

// 檢查config
if(!$config || !isset($config['fromdir'])){
exit();
}

// 原圖檔案
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);

// 目標檔案
$dest = $dest_path.$relative_url;

if(!file_exists($source)){ // 原圖不存在
exit();
}

// 高並發處理
$processing_flag = '/tmp/thumb_'.md5($dest); // 用於判斷檔案是否處理中
$is_wait = 0; // 是否需要等待
$wait_timeout = 5; // 等待逾時時間

if(!file_exists($processing_flag)){
file_put_contents($processing_flag, 1, true);
}else{
$is_wait = 1;
}

if($is_wait){ // 需要等待產生
while(file_exists($processing_flag)){
if(time()-$starttime>$wait_timeout){ // 逾時
exit();
}
usleep(300000); // sleep 300 ms
}

if(file_exists($dest)){ // 圖片產生成功
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}else{
exit(); // 產生失敗退出
}
}

// 建立縮圖
$obj = new PicThumb($logfile);
$obj->set_config($config);
$create_flag = $obj->create_thumb($source, $dest);

unlink($processing_flag); // 刪除處理中標記檔案

if($create_flag){ // 判斷是否產生成功
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}

?>

源碼下載地址:點擊查看

http://www.bkjia.com/PHPjc/726022.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/726022.htmlTechArticle伺服器產生縮圖的時機一般分為兩種: 1.上傳檔案時產生 優點:上傳時就已經產生需要的縮圖,讀取時不需要再判斷,減少cpu運算。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.