Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gurindersingh/c6e9470512d2052d93188c5be556d849 to your computer and use it in GitHub Desktop.
Save gurindersingh/c6e9470512d2052d93188c5be556d849 to your computer and use it in GitHub Desktop.
Get Remote Image Dimensions With PHP and cURL - GetImageSize Alternative
<?php
/** 
* Retrieve remote image dimensions 
* - getimagesize alternative 
*/
/**
* Get Image Size 
* @param string $url 
* @param string $referer 
* @return array 
*/
function getimgsize( $url, $referer = '' ) {
    // Set headers   
$headers = array( 'Range: bytes=0-131072' );   
if ( !empty( $referer ) ) { array_push( $headers, 'Referer: ' . $referer ); }
// Get remote image
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
$data = curl_exec( $ch );
$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$curl_errno = curl_errno( $ch );
curl_close( $ch );
   
// Get network stauts
if ( $http_status != 200 ) {
echo 'HTTP Status[' . $http_status . '] Errno [' . $curl_errno . ']';
return [0,0];
}
// Process image
$image = imagecreatefromstring( $data );
$dims = [ imagesx( $image ), imagesy( $image ) ];
imagedestroy($image);
return $dims;
}
// Set image url
$url = '';
// Get image dimensions
list( $width, $heigth) = getimgsize( $url );
// Doen something?
echo $width.' x '.$heigth;
//end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment