DJ Mike's Tutorials: PHP

Find Image Attributes

Sometimes you need to know something about an image before you can do something with it. For example, before using one image to create another, you need to know it's type so you know if you should use imagecreatefromgif(), imagecreatefromjpeg() or imagecreatefrompng(). Other times you may need to know the image's size. To get the size and type of an image, you can use getimagesize()

getimagesize() will determine the size of the following file types: .gif, .jpg, .jpeg, .jpe, .png .php (when it is an image), .swf, swc, .psd, .tiff, .bmp .iff. The results will be returned in an array containing the following members:

Index[0] Width in pixels. (0 for non-image types)
Index[1] Height in pixels. (0 for non-image types)
Index[2] Flag indicating file type
  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
Index[3] Contains a text string with the size attributes of an image that can be used in an tag
Index[bits] 3 for RGB pictures and 4 for CMYK. Non-animated images only
Index[channels] JPG only. Number of bits per color
Index[mime] Mime type. Used to deliver correct HTTP Content-type headers

If getimagesize() can't access an file or if the file is not a valid type, getimagesize() returns false and an error message is generated.

Using getimagesize()

As mentioned above, getimagesize() will return false if it can't access a file or the file is not an image. You can use this to decide which code you want to execute. In the example below, I used it to execute die() if an invalid URL is entered. Since getimagesize() also generates a PHP warning if a invalid URL is entered, I used

error_reporting(0);

to suppress PHP error messages. Note that I add that after the script is debugged.

Test Image Show example Show Code

Test Image Show example Show Code

If $url is set, I use getimagesize() to check if the URL entered is valid and if not, use die() to make my own error message:



<?php
if ( @getimagesize($url) == FALSE )
{
die(
"
<html>
<body bgcolor=\"#fad888\">
<center>
<h1>Invalid file type or Nonexistant File</h1>
<a target=\"sizer\" href=\"image_sizer.php\">Reset</a>
</center>

</body>

</html>

"
);}
?>


There a several ways that you can use the information that you get from getimagesize(). Two different ways in this demonstration



list($width, $height, $type, $attr) = getimagesize("$url");


list() will assign numerically indexed members of an array to variables. Since bits, channels and mime are not numerically indexed, you can't assign them with list() but more often than not, I only want the size and type so I like using list(). If you want bits, channel and mime, you can assign the contents of getimagesize() to an array:



$image_info = getimagesize("$url");

The contents of $image_info are now avaliable as $image_info[0], $image_info[1], $image_info[2] ect.

Getting Cooridinates And Colors

PHP can get the color of a pixel for you but it will be expressed in an RGB triplet. I already made a function to convert RGB triplets to hex for the page on user defined functions so I use include() so I can use the same function here.

Test Image Show example Show Code

Test Image Show example Show Code

To check if a url has been submited, I use isset($url) and to test if $url is a valid image, I use getimagesize().

if ( isset($url) && getimagesize("$url") == false )

When a form has

<input type="image"  src="URL" />

and you click the image, the x,y cooridinates are submited to the form. Until the user submits an image, I don't have a source for the image input so I use

is ( isset() )

to show the image input and textarea for the output only after an image has been entered.

I don't need to do anything special to get the cooridinates. They are submitted as $x and $y. To get the colors though, I need to make an image resource but before that I need to know the image type. For that, I use getimagesize() again. Once I have the image type, I can use the appropriate imagecreate function to make an image resource. If the image is a .gif, I can use imagecolorstotal() to find the number of colors. imagecolorstotal() accepts an image resource and cooridinates.

To get the colors, use imagecolorat(). imagecolorat() accepts three arguments; an image resource and coordinates, and returns an associative array containing the members $red, $green, $blue and if it exists, $alpha. To convert those to a hex value. I use my user defined function rgb2hex() that I included at the top of the file.


< ^



Was this webpage useful to you? You can support this website by donating.


Created by DJ Mike from Santa Barbara

DJ Mike


<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>