The Little Guy Posted August 19, 2008 Share Posted August 19, 2008 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 More sharing options...
The Little Guy Posted August 19, 2008 Author Share Posted August 19, 2008 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? Link to comment https://forums.phpfreaks.com/topic/120308-solved-curl-get-image/#findComment-619854 Share on other sites More sharing options...
The Little Guy Posted August 19, 2008 Author Share Posted August 19, 2008 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); Link to comment https://forums.phpfreaks.com/topic/120308-solved-curl-get-image/#findComment-619881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.