PHP Echo Tutorials

Using getimagesize

Written by phpecho.com   

The function getimagesize() is very useful when it comes down to image hosting websites. It allows the user to fetch the width and height of any specified image or Flash SWF file.

The function getimagesize() returns an array with 5 indexes.

The first and second indexes are the width and height of the image, in integer format.

The third index is one of the IMAGETYPE_XXX constants indicating the type of the image.

The fourth index is a text string with the correct height="yyy" width="xxx" string that can be used directly in an <img> tag.

And the last index (named 'mime') is the file's Mime Type, which can be used in a Content-Type header.

An example of getimagesize() in action is below.

<?php
$size = getimagesize('http://www.zelune.com/img/design.gif');
echo  'Width: ' . $size[0] . 'px, Height:  ' . $size[1] . 'px';
?>

This would return the following:

Width: 600px, Height: 200px

You can also apply your own variables to the indexes with the list() function. Example:

<?php
list($width, $height, $type, $attr) = 
getimagesize('http://www.zelune.com/img/design.gif'); ?>

And that's all in this simple tutorial from phpecho.com! Thanks for reading!