file_get_contents() مع سياق لاستخدام http/1.1 يبطئ سرعات التنزيل بشكل ملحوظ
سؤال
يستغرق استخدام الكود الموجود أسفل كل تنزيل صورة) file_get_contents() ) في المتوسط من 8 إلى 15 ثانية.....
إذا لم أستخدم سياقًا في file_get_contents()، فسيكون تنزيل الصورة أقل من ثانية.
إذا قمت بتغيير $opts إلى، أدناه، سأحصل على نفس الأداء مثل file_get_contents() بدون سياق يستغرق حوالي 13 ثانية لمعالجة 2500 صورةx.
$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( ' File not cached or cached file has expired<br />' );
if ( $gen_file ) {
echo( ' 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( ' 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( ' Checking For Directory<br />' );
if ( !is_dir( '/tmp/' .$folder ) ) {
mkdir( '/tmp/' .$folder );
}
echo( ' Writing File<br />' );
$new_file = '/tmp/' .$folder .'/' .$code .'_' .$def_width .'.jpg';
imagejpeg( $thumb_image_resized, $new_file, 100);
echo( ' DONE<br />' );
imagedestroy($img);
imagedestroy($thumb_image_resized);
} else {
echo( 'Problem Getting Image<br />' );
die();
}
} else {
echo( ' Already Exists<br />' );
}
}
}
$end_time = mktime();
echo( 'Completed In...' .($end_time - $start_time ) .' seconds(s)<br />' );
المحلول
يتم توجيه طلبات HTTP 1.1 بشكل افتراضي.إذا لم تقم بذلك "الاتصال:إغلاق"، فإنه يفترض "الاتصال:Keep-Alive"، ثم يتعين عليك الانتظار حتى تنتهي مهلة الاتصال (نظرًا لأنك لم تغلقه بشكل صريح أبدًا) قبل أن تبدأ الحلقة التالية.
نصائح أخرى
يخبر السياق الخاص بك file_get_contents() بإغلاق اتصال HTTP في كل مرة.ربما هذا هو السبب في أن الكود يستغرق وقتًا طويلاً، حيث أنه يغلق ويعيد فتح الاتصالات عدة مرات؟لست على دراية بالأجزاء الداخلية لـ file_get_contents()، ولكن قد تتمكن من تعديل السياق لاستخدام "الاتصال:"البقاء على قيد الحياة" للجميع باستثناء الاتصال الأخير، و"الاتصال:إغلاق" للأخير.