2010-08-15 14 views
7

()) औसत 8-15 सेकंड पर ले जाताfile_get_contents() संदर्भ के साथ उपयोग करने के लिए http/1.1 काफी धीमी गति से डाउनलोड गति

प्रत्येक छवि डाउनलोड नीचे दिए गए कोड का उपयोग करना) file_get_contents ..... अगर मैं का उपयोग नहीं करते file_get_contents() पर एक संदर्भ तो छवि डाउनलोड एक सेकंड से भी कम है।

यदि मैं $ opts को नीचे बदलता हूं, तो मुझे एक संदर्भ के बिना file_get_contents() के समान प्रदर्शन मिलता है जो 2,500 छवियों को संसाधित करने के लिए 13 सेकंड एपॉक्स लेता है।

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

पुन: पेश:

$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 />'); 

उत्तर

9

HTTP 1.1 अनुरोध डिफ़ॉल्ट रूप से पाइपलाइन किए गए हैं। यदि आप "कनेक्शन: बंद करें" नहीं करते हैं, तो यह "कनेक्शन: Keep-Alive" मानता है, और फिर आपको अगले लूप शुरू होने से पहले कनेक्शन के समय के लिए प्रतीक्षा करना होगा (क्योंकि आपने कभी इसे स्पष्ट रूप से बंद नहीं किया है)।

+0

धन्यवाद! HTTP 1.0 पर 0.15s ले रहे अनुरोधों को HTTP 1.1 पर कम से कम 5s ले रहे थे। एक सरल 'हेडर ("कनेक्शन: बंद करें"); ठीक कर दिया! – Mave

0

आपका संदर्भ file_get_contents बताता है() HTTP कनेक्शन हर बार बंद हुआ। शायद यही कारण है कि कोड इतने लंबे समय तक लेता है, क्योंकि यह कई बार कनेक्शन बंद करता है और फिर से खोलता है? मैं file_get_contents() के आंतरिक से परिचित नहीं हूं, लेकिन आप अंतिम कनेक्शन के लिए "कनेक्शन: रखें-जिंदा" का उपयोग करने के लिए संदर्भ को ट्विक कर सकते हैं, और अंतिम के लिए "कनेक्शन: बंद करें"।

+1

मैं 'file_get_contents() 'को इसके कनेक्शन को बंद करने की अपेक्षा करता हूं - डिस्क से फ़ाइलों को पढ़ने पर यह फ़ाइल हैंडल बंद करता है। यदि आप प्रदर्शन चाहते हैं, तो curl एक बेहतर शर्त है। आप बस उसी कर्ल हैंडल का उपयोग फिर से कर सकते हैं; अगर मैं सही से याद करता हूं तो कनेक्शन डिफ़ॉल्ट रूप से खुला रहता है। –

+0

मैं curl का उपयोग करने के बारे में आपसे सहमत हूं, जब तक कोई कारण नहीं है कि dorgan को file_get_contents() का उपयोग करना है। –