How to work with images

A function that gets information about an image Name Description

Name Description
getimagesize($path) Returns an array that contains seven elements that provide information about the image. Index 0 gets the image width, index 1 gets the image height, and index 2 gets one of the IMAGETYPE_XXX constants that specifies the type of the image.

Common IMAGETYPE constants

      IMAGETYPE_JPEG 
      IMAGETYPE_GIF 
      IMAGETYPE_PNG

Code that gets information about an image

      // Set the path to the image 
      $image_path = getcwd() . DIRECTORY_SEPARATOR . 'gibson_sg.png'; 
       
      // Get the image width, height, and type 
      $image_info = getimagesize($image_path); 
      $image_width = $image_info[0]; 
      $image_height = $image_info[1]; 
      $image_type = $image_info[2]; 
       
      // Display the image type 
      switch($image_type) {
        case IMAGETYPE_JPEG:
          echo("This is a JPEG image.<br>");
          break;
        case IMAGETYPE_GIF:
          echo("This is a GIF image.<br>");
          break;
        case IMAGETYPE_PNG:
          echo("This is a GIF image.<br>");
          break;
        default: 
          echo("File must be a JPEG, GIF, or PNG image.<br>");
          exit;
      }

Description

Back