Php 文件\u get\u contents()与上下文一起使用http/1.1大大降低了下载速度
使用下面的代码(每个图像下载)文件_get_contents())平均需要8-15秒 如果我不在文件_get_contents()上使用上下文,则图像下载不到一秒钟 如果我将下面的$opts改为,那么我将获得与file_get_contents()相同的性能,而没有处理2500个图像需要大约13秒的上下文Php 文件\u get\u contents()与上下文一起使用http/1.1大大降低了下载速度,php,http,http-1.1,Php,Http,Http 1.1,使用下面的代码(每个图像下载)文件_get_contents())平均需要8-15秒 如果我不在文件_get_contents()上使用上下文,则图像下载不到一秒钟 如果我将下面的$opts改为,那么我将获得与file_get_contents()相同的性能,而没有处理2500个图像需要大约13秒的上下文 $opts = array( 'http'=>array( 'protocol_version'=>'1.1', 'method'=>
$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 />' );
$start_time=mktime();
$products=数组(
数组('code'=>'A123','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A124','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A125','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A126','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A127','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A128','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A134','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A135','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A146','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
数组('code'=>'A165','image\u url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' )
);
如果(计数($产品)>0){
$opts=数组(
“http'=>数组(
“协议_版本”=>“1.1”,
'方法'=>'获取',
“用户代理”=>“图像大小调整器”
)
);
$context=stream\u context\u create($opts);
$def_width=100;
$max_width=$def_width;
foreach($products as$product){
$code=$product['code'];
$folder=substr($code,0,3);
echo('Looking:'.$product['code'].“
”);
$file='/tmp/'.$folder.'/'.$code.''.$def_width.'.jpg';
$filemtime=@filemtime($file);
$gen_file=true;
如果($filemtime!==false){
$file_age=(time()-$filemtime);
如果($file\u age=$image\u x){
$def_width=$image_x;
}
回波(‘计算标度
’);
$ts=最小值($max_width/$image_x,$max_width/$image_y);
$thumbhght=$ts*$image\u y;
$thumbwth=$ts*$image\u x;
$thumb\u image\u resized=imagecreatetruecolor($thumbwth,$thumbhght);
imagecopyresampled($thumb\u image\u resized,$img,0,0,0,0,$thumbwth,$thumbhght,$image\u x,$image\u y);
echo('checkingfordirectory
');
如果(!is_dir('/tmp/'.$folder)){
mkdir('/tmp/'.$folder);
}
echo('Writing File
');
$new_file='/tmp/'.$folder.'/'.$code.''.$def_width..jpg';
imagejpeg($thumb\u image\u resized,$new\u file,100);
回声('DONE
');
图像处理(img);
imagedestroy($thumb_image_resized);
}否则{
回音('problemgetingimage
');
模具();
}
}否则{
回音('已存在
');
}
}
}
$end_time=mktime();
回音('Completed In…'。($end_time-$start_time)。'seconds(s)
);
您的上下文每次都告诉文件\u get\u contents()关闭HTTP连接。也许这就是为什么代码需要这么长时间,因为它会多次关闭和重新打开连接?我不熟悉file_get_contents()的内部结构,但您可以调整上下文,对除最后一个连接外的所有连接使用“连接:保持活动”,对最后一个连接使用“连接:关闭”。HTTP 1.1请求默认为管道化。如果您没有“连接:关闭”,它会假定“连接:保持活动”,然后您必须等待连接超时(因为您从未显式关闭它),然后下一个循环才会开始。我希望file\u get\u contents()
无论如何都会关闭其连接——从磁盘读取文件时,它会关闭文件句柄。如果您想要性能,cURL是更好的选择。你可以简单地重复使用同一个卷曲手柄;如果我没记错的话,默认情况下连接保持打开状态。我同意您使用cURL,除非有理由dorgan必须使用file_get_contents()。谢谢!在HTTP 1.0上花费0.15秒的请求在HTTP 1.1上至少花费5秒。一个简单的`标题(“连接:关闭”);修好了!