亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > PHP > 正文

php動態生成圖片實現瀏覽器緩存

2019-11-08 01:21:17
字體:
來源:轉載
供稿:網友

當我們用php實現網站圖片尺寸重寫時,默認情況下瀏覽器不會將重寫的圖片緩存本地,現在我告訴你們怎樣讓瀏覽器緩存這樣的圖片數據。 // 在php腳本文件加上以下代碼

session_start(); header("Cache-Control: PRivate, max-age=10800, pre-check=10800");header("Pragma: private");header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));

1:如果瀏覽器已經有緩存版本,它會向后端傳一個$_SERVER[‘HTTP_IF_MODIFIED_SINCE’] ,簡單的做法我們只需要告訴瀏覽器用緩存即可。

if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ // if the browser has a cached version of this image, send 304 header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304); exit;}

2:如果瀏覽器有緩存版本,更明智的做法是,后端判斷圖片有沒有更新,對應告知瀏覽器是否用緩存版本

$img = "some_image.png";if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($img))) { // send the last mod time of the file back header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($img)).' GMT', true, 304); exit;}

完整的代碼

session_start(); header("Cache-Control: private, max-age=10800, pre-check=10800");header("Pragma: private");header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));$img = "some_image.png"; if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($img))) { // send the last mod time of the file back header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($img)).' GMT', true, 304); exit;}

// 裁剪圖片,并且告知瀏覽器緩存圖片

$is = new ImageSizer($img); $is->resizeImage(75); // resize image to 75px wide// and here we send the image to the browser with all the stuff required for tell it to cacheheader("Content-type: image/jpeg");header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($thumbnail)) . ' GMT');$is->showImage();

圖片裁剪類

class ImageSizer { var $_baseDir; var $_baseImg; var $_imgData; var $_newImg; var $_newData; var $_newFormat; var $_loadPath; var $_defaultColor = array(255,255,255); var $keepaspectRatio; var $makeBigger; function ImageSizer($baseDir) { $this->changeBaseDir($baseDir); $this->keepAspectRatio = true; $this->makeBigger = false; } function changeBaseDir($baseDir) { $this->_baseDir = $baseDir; } function setDefaultColor($r, $g, $b) { $this->_defaultColor = array($r, $g, $b); } function changeFormat($str) { $str = strtolower($str); if ($str == 'jpg') $str = "jpeg"; $acceptable_formats = array('jpeg', 'gif', 'png'); if (!in_array($str, $acceptable_formats)) return false; $this->_newFormat = $str; } function loadImage($imgPath) { // $this->_imgData = getimagesize($this->_baseDir. $imgPath); $this->_imgData = getimagesize( $imgPath); $this->_imgData['funcType'] = preg_replace('#image/#i', '', $this->_imgData['mime']); $acceptable_formats = array('jpeg', 'gif', 'png'); if (!in_array($this->_imgData['funcType'], $acceptable_formats)) return false; $this->_newData = $this->_imgData; $funcName = 'imagecreatefrom' . $this->_imgData['funcType']; //$this->_newImg = $this->_baseImg = $funcName($this->_baseDir. $imgPath); $this->_newImg = $this->_baseImg = @$funcName( $imgPath); if(!$this->_baseImg){ echo " Failed on this image $imgPath "; return false; } $this->_loadPath = $imgPath; return true; } /*function genImageData(){ $funcName = 'image'.$this->getNewType(); $data = $funcName($this->_newImg ? $this->_newImg : $this->_baseImg); return $data; }*/ function resizeImage($w, $h, $crop=false) { $current_w = $this->getWidth(); $current_h = $this->getHeight(); $src_x = $src_y = 0; $dst_x = $dst_y = 0; if($w && $h && $crop=="f"){ // fill in the image centre it in white. // all the src stuff stays the same, just changing the destination suff $src_x = 0; $src_y = 0; $current_h = $current_h; $current_w = $current_w; // the destination stuff changes if($h && $w){ $h_percent = $percent = $h / $current_h; $w_percent = $percent = $w / $current_w; $percent = min($h_percent, $w_percent); }else if($h){ $h_percent = $percent = $h / $current_h; $percent = $h_percent; }else if($w){ $w_percent = $percent = $w / $current_w; $percent = $w_percent; } $dst_w = $current_w * $percent; $dst_h = $current_h * $percent; $new_w = $w; $new_h = $h; // work out destination x and y points $dst_x = ($new_w - $dst_w)/2; $dst_y = ($new_h - $dst_h)/2; }else if($w && $h && $crop=="c"){ $dst_w = $w; $dst_h = $h; $new_w = $w; $new_h = $h; // if the image we are tyring to crop is smaller than the crop request we dont do aynthing if($w > $current_w || $h > $current_h){ // the image is smaller than we are trying to crop! }else{ //the image is bigger on x and y axis. // check if we can fit horizontally $w_percent = $current_w/$w; $h_percent = $current_h/$h; if($w_percent < $h_percent){ $src_x = 0; $src_y = ($current_h/2) - (($h * $w_percent)/2); $current_w = $current_w; $current_h = ($h * $w_percent); }else{ $src_x = ($current_w/2) - (($w * $h_percent)/2); $src_y = 0; $current_w = ($w * $h_percent); $current_h = $current_h; } } }else if ($this->keepAspectRatio) { $percent = 1; // if ($current_w > $w || $current_h > $h || $this->makeBigger) { $do_resize=false; if($w && $current_w > $w){ $do_resize=true; }else if($h && $current_h > $h){ $do_resize=true; }else if($w && $current_w < $w && $this->makeBigger){ $do_resize=true; }else{ // imaeg is alreaedy smaller than requested size } if ( $do_resize ) { if($h && $w){ $h_percent = $percent = $h / $current_h; $w_percent = $percent = $w / $current_w; $percent = min($h_percent, $w_percent); }else if($h){ $h_percent = $percent = $h / $current_h; $percent = $h_percent; }else if($w){ $w_percent = $percent = $w / $current_w; $percent = $w_percent; } } $dst_w = $new_w = $current_w * $percent; $dst_h = $new_h = $current_h * $percent; } else{ $dst_w = $new_w = $w; $dst_h = $new_h = $h; } $this->_newImg = ImageCreateTrueColor($new_w, $new_h); $this->_newData = array($new_w, $new_h); if ($this->getNewType() == 'png' || $this->getNewType() == 'gif') { // This preserves the transparency imageAlphaBlending($this->_newImg, false); imageSaveAlpha($this->_newImg, true); } else { // This is if converting from PNG to another image format list($r, $g, $b) = $this->_defaultColor; $color = imagecolorallocate($this->_newImg, $r, $g, $b); imagefilledrectangle($this->_newImg, 0,0, $new_w, $new_h, $color); } // dst_w dst_h src_w src_h imagecopyresampled($this->_newImg, $this->_baseImg, $dst_x, $dst_y,$src_x,$src_y, $dst_w, $dst_h, $current_w, $current_h); return true; } function showImage() { header('Content-type: ' . $this->getNewMime()); $funcName = 'image'.$this->getNewType(); $funcName($this->_newImg ? $this->_newImg : $this->_baseImg); } function saveToFile($fileloc) { $funcName = 'image'.$this->getNewType(); $funcName($this->_newImg ? $this->_newImg : $this->_baseImg, $fileloc,100); } function addWatermark($pngloc,$offset_x=0) { $overlay = imagecreatefrompng($pngloc); imageAlphaBlending($overlay, false); imageSaveAlpha($overlay, true); imagecopy($this->_newImg, $overlay, (imagesx($this->_newImg))-(imagesx($overlay)+$offset_x), (imagesy($this->_newImg))-(imagesy($overlay)), 0, 0, imagesx($overlay), imagesy($overlay)); } function getBaseDir() { return $this->_baseDir; } function getWidth() { return $this->_imgData[0]; } function getHeight() { return $this->_imgData[1]; } function getMime() { return $this->_imgData['mime']; } function getType() { return $this->_imgData['funcType'];} function getNewWidth() { return $this->_newData[0]; } function getNewHeight() { return $this->_newData[1]; } function getNewMime() {return $this->_newFormat ? 'image/' . $this->_newFormat : $this->_imgData['mime'];} function getNewType() {return $this->_newFormat ? $this->_newFormat : $this->_imgData['funcType'];}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产亚洲欧洲高清| 国产精品免费观看在线| 久久久久久久久久久国产| 久久成人在线视频| www国产亚洲精品久久网站| 亚洲视频在线播放| 国产脚交av在线一区二区| 国产亚洲精品久久久| 亚洲国产欧美在线成人app| 国产精品久久久久久亚洲影视| 欧美黄色小视频| 精品国产拍在线观看| 午夜免费在线观看精品视频| 欧美孕妇与黑人孕交| 国产亚洲精品久久久优势| 亚洲一区www| 国产精品久久久久久久av大片| 国产日产久久高清欧美一区| 欧美国产激情18| 在线观看精品国产视频| 亚洲sss综合天堂久久| 国产精品对白刺激| 国内精品免费午夜毛片| 福利微拍一区二区| 亚洲第一天堂无码专区| 欧美精品久久久久久久| 国产精品日日做人人爱| 欧美激情精品久久久久久大尺度| 欧美专区第一页| 91精品国产高清自在线看超| 国产偷亚洲偷欧美偷精品| 国产99视频在线观看| 中文字幕日韩在线播放| 国产视频综合在线| 欧美激情2020午夜免费观看| 欧美裸体xxxx极品少妇软件| 精品视频在线播放| 久久国产精品偷| 中文字幕av日韩| 色噜噜亚洲精品中文字幕| 欧美国产日韩视频| 欧美日韩在线免费观看| 日韩欧美在线视频日韩欧美在线视频| 国产精品三级美女白浆呻吟| 高清欧美电影在线| 欧美多人乱p欧美4p久久| 狠狠久久五月精品中文字幕| 日本欧美一级片| 亚洲xxx大片| 国内精品小视频| 青草青草久热精品视频在线观看| 中日韩美女免费视频网址在线观看| 国产丝袜一区二区三区| 亚洲色图第三页| 综合av色偷偷网| 亚洲精品免费网站| 91久久中文字幕| 欧美视频不卡中文| 欧美综合国产精品久久丁香| 日本精品va在线观看| 国产乱肥老妇国产一区二| 美女精品久久久| 欧美—级a级欧美特级ar全黄| 国产精品视频资源| 色777狠狠综合秋免鲁丝| 亚洲综合中文字幕在线观看| 欧美日韩国产中文字幕| 国产精品草莓在线免费观看| 午夜精品久久久久久久久久久久| 国产成人自拍视频在线观看| 久久久久国产视频| 国产精品igao视频| 欧美有码在线观看| 日韩暖暖在线视频| 国产精品视频大全| 久久久久久91香蕉国产| 亚洲的天堂在线中文字幕| 日韩网站在线观看| 欧美与欧洲交xxxx免费观看| 亚洲电影免费在线观看| 国产精品久久久久久久9999| 黑人巨大精品欧美一区免费视频| 91视频国产精品| 国产精品扒开腿爽爽爽视频| xvideos国产精品| 亚洲精品少妇网址| 免费97视频在线精品国自产拍| 欧美国产视频日韩| 日韩成人久久久| 色哟哟入口国产精品| 日本精品一区二区三区在线播放视频| 色婷婷av一区二区三区在线观看| 国产精品高清网站| 日韩免费不卡av| 国产在线播放91| 亚洲变态欧美另类捆绑| 久久久噜噜噜久久| 欧美午夜无遮挡| 久久精品国产一区二区电影| 欧美又大又硬又粗bbbbb| 欧美激情区在线播放| 欧美激情在线观看| 欧美高跟鞋交xxxxxhd| 国产91热爆ts人妖在线| 欧美日产国产成人免费图片| 成人久久18免费网站图片| 亚洲欧美日韩另类| 欧美大片在线免费观看| 久久精品一区中文字幕| 欧美亚洲午夜视频在线观看| 国产精品激情av电影在线观看| 不卡av电影院| 亚洲国产精品久久久久秋霞不卡| 欧美美女15p| 久久久精品免费视频| 国内精品久久久久久久| 亚洲成人aaa| 亚洲图片欧美日产| 福利一区福利二区微拍刺激| 中文字幕欧美精品日韩中文字幕| 欧美久久精品午夜青青大伊人| 91免费视频网站| 久久视频在线直播| 亚洲电影第1页| 久久国产视频网站| 亚洲精选在线观看| 国产欧美久久久久久| 欧美高清无遮挡| 国产日韩换脸av一区在线观看| 在线国产精品播放| 97在线免费视频| 欧美在线一区二区视频| 久久久精品一区| 色999日韩欧美国产| 精品久久久久国产| 欧美与黑人午夜性猛交久久久| 91免费的视频在线播放| 国产91精品视频在线观看| 欧美日韩人人澡狠狠躁视频| 欧美大码xxxx| 国产精品视频地址| 亚洲人在线视频| 亚洲亚裔videos黑人hd| www.日韩欧美| 亚洲欧美中文日韩在线| 久久精品国产91精品亚洲| 久久久久久香蕉网| 欧美激情在线视频二区| 国产精品91一区| 1769国内精品视频在线播放| 国产丝袜一区二区三区| 精品国产91久久久久久| 一区二区三区无码高清视频| 欧美激情亚洲另类| 国产精品久久久久久久久久久新郎| 18一19gay欧美视频网站| 国产精品久久91| 欧美专区在线观看| 中文字幕综合一区| 久久精品人人做人人爽| 日本a级片电影一区二区| 亚洲精品欧美日韩专区| 日本精品中文字幕| 成人欧美在线观看|