I was wondering how can I find the width of an image using php.
In addition to getimagesize()
you can get the dimensions from an image resource using imagesx()
and imagesy()
.
First of all you will have to download the image data using for example file_get_contents()
Then you create the image resource using imagecreatefromstring()
Finally get the image width with imagesx()
and the image height with imagesy()
.
function get_image_dimensions($img_url=""){
$image_dimensions=array();
$image_dimensions['w']=0;
$image_dimensions['h']=0;
if($image_data=custom_file_get_contents($img_url)){
$image_resource = imagecreatefromstring($image_data);
$image_dimensions['w']=imagesx($image_resource);
$image_dimensions['h']=imagesy($image_resource);
imagedestroy($image_resource);
}
return $image_dimensions;
}