<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get the size of an image</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.gd-info.html">gd_info</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getimagesizefromstring.html">getimagesizefromstring</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.getimagesize" class="refentry"> <div class="refnamediv"> <h1 class="refname">getimagesize</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">getimagesize</span> — <span class="dc-title">Get the size of an image</span></p> </div> <div class="refsect1 description" id="refsect1-function.getimagesize-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>getimagesize</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$imageinfo</code></span> ] )</div> <p class="para rdfs-comment"> The <span class="function"><strong>getimagesize()</strong></span> function will determine the size of any supported given image file and return the dimensions along with the file type and a <em>height/width</em> text string to be used inside a normal <acronym title="Hyper Text Markup Language">HTML</acronym> <code class="tag">IMG</code> tag and the correspondent <acronym title="Hypertext Transfer Protocol">HTTP</acronym> content type. </p> <p class="para"> <span class="function"><strong>getimagesize()</strong></span> can also return some more information in <code class="parameter">imageinfo</code> parameter. </p> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> This function expects <code class="parameter">filename</code> to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values. </p> <p class="para"> Do not use <span class="function"><strong>getimagesize()</strong></span> to check that a given file is a valid image. Use a purpose-built solution such as the <a href="book.fileinfo.html" class="link">Fileinfo</a> extension instead. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Note that JPC and JP2 are capable of having components with different bit depths. In this case, the value for "bits" is the highest bit depth encountered. Also, JP2 files may contain <em>multiple JPEG 2000 codestreams</em>. In this case, <span class="function"><strong>getimagesize()</strong></span> returns the values for the first codestream it encounters in the root of the file. </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> The information about icons are retrieved from the icon with the highest bitrate. </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> GIF images consist of one or more frames, where each frame may only occupy part of the image. The size of the image which is reported by <span class="function"><strong>getimagesize()</strong></span> is the overall size (read from the logical screen descriptor). </span> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.getimagesize-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">filename</code></dt> <dd> <p class="para"> This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the <a href="wrappers.html" class="link">supported streams</a>. </p> </dd> <dt> <code class="parameter">imageinfo</code></dt> <dd> <p class="para"> This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different <acronym>JPG</acronym> APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed <a href="http://www.iptc.org/" class="link external">» IPTC</a> information in the APP13 marker. You can use the <span class="function"><a href="function.iptcparse.html" class="function">iptcparse()</a></span> function to parse the binary APP13 marker into something readable. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <code class="parameter">imageinfo</code> only supports <acronym>JFIF</acronym> files. </p> </p></blockquote> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.getimagesize-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array with up to 7 elements. Not all image types will include the <em>channels</em> and <em>bits</em> elements. </p> <p class="para"> Index 0 and 1 contains respectively the width and the height of the image. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Some formats may contain no image or may contain multiple images. In these cases, <span class="function"><strong>getimagesize()</strong></span> might not be able to properly determine the image size. <span class="function"><strong>getimagesize()</strong></span> will return zero for width and height in these cases. </p> </p></blockquote> <p class="para"> Index 2 is one of the <a href="image.constants.html" class="link">IMAGETYPE_XXX constants</a> indicating the type of the image. </p> <p class="para"> Index 3 is a text string with the correct <em>height="yyy" width="xxx"</em> string that can be used directly in an <acronym>IMG</acronym> tag. </p> <p class="para"> <em>mime</em> is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP <em>Content-type</em> header: <div class="example" id="example-3730"> <p><strong>Example #1 <span class="function"><strong>getimagesize()</strong></span> and MIME types</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$size </span><span style="color: #007700">&& </span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-type: </span><span style="color: #007700">{</span><span style="color: #0000BB">$size</span><span style="color: #007700">[</span><span style="color: #DD0000">'mime'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">fpassthru</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /> exit;<br />} else {<br /> </span><span style="color: #FF8000">// error<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <em>channels</em> will be 3 for RGB pictures and 4 for CMYK pictures. </p> <p class="para"> <em>bits</em> is the number of bits for each color. </p> <p class="para"> For some image types, the presence of <em>channels</em> and <em>bits</em> values can be a bit confusing. As an example, <acronym title="Graphic Interchange Format">GIF</acronym> always uses 3 channels per pixel, but the number of bits per pixel cannot be calculated for an animated <acronym title="Graphic Interchange Format">GIF</acronym> with a global color table. </p> <p class="para"> On failure, <strong><code>FALSE</code></strong> is returned. </p> </div> <div class="refsect1 errors" id="refsect1-function.getimagesize-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If accessing the <code class="parameter">filename</code> image is impossible <span class="function"><strong>getimagesize()</strong></span> will generate an error of level <strong><code>E_WARNING</code></strong>. On read error, <span class="function"><strong>getimagesize()</strong></span> will generate an error of level <strong><code>E_NOTICE</code></strong>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.getimagesize-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.1.0</td> <td> Added WebP support. </td> </tr> <tr> <td>5.3.0</td> <td> Added icon support. </td> </tr> <tr> <td>5.2.3</td> <td> Read errors generated by this function downgraded to <strong><code>E_NOTICE</code></strong> from <strong><code>E_WARNING</code></strong>. </td> </tr> <tr> <td>4.3.2</td> <td> Support for <acronym title="Japan Picture">JPC</acronym>, <acronym title="JPEG-2000">JP2</acronym>, <acronym>JPX</acronym>, <acronym title="Joint Bilevel Image Experts Group">JB2</acronym>, <acronym title="X Bitmap">XBM</acronym>, and <acronym title="Wireless Bitmap">WBMP</acronym> became available. </td> </tr> <tr> <td>4.3.2</td> <td> JPEG 2000 support was added for the <code class="parameter">imageinfo</code> parameter. </td> </tr> <tr> <td>4.3.0</td> <td> <em>bits</em> and <em>channels</em> are present for other image types, too. </td> </tr> <tr> <td>4.3.0</td> <td> Support for <acronym>SWC</acronym> and <acronym title="Interchange File Format">IFF</acronym> was added. </td> </tr> <tr> <td>4.2.0</td> <td> Support for <acronym title="Tagged Image File Format">TIFF</acronym> was added. </td> </tr> <tr> <td>4.0.6</td> <td> Support for <acronym title="Bitmap">BMP</acronym> and <acronym title="Photoshop Document">PSD</acronym> was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.getimagesize-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3731"> <p><strong>Example #2 <span class="function"><strong>getimagesize()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width</span><span style="color: #007700">, </span><span style="color: #0000BB">$height</span><span style="color: #007700">, </span><span style="color: #0000BB">$type</span><span style="color: #007700">, </span><span style="color: #0000BB">$attr</span><span style="color: #007700">) = </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"img/flag.jpg"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"<img src=\"img/flag.jpg\" </span><span style="color: #0000BB">$attr</span><span style="color: #DD0000"> alt=\"getimagesize() example\" />"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3732"> <p><strong>Example #3 getimagesize (URL)</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/gifs/logo.gif"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// if the file name has space in it, encode it properly<br /></span><span style="color: #0000BB">$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/gifs/lo%20go.gif"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3733"> <p><strong>Example #4 getimagesize() returning IPTC</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"testimg.jpg"</span><span style="color: #007700">, </span><span style="color: #0000BB">$info</span><span style="color: #007700">);<br />if (isset(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">"APP13"</span><span style="color: #007700">])) {<br /> </span><span style="color: #0000BB">$iptc </span><span style="color: #007700">= </span><span style="color: #0000BB">iptcparse</span><span style="color: #007700">(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">"APP13"</span><span style="color: #007700">]);<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$iptc</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.getimagesize-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">This function does not require the GD image library.</p></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.getimagesize-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.image-type-to-mime-type.html" class="function" rel="rdfs-seeAlso">image_type_to_mime_type()</a> - Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype</span></li> <li class="member"><span class="function"><a href="function.exif-imagetype.html" class="function" rel="rdfs-seeAlso">exif_imagetype()</a> - Determine the type of an image</span></li> <li class="member"><span class="function"><a href="function.exif-read-data.html" class="function" rel="rdfs-seeAlso">exif_read_data()</a> - Reads the EXIF headers from an image file</span></li> <li class="member"><span class="function"><a href="function.exif-thumbnail.html" class="function" rel="rdfs-seeAlso">exif_thumbnail()</a> - Retrieve the embedded thumbnail of an image</span></li> <li class="member"><span class="function"><a href="function.imagesx.html" class="function" rel="rdfs-seeAlso">imagesx()</a> - Get image width</span></li> <li class="member"><span class="function"><a href="function.imagesy.html" class="function" rel="rdfs-seeAlso">imagesy()</a> - Get image height</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.gd-info.html">gd_info</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getimagesizefromstring.html">getimagesizefromstring</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>