在PHP脚本上缓存图像 [英] Cache images on php script

查看:89
本文介绍了在PHP脚本上缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此处的优秀随机码代码: http://alistapart.com/article/randomizer,但图像不会保存在浏览器的缓存中,这意味着它们会在网站重新加载时刷新。

I am using this great randomizer code found here: http://alistapart.com/article/randomizer , but the images are not saved on the browser's cache which means they refresh whenever the site reloads.

是否可以将图像缓存在以下代码中?
我使用这个脚本的原因是css上的php解析由于某种原因在我的服务器上不起作用。

Is it possible to keep the images cached on the following code? The reason I am using this script is that php parse on css doesn't work on my server for some reason.

<?php

$folder = '.';
$extList = array();
    $extList['gif'] = 'image/gif';
    $extList['jpg'] = 'image/jpeg';
    $extList['jpeg'] = 'image/jpeg';
    $extList['png'] = 'image/png';

$img = null;

if (substr($folder,-1) != '/') {
    $folder = $folder.'/';
}

if (isset($_GET['img'])) {
    $imageInfo = pathinfo($_GET['img']);
    if (
        isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
        $img = $folder.$imageInfo['basename'];
    }
} else {
    $fileList = array();
    $handle = opendir($folder);
    while ( false !== ( $file = readdir($handle) ) ) {
        $file_info = pathinfo($file);
        if (
            isset( $extList[ strtolower( $file_info['extension'] ) ] )
        ) {
            $fileList[] = $file;
        }
    }
    closedir($handle);

    if (count($fileList) > 0) {
        $imageNumber = time() % count($fileList);
        $img = $folder.$fileList[$imageNumber];
    }
}

if ($img!=null) {
    $imageInfo = pathinfo($img);
    $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    header ($contentType);
    readfile($img);
} else {
    if ( function_exists('imagecreate') ) {
        header ("Content-type: image/png");
        $im = @imagecreate (100, 100)
            or die ("Cannot initialize new GD image stream");
        $background_color = imagecolorallocate ($im, 255, 255, 255);
        $text_color = imagecolorallocate ($im, 0,0,0);
        imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
        imagepng ($im);
        imagedestroy($im);
    }
}

?>


推荐答案

生成电子标签 Cache-Control 标题会帮助您喜欢这个

Generating an E-tag and Cache-Control header will help you like this

<?php
  $folder   = '.';
  $extList  = array( "gif", "jpg", "jpeg", "png" );
  $image    = false;

  if ( substr( $folder, -1 ) != '/' ) {
    $folder = $folder.'/';
  }

  if ( isset( $_GET["img"] ) ) {
    $imageExtn  = strtolower( pathinfo( $_GET["img"], PATHINFO_EXTENSION ) );
    $imageName  = basename( $_GET["img"] );
    if ( in_array( $imageExtn, $extList ) && file_exists( $folder.$imageName ) ) {
      $image  = $folder.$imageName;
    }
  }
  else {
    $fileList = array();
    $handle   = opendir( $folder );
    while( false !== ( $file = readdir( $handle ) ) ) {
      $fileExtn = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
      if ( in_array( $fileExtn, $extList ) ) {
        $fileList[] = $file;
      }
    }
    closedir( $handle );

    if ( !empty( $fileList ) ) {
      $imageNumber  = time() % count( $fileList );
      $image  = $folder.$fileList[$imageNumber];
    }
  }

  if ( $image !== false ) {
    $contents = implode( "", file( $image ) );
    $md5  = md5( $contents );
    $mtime  = filemtime( $image );
    $etag = etag( $md5, $mtime );

    $r_mtime  = 0;
    $r_etag   = null;

    if ( isset( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
      $r_mtime  = strtotime( $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
    }
    if ( isset( $_SERVER["HTTP_IF_NONE_MATCH"] ) ) {
      $r_etag   = trim( $_SERVER["HTTP_IF_NONE_MATCH"] );
    }

    if ( $mtime == $r_mtime && $r_etag == $etag ) {
      header( "HTTP/1.0 304 Not Modified", true, 304 );
      header( "HTTP/1.1 304 Not Modified", true, 304 );
      header( "Content-length: 0" );
      exit;
    }

    header( "Content-type: image/".strtolower( pathinfo( $image, PATHINFO_EXTENSION ) ) );
    header( "Last-Modified: ".gmdate( "D, d M Y H:i:s", $mtime )." GMT" );
    header( "ETag: ".$etag );
    header( "Expires: ".gmdate( "D, d M Y H:i:s", time()+3600 )." GMT" );
    header( "Cache-Control: max-age=3600" );
    header( "Cache-Control: public" );

    echo $contents;
    exit();
  }
  else {
    if ( function_exists( "imagecreate" ) ) {
      header( "Content-type: image/png" );
      $im = @imagecreate( 100, 100 ) or die( "Cannot initialize new GD image stream" );
      $background_color = imagecolorallocate( $im, 255, 255, 255 );
      $text_color = imagecolorallocate( $im, 0, 0, 0 );
      imagestring( $im, 2, 5, 5,  "IMAGE ERROR", $text_color );
      imagepng( $im );
      imagedestroy( $im );
    }
  }

  function etag( $string_1 = null, $string_2 = null, $quote = true ) {
    $quote  = ( $quote ) ? '"' : '';
    $etag   = sprintf( $quote."%s-%s".$quote, $string_1, $string_2 );
    return $etag;
  }
?>

这篇关于在PHP脚本上缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆