Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Created April 14, 2013 13:05
Show Gist options
  • Save irazasyed/5382666 to your computer and use it in GitHub Desktop.
Save irazasyed/5382666 to your computer and use it in GitHub Desktop.
PHP: Get image raw data (cURL & file_get_contents support)
/*-------------------------------------------------+
| Gets Image Raw Data,
| Used with GD Lib (imagecreatefromstring)
+-------------------------------------------------*/
function getImageRawData($image_url) {
if (function_exists('curl_init')) {
$opts = array();
$http_headers = array();
$http_headers[] = 'Expect:';
$opts[CURLOPT_URL] = $image_url;
$opts[CURLOPT_HTTPHEADER] = $http_headers;
$opts[CURLOPT_CONNECTTIMEOUT] = 10;
$opts[CURLOPT_TIMEOUT] = 60;
$opts[CURLOPT_HEADER] = FALSE;
$opts[CURLOPT_BINARYTRANSFER] = TRUE;
$opts[CURLOPT_VERBOSE] = FALSE;
$opts[CURLOPT_SSL_VERIFYPEER] = FALSE;
$opts[CURLOPT_SSL_VERIFYHOST] = 2;
$opts[CURLOPT_RETURNTRANSFER] = TRUE;
$opts[CURLOPT_FOLLOWLOCATION] = TRUE;
$opts[CURLOPT_MAXREDIRS] = 2;
$opts[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
# Initialize PHP/CURL handle
$ch = curl_init();
curl_setopt_array($ch, $opts);
$content = curl_exec($ch);
# Close PHP/CURL handle
curl_close($ch);
}// use file_get_contents
elseif (ini_get('allow_url_fopen')) {
$content = file_get_contents($image_url);
}
# Return results
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment