为了降低服务器的CPU和内存的使用,加速网站访问速度,自动调整网站图片,记录网站图片的缓存并读取。每一个文件,一个月以上将自动删除清理缓存文件夹未使用的图像。
代码如下:
/** Cached Resized Images Hack Start **/
function flushOldCachedImages(){
$ts = strtotime(‘now’);
if ($handle = opendir(DIR_WS_PRODUCT_IMAGE_CACHE_FS_PATH)) {
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
$hashFile = DIR_WS_PRODUCT_IMAGE_CACHE_FS_PATH . ‘/’ . $file;
if($ts – filemtime($hashFile) > 86400*30){ // older than a month then remove
@unlink($hashFile);
}
}
}
closedir($handle);
}
}
function resizeImage($imgPath,$width,$height){
$noGifSupportHandleStrategy = ‘keepImage’;
// note: if you want the no_image.png to be shown uncomment the following
//$noGifSupportHandleStrategy = ‘noImage’;
if(file_exists($imgPath) && is_file($imgPath) && !is_dir($imgPath)){
$fp = fopen($imgPath,’rb’);
$hash = md5(base64_encode(fread($fp,filesize($imgPath))));
fclose($fp);
$fileinfo = pathinfo($imgPath);
$file_type = strtolower($fileinfo["extension"]);
$hashFile = DIR_WS_PRODUCT_IMAGE_CACHE_FS_PATH . ‘/’ . $hash . ‘.’ . $file_type;
if(!file_exists($hashFile)){
if(!imageCopyResize($imgPath,$width,$height,0,$hashFile)){
if($noGifSupportHandleStrategy == ‘noImage’){
return array(DIR_WS_IMAGES . ‘nopic.jpg’, $width, $height);
} else {
return array(DIR_WS_IMAGES . basename($imgPath), SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
}
}
}
$size = getimagesize($hashFile);
return array(DIR_WS_PRODUCT_IMAGE_CACHE_HTTP_PATH . ‘/’ . $hash . ‘.’ . $file_type, $size[0],$size[1]);
}
return array(DIR_WS_IMAGES . ‘nopic.jpg’, $width, $height);
}
function resizeImageCat($imgPath,$width,$height){
$noGifSupportHandleStrategy = ‘keepImage’;
// note: if you want the no_image.png to be shown uncomment the following
//$noGifSupportHandleStrategy = ‘noImage’;
if(file_exists($imgPath) && is_file($imgPath) && !is_dir($imgPath)){
$fp = fopen($imgPath,’rb’);
$hash = md5(base64_encode(fread($fp,filesize($imgPath))));
fclose($fp);
$fileinfo = pathinfo($imgPath);
$file_type = strtolower($fileinfo["extension"]);
$hashFile = DIR_WS_PRODUCT_IMAGE_CACHE_FS_PATH . ‘/’ . $hash . ‘Cat.’ . $file_type;
if(!file_exists($hashFile)){
if(!imageCopyResize($imgPath,$width,$height,0,$hashFile)){
if($noGifSupportHandleStrategy == ‘noImage’){
return array(DIR_WS_IMAGES . ‘nopic.jpg’, $width, $height);
} else {
return array(DIR_WS_IMAGES .basename($imgPath), SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
}
}
}
$size = getimagesize($hashFile);
return array(DIR_WS_PRODUCT_IMAGE_CACHE_HTTP_PATH . ‘/’ . $hash . ‘Cat.’ . $file_type, $size[0],$size[1]);
}
return array(DIR_WS_IMAGES . ‘nopic.jpg’, $width, $height);
}
function imageCopyResize($image, $xSize, $ySize, $needed_user_factor, $new_image_path = “”, $returnOnlyImgRef = false)
{
if(!function_exists(‘ImageCreateFromGif’) || !function_exists(‘ImageGif’)){
return false;
}
//example: neded_user_factor = 0.2
if($xSize == 0 || $ySize == 0)
die(“At the leat one pair of size is 0″);
$size = getImageSize($image);
$width = $size[0];
$height = $size[1];
$x_ratio = $xSize / $size[0];
$y_ratio = $ySize / $size[1];
if($x_ratio > $y_ratio) $res_ratio = $y_ratio;
else $res_ratio = $x_ratio;
if(abs($res_ratio – 1) > $needed_user_factor)
{
$width = ceil($width * $res_ratio);
$height = ceil($height * $res_ratio);
}
$fileinfo = pathinfo($image);
$file_type = strtolower($fileinfo["extension"]);
if($file_type == “jpg” || $file_type == “jpeg”)
$src = ImageCreateFromJpeg($image);
else if($file_type == “gif”)
$src = ImageCreateFromGif($image);
else if($file_type == “png”)
$src = ImageCreateFromPNG($image);
else if($file_type == “bmp”)
$src = ImageCreateFromBmp($image); //Thanks Previous Coder
else
die(“Unknown image format: “.$file_type);
$dst = ImageCreateTrueColor($width, $height);
imagecopyresampled($dst, $src, 0,0,0,0,$width, $height, $size[0], $size[1]);
if($returnOnlyImgRef){
ImageDestroy($src);
return $dst;
}
$output_path = ($new_image_path == “”)?$image:$new_image_path;
if($file_type == “jpg” || $file_type == “jpeg”)
ImageJpeg($dst, $output_path, 100);
else if($file_type == “gif” || $file_type == “bmp”)
ImageGif($dst, $output_path);
else if($file_type == “png”)
ImagePNG($dst, $output_path);
ImageDestroy($src);
ImageDestroy($dst);
return true;
}
/** Cached Resized Images Hack End **/
代码引用:
$fileToResize = 动态图像路径;
$resizedImageResult = resizeImage($fileToResize,SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
$resizedImage = $resizedImageResult[0];(取出缓存图像路径)
$resizedImageWidth = $resizedImageResult[1];(取出缓存图像width)
$resizedImageHeight = $resizedImageResult[2];(取出缓存图像height)