0

下载远程图像然后将其显示给用户的最快最节省内存的方法是什么?- 注意我不想保存它,我只想将它传递给用户。

即用户访问 www.website.com/image1.jpg

在后台,我将使用与此类似的 getFile 脚本来检索文件,但是如何将其显示给用户,这是一种高效的内存方式吗?

function getUrl($url, $method='', $vars='', $fh = '') {
    $ch = curl_init();
    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    if ($fh != '')
    {
        curl_setopt($ch, CURLOPT_FILE, $fh);
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
    $buffer = curl_exec($ch);

    curl_close($ch);
    return $buffer;
}
4

1 回答 1

1

这里是:

function proxyImage( $fromHost, $path, $bufsize=4096 ) {
  $conn = fsockopen($fromHost,80);
  fwrite( $conn, "GET {$path} HTTP/1.0\r\n" );
  fwrite( $conn, "Host: {$fromHost}\r\n" );
  fwrite( $conn, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0\r\n" );
  fwrite( $conn, "Connection: close\r\n" );
  fwrite( $conn, "\r\n" );

  $answer = ''; 
  while( !feof($conn) and !$bodystarted ) {
    $portion = fread( $conn, $bufsize );
    if( strpos($portion,"\r\n\r\n")!==false ) $bodystarted = true;
    $answer .= $portion;
  }
  list( $headers, $bodypart ) = explode( "\r\n\r\n", $answer, 2 );

  foreach( explode("\r\n",$headers) as $h )
    header($h);

  echo $bodypart;
  while( !feof($conn) )
    echo fread( $conn, $bufsize );
  fclose($conn);
}

proxyImage( 'www.newyorker.com', '/online/blogs/photobooth/NASAEarth-01.jpg' );
于 2013-10-17T19:09:41.580 回答