How to Retrieve the Camera Information (Meta Data) from JPEG Images using PHP?


Pictures/Images taken by Modern cameras (SLR or Smart Phones) have stored some additional data (meta information) in the JPEG such as camera model, make, exposure time, ISO etc. We can use the following PHP Function to retrieve such information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<php?
  function cameraUsed($imagePath) {
      $ext = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION));
      // Check if the variable is set and if the file itself exists before continuing
      if ((isset($imagePath)) and (file_exists($imagePath)) and (($ext=='jpg') || ($ext=='jpeg'))) {
      
        // There are 2 arrays which contains the information we are after, so it's easier to state them both
        $exif_ifd0 = @read_exif_data($imagePath ,'IFD0' ,0);       
        $exif_exif = @read_exif_data($imagePath ,'EXIF' ,0);
        
        //error control
        $notFound = "";
        
        // Make 
        if (@array_key_exists('Make', $exif_ifd0)) {
          $camMake = $exif_ifd0['Make'];
        } else { $camMake = $notFound; }
        
        // Model
        if (@array_key_exists('Model', $exif_ifd0)) {
          $camModel = $exif_ifd0['Model'];
        } else { $camModel = $notFound; }
        
        // Exposure
        if (@array_key_exists('ExposureTime', $exif_ifd0)) {
          $camExposure = $exif_ifd0['ExposureTime'];
        } else { $camExposure = $notFound; }
  
        // Aperture
        if (@array_key_exists('ApertureFNumber', $exif_ifd0['COMPUTED'])) {
          $camAperture = $exif_ifd0['COMPUTED']['ApertureFNumber'];
        } else { $camAperture = $notFound; }
        
        // Date
        if (@array_key_exists('DateTime', $exif_ifd0)) {
          $camDate = $exif_ifd0['DateTime'];
        } else { $camDate = $notFound; }
        
        // ISO
        if (@array_key_exists('ISOSpeedRatings',$exif_exif)) {
          $camIso = $exif_exif['ISOSpeedRatings'];
        } else { $camIso = $notFound; }
        
        $return = array();
        $return['make'] = $camMake;
        $return['model'] = $camModel;
        $return['exposure'] = $camExposure;
        $return['aperture'] = $camAperture;
        $return['date'] = $camDate;
        $return['iso'] = $camIso;
        return $return;
      } 
      return false;
  }
<php?
  function cameraUsed($imagePath) {
      $ext = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION));
      // Check if the variable is set and if the file itself exists before continuing
      if ((isset($imagePath)) and (file_exists($imagePath)) and (($ext=='jpg') || ($ext=='jpeg'))) {
      
        // There are 2 arrays which contains the information we are after, so it's easier to state them both
        $exif_ifd0 = @read_exif_data($imagePath ,'IFD0' ,0);       
        $exif_exif = @read_exif_data($imagePath ,'EXIF' ,0);
        
        //error control
        $notFound = "";
        
        // Make 
        if (@array_key_exists('Make', $exif_ifd0)) {
          $camMake = $exif_ifd0['Make'];
        } else { $camMake = $notFound; }
        
        // Model
        if (@array_key_exists('Model', $exif_ifd0)) {
          $camModel = $exif_ifd0['Model'];
        } else { $camModel = $notFound; }
        
        // Exposure
        if (@array_key_exists('ExposureTime', $exif_ifd0)) {
          $camExposure = $exif_ifd0['ExposureTime'];
        } else { $camExposure = $notFound; }
  
        // Aperture
        if (@array_key_exists('ApertureFNumber', $exif_ifd0['COMPUTED'])) {
          $camAperture = $exif_ifd0['COMPUTED']['ApertureFNumber'];
        } else { $camAperture = $notFound; }
        
        // Date
        if (@array_key_exists('DateTime', $exif_ifd0)) {
          $camDate = $exif_ifd0['DateTime'];
        } else { $camDate = $notFound; }
        
        // ISO
        if (@array_key_exists('ISOSpeedRatings',$exif_exif)) {
          $camIso = $exif_exif['ISOSpeedRatings'];
        } else { $camIso = $notFound; }
        
        $return = array();
        $return['make'] = $camMake;
        $return['model'] = $camModel;
        $return['exposure'] = $camExposure;
        $return['aperture'] = $camAperture;
        $return['date'] = $camDate;
        $return['iso'] = $camIso;
        return $return;
      } 
      return false;
  }

The PHP provides a handy function read_exif_data to read the exif/meta data from JPEG images, and the above would then process these data. If no such exif_data is found, the function will return false.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
335 words
Last Post: Ways to Prevent an Account Takeover Fraud
Next Post: Teaching Kids Programming - N-ary Tree Preorder Traversal Algorithms using Iterations or Recursion

The Permanent URL is: How to Retrieve the Camera Information (Meta Data) from JPEG Images using PHP?

Leave a Reply