Get remote image using cURL then resample.

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-13T12:25:46Z Indexed on 2010/04/13 12:42 UTC
Read the original article Hit count: 355

Filed under:
|
|
|

I want to be able to retrieve a remote image from a webserver, resample it, and then serve it up to the browser AND save it to a file. Here is what I have so far:

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$rURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$out = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

$imgRes = imagecreatefromstring($out);
imagejpeg($imgRes, $filename, 70);

header("Content-Type: image/jpg");
header("Content-length: ".filesize($filename));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

Update

Updated code to include imjpeg step to save the image as lower quality. But how do I then, efficiently, serve this up to the browser. I currently, later in the code, do this readfile("$filename"); along with some header information but that means I'm reading the file back in again which seems inefficient.

© Stack Overflow or respective owner

Related posts about php

Related posts about gd