PHP may be used to process, generate, or edit image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, XPM, etc...

GD Library

PHP can actually output images directly into a browser. To enable that functionality the GD library has to be compiled into a PHP server.

NOTE: GD and PHP may require other libraries, depending on which image formats you want to work with.

The exif extension

To work with the exif extension, a developer may use data stored in headers of JPEG and TIFF images (for instance). This way an user can read meta-data generated by digital cameras. The exif functions do not require the GD library.

Image size (getimagesize(), imagecopyresized()) 

An image size can be checked with the function getimagesize().

Syntax for getimagesize()

$size = getimagesize("image_file.png");

An image can be also resized with the function imagecopyresized().

Syntax for imagecopyresized()

imagecopyresized (resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h);

The arguments above include: image location, image destination, current height and width, and resized height and width. The example below shows how it may be done.

Example of image resizing

<?php
    $image_orig = "sample_image.png";
    $resize = 0.4; // Resize factor
    header("Content-Type: image/jpeg");
    list($width, $height) = getimagesize($image_orig);
    
    // new size
    $newwidth = $width * $resize;
    $newheight = $height * $resize;
    
    // create
    $image_new = imagecreatetruecolor($newwidth, $newheight);
    $image_src = imagecreatefromjpeg($image_orig);
    imagecopyresized($image_new, $image_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
?>

Image format (exif_imagetype(), exif_read_data()) 

Image format can be checked by using image functions such as exif_imagetype() and exif_read_data() along with a little bit of logic, as shown in the example below.

Syntax for exif_imagetype()

if (exif_imagetype("image.png") == IMAGETYPE_PNG 
{
    // code...
}

In the example above IMAGETYPE_PNG may replaced with other extensions such as GIF, JPEG, SWF, etc...

To check the meta-data information of an image exif_read_data() function is used to read the EXIF header from JPEG or TIFF.

Syntax for exif_read_data()

exif_read_data (string $filename [, string $sections = NULL [, bool $arrays = false [, bool $thumbnail = false ]]]);

By using this approach a developer can read the meta-data of a digital camera's photo.

NOTE: Although this is valid for most of the cameras which support these formats, have in mind that every manufacturer has different settings for their products.

Example of using exif_read_data() function

<?php
    $image_meta = exif_read_data("path/image.jpg", "IFD0");
    echo $image_meta===false ? "No meta found." : "Image contains meta.";
?>

 

›› go to examples ››