2010-08-15 20 views
7

Sử dụng mã bên dưới mỗi tải hình ảnh) file_get_contents()) mất trung bình 8-15 giây .....file_get_contents() với ngữ cảnh để sử dụng http/1.1 chậm đáng kể tốc độ tải

Nếu tôi không sử dụng một bối cảnh trên file_get_contents() sau đó tải xuống hình ảnh nhỏ hơn một giây.

Nếu tôi thay đổi $ opts thành, bên dưới thì tôi nhận được hiệu suất giống như file_get_contents() không có bối cảnh mất 13 giây để xử lý 2.500 imagesx.

$opts = array(
    'http'=>array(
     'protocol_version'=>'1.1', 
     'method'=>'GET', 
     'header'=>array(
      'Connection: close' 
     ), 
     'user_agent'=>'Image Resizer' 
    ) 
); 

tái sản xuất:

$start_time = mktime(); 
$products = array(
     array('code'=>'A123', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A124', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A125', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A126', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A127', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A128', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A134', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A135', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A146', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'), 
     array('code'=>'A165', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png') 
    ); 

    if (count($products) > 0) { 
     $opts = array(
      'http'=>array(
       'protocol_version'=>'1.1', 
       'method'=>'GET', 
       'user_agent'=>'Image Resizer' 
      ) 
     ); 
     $context = stream_context_create($opts); 
     $def_width = 100; 
     $max_width = $def_width; 
     foreach($products as $product) { 
      $code = $product['code']; 
      $folder = substr($code, 0, 3); 
      echo('Looking at: ' .$product['code'] ."<br />"); 
      $file = '/tmp/' .$folder .'/' .$code .'_' .$def_width .'.jpg'; 
      $filemtime = @filemtime($file); 
      $gen_file = true; 
      if ($filemtime !== false) { 
       $file_age = (time() - $filemtime); 
       if ($file_age <= 300) { 
        $gen_file = false; 
       } 
      } 
      echo('&nbsp;&nbsp;&nbsp;&nbsp;File not cached or cached file has expired<br />'); 
      if ($gen_file) { 
       echo('&nbsp;&nbsp;&nbsp;&nbsp;Getting File...'); 
       $imgStr = file_get_contents($product['image_url'], false, $context); 
       $img = @imagecreatefromstring($imgStr); 
       if (is_resource($img)) { 
        echo('DONE' .'<br />'); 
        $image_x = imagesx($img); 
        $image_y = imagesy($img); 
        if ($def_width >= $image_x) { 
         $def_width = $image_x; 
        } 
        echo('&nbsp;&nbsp;&nbsp;&nbsp;Calculating Scale<br />'); 
        $ts = min($max_width/$image_x,$max_width/$image_y); 
        $thumbhght = $ts * $image_y; 
        $thumbwth = $ts * $image_x; 

        $thumb_image_resized = imagecreatetruecolor($thumbwth, $thumbhght); 
        imagecopyresampled($thumb_image_resized, $img, 0, 0, 0, 0, $thumbwth, $thumbhght, $image_x, $image_y); 
        echo('&nbsp;&nbsp;&nbsp;&nbsp;Checking For Directory<br />'); 
        if (!is_dir('/tmp/' .$folder)) { 
         mkdir('/tmp/' .$folder); 
        } 
        echo('&nbsp;&nbsp;&nbsp;&nbsp;Writing File<br />'); 
        $new_file = '/tmp/' .$folder .'/' .$code .'_' .$def_width .'.jpg'; 

        imagejpeg($thumb_image_resized, $new_file, 100); 
        echo('&nbsp;&nbsp;&nbsp;&nbsp;DONE<br />'); 

        imagedestroy($img); 
        imagedestroy($thumb_image_resized); 
       } else { 
        echo('Problem Getting Image<br />'); 
        die(); 
       } 
      } else { 
       echo('&nbsp;&nbsp;&nbsp;&nbsp;Already Exists<br />'); 
      } 
     } 
    } 
    $end_time = mktime(); 
    echo('Completed In...' .($end_time - $start_time) .' seconds(s)<br />'); 

Trả lời

9

Yêu cầu HTTP 1.1 được pipelined theo mặc định. Nếu bạn không "Connection: Close", nó giả định "Connection: Keep-Alive", và sau đó bạn phải đợi kết nối hết thời gian (vì bạn chưa bao giờ đóng nó một cách rõ ràng) trước khi vòng lặp tiếp theo bắt đầu.

+0

Cảm ơn bạn! Các yêu cầu sử dụng 0,15 giây trên HTTP 1.0 đã lấy ít nhất 5 giây trên HTTP 1.1. Một tiêu đề đơn giản ("Connection: close"); cố định nó! – Mave

0

bối cảnh của bạn nói với file_get_contents() để đóng kết nối HTTP mỗi lần. Có lẽ đó là lý do tại sao mã mất quá nhiều thời gian, vì nó đóng và mở lại các kết nối nhiều lần? Tôi không quen thuộc với nội bộ của file_get_contents(), nhưng bạn có thể tinh chỉnh ngữ cảnh để sử dụng "Connection: keep-alive" cho tất cả trừ kết nối cuối cùng và "Connection: close" cho lần cuối cùng.

+1

Tôi mong đợi 'file_get_contents()' đóng kết nối của nó bất kể - nó đóng chốt xử lý khi đọc tệp từ đĩa. Nếu bạn muốn thực hiện, cURL là một cược tốt hơn. Bạn chỉ có thể tái sử dụng cùng một tay cầm curl nhiều lần; kết nối được giữ mở theo mặc định, nếu tôi nhớ chính xác. –

+0

Tôi đồng ý với bạn về việc sử dụng cURL, trừ khi có lý do dorgan phải sử dụng file_get_contents(). –

Các vấn đề liên quan