54

I have an url to the image, like $var = http://example.com/image.png

How do I get its dimensions to an array, like array([h]=> 200, [w]=>100) (height=200, width=100)?

jooas
  • 117
  • 1
  • 9
James
  • 42,081
  • 53
  • 136
  • 161

3 Answers3

140

You can use the getimagesize function like this:

list($width, $height) = getimagesize('path to image');
echo "width: " . $width . "<br />";
echo "height: " .  $height;
Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    OP did ask "How do I get its dimensions to an array" so Im not sure why this is the fully accepted answer... GJ though. – Dutchie432 Oct 08 '10 at 20:41
  • 5
    @Dutchie432: He mainly meant how to get image dimensions and creating an array once you have width and height is/should be easy :) – Sarfraz Oct 08 '10 at 20:50
  • 1
    I think it's safe to say that's your (likely correct) assumption, and not addressing the specifics of the question. – Dutchie432 Oct 12 '10 at 14:05
  • This option does not support the https:// protocol. Anyone know of any good options for secure image size detection? – JustinKaz Nov 25 '18 at 16:07
  • 1
    @JustinKaz It supports https, but your server needs to have openSSL. https://stackoverflow.com/a/8518616/76672 https is working well for me, for example. – Jake Feb 22 '19 at 00:25
  • @Dutchie432 `getimagesize` returns an array... `list` lists the array items out into variables. – Jake Feb 22 '19 at 00:26
45

Using getimagesize function, we can also get these properties of that specific image-

<?php

list($width, $height, $type, $attr) = getimagesize("image_name.jpg");

echo "Width: " .$width. "<br />";
echo "Height: " .$height. "<br />";
echo "Type: " .$type. "<br />";
echo "Attribute: " .$attr. "<br />";

//Using array
$arr = array('h' => $height, 'w' => $width, 't' => $type);
?>

**Result like this -**

Width: 200
Height: 100
Type: 2
Attribute: width='200' height='100'


Type of image consider like -

1 = GIF
2 = JPG
3 = PNG
4 = SWF
5 = PSD
6 = BMP
7 = TIFF(intel byte order)
8 = TIFF(motorola byte order)
9 = JPC
10 = JP2
11 = JPX
12 = JB2
13 = SWC
14 = IFF
15 = WBMP
16 = XBM

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
20
<?php 
    list($width, $height) = getimagesize("http://site.com/image.png"); 
    $arr = array('h' => $height, 'w' => $width );
?>
Dutchie432
  • 28,798
  • 20
  • 92
  • 109