PHP动态修改图片尺寸,加入浏览器缓存

该代码定义了一个PHP函数,用于创建图片缩略图,支持gif、jpeg、png和bmp格式。函数接受源图片路径、缩略图宽高以及保存路径作为参数,进行等比例缩放,并能直接输出到浏览器或保存到文件。同时,它还设置了HTTP缓存头以优化性能。
摘要由CSDN通过智能技术生成
	/**
 * 简单的生成缩略图函数(支持图片格式:gif、jpeg、png和bmp)
 * @author  xiaoshuoit@163.com
 * @param   string  $src        源图片路径
 * @param   int     $width      缩略图宽度(只指定高度时进行等比缩放)
 * @param   int     $width      缩略图高度(只指定宽度时进行等比缩放)
 * @param   string  $filename   保存路径(不指定时直接输出到浏览器)
 * @return  bool
 */
public function simple_thumb($src, $width = null, $height = null, $filename = null) {
    if (!isset($width) && !isset($height))
        return false;
    if (isset($width) && $width <= 0)
        return false;
    if (isset($height) && $height <= 0)
        return false;
 
    $size = getimagesize($src);
    if (!$size)
        return false;
 
    list($src_w, $src_h, $src_type) = $size;
    $src_mime = $size['mime'];
    switch($src_type) {
        case 1 :
            $img_type = 'gif';
            break;
        case 2 :
            $img_type = 'jpeg';
            break;
        case 3 :
            $img_type = 'png';
            break;
        case 15 :
            $img_type = 'wbmp';
            break;
        default :
            return false;
    }
 
    if (!isset($width))
        $width = $src_w * ($height / $src_h);
    if (!isset($height))
        $height = $src_h * ($width / $src_w);
 
    $imagecreatefunc = 'imagecreatefrom' . $img_type;
    $src_img = $imagecreatefunc($src);
    $dest_img = imagecreatetruecolor($width, $height);
    imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
 
    $imagefunc = 'image' . $img_type;
    if ($filename) {
        $imagefunc($dest_img, $filename,90);
    } else {
        header('Content-Type: ' . $src_mime);
        $imagefunc($dest_img);
    }
 
    imagedestroy($src_img);
    imagedestroy($dest_img);
	//开启缓存
	session_start(); 
	header("Cache-Control: private, max-age=10800, pre-check=10800");
	header("Pragma: private");
	header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值