105

I've tried to use the PHP function getimagesize, but I was unable to extract the image width and height as an integer value.

How can I achieve this?

Zuul
  • 16,217
  • 6
  • 61
  • 88
Mostafa Elkady
  • 5,645
  • 10
  • 45
  • 69

6 Answers6

214

Try like this:

list($width, $height) = getimagesize('path_to_image');

Make sure that:

  1. You specify the correct image path there
  2. The image has read access
  3. Chmod image dir to 755

Also try to prefix path with $_SERVER["DOCUMENT_ROOT"], this helps sometimes when you are not able to read files.

Peter Veris
  • 43
  • 3
  • 10
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 2
    777 on directory is not needed. – poke Feb 01 '10 at 18:57
  • @poke: are you really 100% sure? – Sarfraz Feb 18 '10 at 05:04
  • 6
    Yes. 777 means read, write and execute right for owner, group, and all. You need read and execute right to access a directory, but you don't need write right; and you also don't need that right for everybody. 755 should be fine for every access where you don't need to create files inside of the directory. – poke Feb 18 '10 at 18:51
  • 1
    Interesting to me that you need execute rights on the directory but not the image itself (works with the image on 644). – Lee Saxon Jan 10 '16 at 16:15
  • 1
    You need *allow_url_fopen* turned on to be able to use `getimagesize()` on remote images. – VertigoRay Mar 17 '17 at 14:09
  • @Lee: On a directory, x refers to traversal, not execution. I.e. permission to view the contents. – Dave Oct 20 '18 at 22:45
  • Can you explain why I am getting the correct data this way list($height, $width) = getimagesize('path_to_image'); array 0 index is height and 1 index is width – notcontrol Apr 06 '23 at 14:19
69
list($width, $height) = getimagesize($filename)

Or,

$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
davethegr8
  • 11,323
  • 5
  • 36
  • 61
13

getimagesize() returns an array containing the image properties.

list($width, $height) = getimagesize("path/to/image.jpg");

to just get the width and height or

list($width, $height, $type, $attr)

to get some more information.

Michael Robinson
  • 1,985
  • 2
  • 21
  • 31
Matt
  • 1,247
  • 8
  • 16
7

Like this :

imageCreateFromPNG($var);
//I don't know where from you get your image, here it's in the png case
// and then :
list($width, $height) = getimagesize($image);
echo $width;
echo $height;
Julien
  • 9,312
  • 10
  • 63
  • 86
  • Don't work: `getimagesize() expects parameter 1 to be string, resource given`, function `getimagesize` expects filename – lopisan Aug 14 '15 at 07:48
6

getimagesize('image.jpg') function works only if allow_url_fopen is set to 1 or On inside php.ini file on the server, if it is not enabled, one should use ini_set('allow_url_fopen',1); on top of the file where getimagesize() function is used.

ronforever
  • 79
  • 1
  • 4
6

PHP's getimagesize() returns an array of data. The first two items in the array are the two items you're interested in: the width and height. To get these, you would simply request the first two indexes in the returned array:

var $imagedata = getimagesize("someimage.jpg");

print "Image width  is: " . $imagedata[0];
print "Image height is: " . $imagedata[1];

For further information, see the documentation.

Sampson
  • 265,109
  • 74
  • 539
  • 565