<?php require 'PHPThumb.php'; require 'GD.php'; //http 304缓存 if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == md5($mixData)) { //header("HTTP/1.1 304 Not Modified"); //exit; } $imgSrc = 'banner.jpg'; if(strpos($imgSrc,"http") === 0) { // 远程图片 $extPad = ''; $fileExt = pathinfo($imgSrc,PATHINFO_EXTENSION); if($fileExt == "") { $extPad = ".jpg"; } else if(!in_array(strtolower($fileExt),array("jpg","png","gif","tbi"))) { return; } $downFile = basename($imgSrc).$extPad; if(!is_file($downFile)) { $ch = curl_init($imgSrc); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $fp = fopen($downFile, 'w+b'); if($fp) { flock($fp,LOCK_EX); fwrite($fp,curl_exec($ch)); flock($fp,LOCK_UN); fclose($fp); } } } else { // 本地图片 if(is_file($imgSrc) == false) { return; } $width = 100; $height = 100; $saveDir= './'; $extName= '_thumb'; $GD = new GD($imgSrc); if($GD) { $GD->resize($width, $height); $GD->pad($width, $height); //存储缩略图 if ($saveDir) { //生成缩略图文件名 $thumbBaseName = $extName . basename($imgSrc); $thumbFileName = $saveDir . basename($thumbBaseName); $GD->save($thumbFileName); return $thumbFileName; } //直接输出浏览器 else { return $GD->show(); } } }
DEMO下载
访客评论