Problems with Curl result and file_get_contents

4

I'm trying to get an image from a website to use imagecreatefrompng (), but the result was never what I expected ...

Url: link in the case when I call as <img> works perfectly

<img src="https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m">

TheproblemisthatwhenIcallcurlitreturnsthis:

  

GIF89a6>XXXLLL555AAAFFF))///_t#4ه*ílQЂ!(c)  sulake!,6>H*\ȰÇ#JHŋ3jȱǏCIɓ(S\J-c  ʘI͓(P@ѣpI2D"M Td˧ 5ʵ ( ? = Uk ׯ h vp @ 6e " E @ n, l @ ݝ & lt ; ^ d vo ٿ w L "HLy b;   ? [9q j ~ : AӤ qD o9 Z & ! > p 3m ( ;3�CؓoS�s��3�H8�e��[.����t����߾�]�s�������4[ �� ̘ z ~ 5 \ @ k _L < ��7�~��t * aKV &   nx }! # h B b . 0 ~@ 8 H! JĖh> J    / ӎ g & 4a ) 7 ! / $ Ӆ) Kh l p ) tB;

I have tried to use file_get_contents but it is an https url and it does not work, I want it to return the image to be able to use with php's imagecreatefrompng ()

code I've used:

function getimg($url) {
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';

    $process = curl_init($url);

    curl_setopt($process, CURLOPT_URL, $url);
    curl_setopt($process, CURLOPT_HEADER, false);
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate, sdch'));
    curl_setopt($process, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);

    $return = curl_exec($process);
    curl_close($process);

    return $return;
}

echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');

//echo '<img src="'.getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m').'">';
//$habbo = imagecreatefrompng('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');
//imagecopy($im, $habbo, -2, -9, 0, 0, 33, 33);
    
asked by anonymous 18.01.2017 / 14:16

2 answers

3

I just successfully tested here :

Infactitcanbesimplerthanwhatyouweredoing:

functiongetimg($url){$process=curl_init();curl_setopt($process,CURLOPT_URL,$url);curl_setopt($process,CURLOPT_RETURNTRANSFER,true);curl_setopt($process,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);$return=curl_exec($process);curl_close($process);return$return;}header("Content-Type: image/jpeg");
echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');

Here's a solution with file_get_contents ():

function getimg($url) {
    $options = array(
        'http'=>array(
            'method'=> "GET",
            'header'=> "Accept-language: en\r\n" .
            'User-Agent: ' .$_SERVER['HTTP_USER_AGENT']. "\r\n"
        )
    );
    $context = stream_context_create($options);
    return file_get_contents($url, false, $context);
}
header("Content-Type: image/jpeg");
echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');
    
18.01.2017 / 14:52
1

You can use the following code, which uses file_get_contents while instead of curl , it is passed User-Agent so that the connection is not rejected, mime-type is also automatically captured by the finfo .

To avoid errors in obtaining mime-type , check in php.ini if the fileinfo dll is enabled extension=php_fileinfo.dll without ; .

function getimg($url) {
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (Android; Mobile; rv:40.0) Gecko/40.0 Firefox/40.0
\r\n" // Edite com o user agent que você deseja usar
  )
);

$context = stream_context_create($options);
$image = file_get_contents($url, false, $context);

//Obtem o Mime Type
$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer($image);

return 'data:'.$mime_type.';base64,'.base64_encode($image);
} 

echo '<img src="'.getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m').'">';

With this you can use <img src= to use the images directly in the code without the need to use header .

    
18.01.2017 / 16:18