代码之家  ›  专栏  ›  技术社区  ›  Dagg Nabbit

通过“print fread(…)”提供图像速度很慢,怎么办?

  •  0
  • Dagg Nabbit  · 技术社区  · 15 年前

    我有一个动态缩略图脚本,我发现周围的网络和调整了一点。我添加了一个缓存机制。每当生成新的缩略图时,它都会保存到磁盘,如果再次请求相同的缩略图(具有所有相同的选项),则将使用磁盘副本。

    一个片段:

      // name of cached file
      $thumb_file = $_SERVER['DOCUMENT_ROOT'].'/thumbs/cache/'.
                    str_replace('/', '_', $_REQUEST['p']).
                    ".{$def_width}x{$def_height}".
                    ($clamp ? '_'.implode('x',$clamp) : '').
                    ($make_png?'.png':'.jpg');
    
      // get it from cache if it's there
      if ($use_cache && file_exists($thumb_file)) {
        Header("Content-type: image/".($make_png?'png':'jpeg'));
    
        // this part seems really slow
    
        $fp=fopen($thumb_file, "rb");
        while (!feof($fp)) print fread($fp, 4096);
    
        exit();
      }
    

    然而, print fread

    弗雷德 还是有别的选择?

    为了以防万一,我在下面提供了完整的PHP脚本。

    <?php
    
      $use_cache = $_REQUEST['nc'] ? false : true;
      // $use_cache = false;
    
      $upfile = $_SERVER['DOCUMENT_ROOT'] .'/'. $_REQUEST['p'];
      $def_width  = $_REQUEST["w"];
      $def_height = $_REQUEST["h"];
      $clamp = $_REQUEST['c'] ? explode("x",$_REQUEST['c']) : null;
      $make_png = $_REQUEST['png'];
    
      if (!file_exists($upfile)) {
        die();  // $upfile = "nophoto.jpg";
      }
    
      if (!"{$def_width}{$def_height}") {
        $def_width = $def_height = '100';
      }
    
      // name of cached file
      $thumb_file = $_SERVER['DOCUMENT_ROOT'].'/thumbs/cache/'.
                    str_replace('/', '_', $_REQUEST['p']).
                    ".{$def_width}x{$def_height}".
                    ($clamp ? '_'.implode('x',$clamp) : '').
                    ($make_png?'.png':'.jpg');
    
      // get it from cache if it's there
      if ($use_cache && file_exists($thumb_file)) {
        Header("Content-type: image/".($make_png?'png':'jpeg'));
        $fp=fopen($thumb_file, "rb");
        while (!feof($fp)) print fread($fp, 4096);
        exit();
      }
    
      $ext = strtolower(substr($upfile, -3));
    
      ini_set('memory_limit', '64M');
    
      if ($ext=="gif") 
        $src = @ImageCreateFromGif ($upfile);
      else if ($ext=="jpg") 
        $src = @ImageCreateFromJpeg($upfile);
      else if ($ext=="png") 
        $src = @ImageCreateFromPng($upfile);
    
      $size = GetImageSize($upfile); 
      $width = $size[0];
      $height = $size[1];
    
      $long_side = $def_width;
      if ($def_width < $def_height) $long_side = $def_height;
    
      if (!$def_width) {
        $factor_h = $height / $def_height;
        $def_width = $width / $factor_h;
      }
      if (!$def_height) {
        $factor_w = $width / $def_width;
        $def_height = $height / $factor_w;
      }
      $factor_w = $width / $def_width;
      $factor_h = $height / $def_height;
    
      if ($factor_w > $factor_h) {
        $new_height = floor($def_height * $factor_h);
        $new_width = floor($def_width  * $factor_h);
      } else {
        $new_height = floor($def_height * $factor_w);
        $new_width = floor($def_width  * $factor_w);
      }
    
      if ((!$clamp[0])&&$clamp[0]!=='0') $clamp[0] = 50;
      if ((!$clamp[1])&&$clamp[1]!=='0') $clamp[1] = 50;
    
      $src_x = ceil(($width  - $new_width)  * ($clamp[0] / 100));
      $src_y = ceil(($height - $new_height) * ($clamp[1] / 100));
    
      $dst = ImageCreateTrueColor($def_width, $def_height);
    
      @ImageCopyResampled($dst, $src, 0, 0, $src_x, $src_y, 
                          $def_width, $def_height, $new_width, $new_height);
    
      Header("Content-type: image/".($make_png?'png':'jpeg'));
    
      if ($make_png) {
        ImagePng($dst);
        if ($use_cache) { 
          ImagePng($dst, $thumb_file); 
        }
      } else {
        ImageJpeg($dst, null, 95);
        if ($use_cache) { 
          ImageJpeg($dst, $thumb_file, 95); 
        }
      }
    
      @ImageDestroy($src);
      @ImageDestroy($dst);
    
    ?>
    
    4 回复  |  直到 15 年前
        1
  •  0
  •   Your Common Sense    15 年前

    请用某些数字来定义“似乎”和“非常慢”。
    否则,答案只能用同样的术语来表达——“你似乎可以大致得到一些东西”。

    如果有人问“我如何加速”,应该首先回答以下问题:

    • 我想加快什么行动


    所以,如果它运行“非常慢”,可能还有其他一些原因。由于某些错误,这种缓存机制根本不起作用。或者别的什么。一些 剖析 ,以及 调试 在任何人能帮忙之前。

    尤其是那些会尖叫“这不是答案!”(因为那个愚蠢的假设,答案只算数,它是直接的,而且 积极的 ),这里有一个供OP学习的链接:
    http://shiftingpixel.com/2008/03/03/smart-image-resizer/

        2
  •  1
  •   timdev    15 年前

    函数 readfile

    如果您使用PHP作为Apache模块,还可以查看 virtual .

        3
  •  0
  •   gone    15 年前

    如果您的web服务器支持它, X-Sendfile

        4
  •  0
  •   SamGoody    15 年前

    移到PHP5。

    升级,最好至少升级到5.3+

    推荐文章