復(fù)制代碼 代碼如下:
<?php
define('WWW_PATH', dirname(dirname(__FILE__))); // 站點(diǎn)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/'; // 目標(biāo)路徑
$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, 'http://www.survivalescaperooms.com/'));
// 獲取config
$config = isset($thumb_config[$type])? $thumb_config[$type] : '';
// 檢查config
if(!$config || !isset($config['fromdir'])){
exit();
}
// 原圖文件
$source = str_replace('http://www.survivalescaperooms.com/'.$type.'http://www.survivalescaperooms.com/', 'http://www.survivalescaperooms.com/'.$config['fromdir'].'http://www.survivalescaperooms.com/', $source_path.$relative_url);
// 目標(biāo)文件
$dest = $dest_path.$relative_url;
if(!file_exists($source)){ // 原圖不存在
exit();
}
// 高并發(fā)處理
$processing_flag = '/tmp/thumb_'.md5($dest); // 用于判斷文件是否處理中
$is_wait = 0; // 是否需要等待
$wait_timeout = 5; // 等待超時(shí)時(shí)間
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){ // 超時(shí)
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(); // 生成失敗退出
}
}
// 創(chuàng)建縮略圖
$obj = new PicThumb($logfile);
$obj->set_config($config);
$create_flag = $obj->create_thumb($source, $dest);
unlink($processing_flag); // 刪除處理中標(biāo)記文件
if($create_flag){ // 判斷是否生成成功
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}
?>
|
新聞熱點(diǎn)
疑難解答