Jump to content

[SOLVED] cURL get Image


The Little Guy

Recommended Posts

Anyone know how I can use cURL to get an image from a url?

 

<?php
$ch = curl_init("http://share.meebo.com/content/katy_perry/wallpapers/3.jpg");

curl_setopt($ch, CURLOPT_HEADER, TRUE);

curl_exec($ch);
curl_close($ch);
?>

 

The above gets the image, but then it just displays the file contents as text, and not an image see here:

http://dudeel.com/process/build/newTempImg.php

 

(my server doesn't support fopen/fread/fwrite on url's)

Link to comment
https://forums.phpfreaks.com/topic/120308-solved-curl-get-image/
Share on other sites

OK, I found something...

 

 

<?php
$url = "http://share.meebo.com/content/katy_perry/wallpapers/3.jpg";

$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

$image = curl_exec($ch);
curl_close($ch);

// output to browser
header("Content-type: image/jpeg");
echo $image;
?>

 

Another question I have, how could I take this, and resize the image?

GOT IT!

 

If anyone wants to do this, just try this:

 

$url = $_GET['url'];

$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

$image = curl_exec($ch);
curl_close($ch);

// output to browser
header("Content-type: image/jpeg");
//echo $image;
$im = imagecreatefromstring($image);

$tw = imagesx($im);
$th = imagesy($im);
$thumbWidth = 100;
$thumbHeight = $th * ($thumbWidth / $tw);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $im, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $tw, $th);


imagejpeg($thumbImg, NULL, 100);
imagedestroy($thumbImg);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.