Exemple #1
0
 private function bwg_wp_read_image_metadata($file)
 {
     if (!file_exists($file)) {
         return false;
     }
     list(, , $sourceImageType) = getimagesize($file);
     $meta = array('aperture' => 0, 'credit' => '', 'camera' => '', 'caption' => '', 'created_timestamp' => 0, 'copyright' => '', 'focal_length' => 0, 'iso' => 0, 'shutter_speed' => 0, 'title' => '', 'orientation' => 0);
     if (is_callable('iptcparse')) {
         getimagesize($file, $info);
         if (!empty($info['APP13'])) {
             $iptc = iptcparse($info['APP13']);
             if (!empty($iptc['2#105'][0])) {
                 $meta['title'] = trim($iptc['2#105'][0]);
             } elseif (!empty($iptc['2#005'][0])) {
                 $meta['title'] = trim($iptc['2#005'][0]);
             }
             if (!empty($iptc['2#120'][0])) {
                 $caption = trim($iptc['2#120'][0]);
                 if (empty($meta['title'])) {
                     mbstring_binary_safe_encoding();
                     $caption_length = strlen($caption);
                     reset_mbstring_encoding();
                     if ($caption_length < 80) {
                         $meta['title'] = $caption;
                     } else {
                         $meta['caption'] = $caption;
                     }
                 } elseif ($caption != $meta['title']) {
                     $meta['caption'] = $caption;
                 }
             }
             if (!empty($iptc['2#110'][0])) {
                 $meta['credit'] = trim($iptc['2#110'][0]);
             } elseif (!empty($iptc['2#080'][0])) {
                 $meta['credit'] = trim($iptc['2#080'][0]);
             }
             if (!empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0])) {
                 $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
             }
             if (!empty($iptc['2#116'][0])) {
                 $meta['copyright'] = trim($iptc['2#116'][0]);
             }
         }
     }
     if (is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)))) {
         $exif = @exif_read_data($file);
         if (empty($meta['title']) && !empty($exif['Title'])) {
             $meta['title'] = trim($exif['Title']);
         }
         if (!empty($exif['ImageDescription'])) {
             mbstring_binary_safe_encoding();
             $description_length = strlen($exif['ImageDescription']);
             reset_mbstring_encoding();
             if (empty($meta['title']) && $description_length < 80) {
                 $meta['title'] = trim($exif['ImageDescription']);
                 if (empty($meta['caption']) && !empty($exif['COMPUTED']['UserComment']) && trim($exif['COMPUTED']['UserComment']) != $meta['title']) {
                     $meta['caption'] = trim($exif['COMPUTED']['UserComment']);
                 }
             } elseif (empty($meta['caption']) && trim($exif['ImageDescription']) != $meta['title']) {
                 $meta['caption'] = trim($exif['ImageDescription']);
             }
         } elseif (empty($meta['caption']) && !empty($exif['Comments']) && trim($exif['Comments']) != $meta['title']) {
             $meta['caption'] = trim($exif['Comments']);
         }
         if (empty($meta['credit'])) {
             if (!empty($exif['Artist'])) {
                 $meta['credit'] = trim($exif['Artist']);
             } elseif (!empty($exif['Author'])) {
                 $meta['credit'] = trim($exif['Author']);
             }
         }
         if (empty($meta['copyright']) && !empty($exif['Copyright'])) {
             $meta['copyright'] = trim($exif['Copyright']);
         }
         if (!empty($exif['FNumber'])) {
             $meta['aperture'] = round(wp_exif_frac2dec($exif['FNumber']), 2);
         }
         if (!empty($exif['Model'])) {
             $meta['camera'] = trim($exif['Model']);
         }
         if (empty($meta['created_timestamp']) && !empty($exif['DateTimeDigitized'])) {
             $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
         }
         if (!empty($exif['FocalLength'])) {
             $meta['focal_length'] = (string) wp_exif_frac2dec($exif['FocalLength']);
         }
         if (!empty($exif['ISOSpeedRatings'])) {
             $meta['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
             $meta['iso'] = trim($meta['iso']);
         }
         if (!empty($exif['ExposureTime'])) {
             $meta['shutter_speed'] = (string) wp_exif_frac2dec($exif['ExposureTime']);
         }
         if (!empty($exif['Orientation'])) {
             $meta['orientation'] = $exif['Orientation'];
         }
     }
     foreach (array('title', 'caption', 'credit', 'copyright', 'camera', 'iso') as $key) {
         if ($meta[$key] && !seems_utf8($meta[$key])) {
             $meta[$key] = utf8_encode($meta[$key]);
         }
     }
     foreach ($meta as &$value) {
         if (is_string($value)) {
             $value = wp_kses_post($value);
         }
     }
     return $meta;
 }
/**
 * Get extended image metadata, exif or iptc as available.
 *
 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
 * created_timestamp, focal_length, shutter_speed, and title.
 *
 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
 * and time, caption, copyright, and title. Also includes FNumber, Model,
 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
 *
 * @todo Try other exif libraries if available.
 * @since 2.5.0
 *
 * @param string $file
 * @return bool|array False on failure. Image metadata array on success.
 */
function wp_read_image_metadata($file)
{
    if (!file_exists($file)) {
        return false;
    }
    list(, , $sourceImageType) = getimagesize($file);
    // exif contains a bunch of data we'll probably never need formatted in ways
    // that are difficult to use. We'll normalize it and just extract the fields
    // that are likely to be useful. Fractions and numbers are converted to
    // floats, dates to unix timestamps, and everything else to strings.
    $meta = array('aperture' => 0, 'credit' => '', 'camera' => '', 'caption' => '', 'created_timestamp' => 0, 'copyright' => '', 'focal_length' => 0, 'iso' => 0, 'shutter_speed' => 0, 'title' => '');
    // read iptc first, since it might contain data not available in exif such
    // as caption, description etc
    if (is_callable('iptcparse')) {
        getimagesize($file, $info);
        if (!empty($info['APP13'])) {
            $iptc = iptcparse($info['APP13']);
            // headline, "A brief synopsis of the caption."
            if (!empty($iptc['2#105'][0])) {
                $meta['title'] = trim($iptc['2#105'][0]);
            } elseif (!empty($iptc['2#005'][0])) {
                $meta['title'] = trim($iptc['2#005'][0]);
            }
            if (!empty($iptc['2#120'][0])) {
                // description / legacy caption
                $caption = trim($iptc['2#120'][0]);
                if (empty($meta['title'])) {
                    // Assume the title is stored in 2:120 if it's short.
                    if (strlen($caption) < 80) {
                        $meta['title'] = $caption;
                    } else {
                        $meta['caption'] = $caption;
                    }
                } elseif ($caption != $meta['title']) {
                    $meta['caption'] = $caption;
                }
            }
            if (!empty($iptc['2#110'][0])) {
                // credit
                $meta['credit'] = trim($iptc['2#110'][0]);
            } elseif (!empty($iptc['2#080'][0])) {
                // creator / legacy byline
                $meta['credit'] = trim($iptc['2#080'][0]);
            }
            if (!empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0])) {
                // created date and time
                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
            }
            if (!empty($iptc['2#116'][0])) {
                // copyright
                $meta['copyright'] = trim($iptc['2#116'][0]);
            }
        }
    }
    // fetch additional info from exif if available
    if (is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)))) {
        $exif = @exif_read_data($file);
        if (!empty($exif['Title'])) {
            $meta['title'] = trim($exif['Title']);
        }
        if (!empty($exif['ImageDescription'])) {
            if (empty($meta['title']) && strlen($exif['ImageDescription']) < 80) {
                // Assume the title is stored in ImageDescription
                $meta['title'] = trim($exif['ImageDescription']);
                if (!empty($exif['COMPUTED']['UserComment']) && trim($exif['COMPUTED']['UserComment']) != $meta['title']) {
                    $meta['caption'] = trim($exif['COMPUTED']['UserComment']);
                }
            } elseif (trim($exif['ImageDescription']) != $meta['title']) {
                $meta['caption'] = trim($exif['ImageDescription']);
            }
        } elseif (!empty($exif['Comments']) && trim($exif['Comments']) != $meta['title']) {
            $meta['caption'] = trim($exif['Comments']);
        }
        if (!empty($exif['Artist'])) {
            $meta['credit'] = trim($exif['Artist']);
        } elseif (!empty($exif['Author'])) {
            $meta['credit'] = trim($exif['Author']);
        }
        if (!empty($exif['Copyright'])) {
            $meta['copyright'] = trim($exif['Copyright']);
        }
        if (!empty($exif['FNumber'])) {
            $meta['aperture'] = round(wp_exif_frac2dec($exif['FNumber']), 2);
        }
        if (!empty($exif['Model'])) {
            $meta['camera'] = trim($exif['Model']);
        }
        if (!empty($exif['DateTimeDigitized'])) {
            $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
        }
        if (!empty($exif['FocalLength'])) {
            $meta['focal_length'] = (string) wp_exif_frac2dec($exif['FocalLength']);
        }
        if (!empty($exif['ISOSpeedRatings'])) {
            $meta['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
            $meta['iso'] = trim($meta['iso']);
        }
        if (!empty($exif['ExposureTime'])) {
            $meta['shutter_speed'] = (string) wp_exif_frac2dec($exif['ExposureTime']);
        }
    }
    foreach (array('title', 'caption', 'credit', 'copyright', 'camera', 'iso') as $key) {
        if ($meta[$key] && !seems_utf8($meta[$key])) {
            $meta[$key] = utf8_encode($meta[$key]);
        }
    }
    return apply_filters('wp_read_image_metadata', $meta, $file, $sourceImageType);
}
Exemple #3
0
/**
 * Get extended image metadata, exif or iptc as available.
 *
 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
 * created_timestamp, focal_length, shutter_speed, and title.
 *
 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
 * and time, caption, copyright, and title. Also includes FNumber, Model,
 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
 *
 * @todo Try other exif libraries if available.
 * @since 2.5.0
 *
 * @param string $file
 * @return bool|array False on failure. Image metadata array on success.
 */
function wp_read_image_metadata($file)
{
    if (!file_exists($file)) {
        return false;
    }
    list(, , $sourceImageType) = getimagesize($file);
    /*
     * EXIF contains a bunch of data we'll probably never need formatted in ways
     * that are difficult to use. We'll normalize it and just extract the fields
     * that are likely to be useful. Fractions and numbers are converted to
     * floats, dates to unix timestamps, and everything else to strings.
     */
    $meta = array('aperture' => 0, 'credit' => '', 'camera' => '', 'caption' => '', 'created_timestamp' => 0, 'copyright' => '', 'focal_length' => 0, 'iso' => 0, 'shutter_speed' => 0, 'title' => '', 'orientation' => 0, 'keywords' => array());
    $iptc = array();
    /*
     * Read IPTC first, since it might contain data not available in exif such
     * as caption, description etc.
     */
    if (is_callable('iptcparse')) {
        getimagesize($file, $info);
        if (!empty($info['APP13'])) {
            $iptc = iptcparse($info['APP13']);
            // Headline, "A brief synopsis of the caption."
            if (!empty($iptc['2#105'][0])) {
                $meta['title'] = trim($iptc['2#105'][0]);
                /*
                 * Title, "Many use the Title field to store the filename of the image,
                 * though the field may be used in many ways."
                 */
            } elseif (!empty($iptc['2#005'][0])) {
                $meta['title'] = trim($iptc['2#005'][0]);
            }
            if (!empty($iptc['2#120'][0])) {
                // description / legacy caption
                $caption = trim($iptc['2#120'][0]);
                mbstring_binary_safe_encoding();
                $caption_length = strlen($caption);
                reset_mbstring_encoding();
                if (empty($meta['title']) && $caption_length < 80) {
                    // Assume the title is stored in 2:120 if it's short.
                    $meta['title'] = $caption;
                }
                $meta['caption'] = $caption;
            }
            if (!empty($iptc['2#110'][0])) {
                // credit
                $meta['credit'] = trim($iptc['2#110'][0]);
            } elseif (!empty($iptc['2#080'][0])) {
                // creator / legacy byline
                $meta['credit'] = trim($iptc['2#080'][0]);
            }
            if (!empty($iptc['2#055'][0]) && !empty($iptc['2#060'][0])) {
                // created date and time
                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
            }
            if (!empty($iptc['2#116'][0])) {
                // copyright
                $meta['copyright'] = trim($iptc['2#116'][0]);
            }
            if (!empty($iptc['2#025'][0])) {
                // keywords array
                $meta['keywords'] = array_values($iptc['2#025']);
            }
        }
    }
    /**
     * Filter the image types to check for exif data.
     *
     * @since 2.5.0
     *
     * @param array $image_types Image types to check for exif data.
     */
    if (is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)))) {
        $exif = @exif_read_data($file);
        if (!empty($exif['ImageDescription'])) {
            mbstring_binary_safe_encoding();
            $description_length = strlen($exif['ImageDescription']);
            reset_mbstring_encoding();
            if (empty($meta['title']) && $description_length < 80) {
                // Assume the title is stored in ImageDescription
                $meta['title'] = trim($exif['ImageDescription']);
            }
            if (empty($meta['caption']) && !empty($exif['COMPUTED']['UserComment'])) {
                $meta['caption'] = trim($exif['COMPUTED']['UserComment']);
            }
            if (empty($meta['caption'])) {
                $meta['caption'] = trim($exif['ImageDescription']);
            }
        } elseif (empty($meta['caption']) && !empty($exif['Comments'])) {
            $meta['caption'] = trim($exif['Comments']);
        }
        if (empty($meta['credit'])) {
            if (!empty($exif['Artist'])) {
                $meta['credit'] = trim($exif['Artist']);
            } elseif (!empty($exif['Author'])) {
                $meta['credit'] = trim($exif['Author']);
            }
        }
        if (empty($meta['copyright']) && !empty($exif['Copyright'])) {
            $meta['copyright'] = trim($exif['Copyright']);
        }
        if (!empty($exif['FNumber'])) {
            $meta['aperture'] = round(wp_exif_frac2dec($exif['FNumber']), 2);
        }
        if (!empty($exif['Model'])) {
            $meta['camera'] = trim($exif['Model']);
        }
        if (empty($meta['created_timestamp']) && !empty($exif['DateTimeDigitized'])) {
            $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
        }
        if (!empty($exif['FocalLength'])) {
            $meta['focal_length'] = (string) wp_exif_frac2dec($exif['FocalLength']);
        }
        if (!empty($exif['ISOSpeedRatings'])) {
            $meta['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
            $meta['iso'] = trim($meta['iso']);
        }
        if (!empty($exif['ExposureTime'])) {
            $meta['shutter_speed'] = (string) wp_exif_frac2dec($exif['ExposureTime']);
        }
        if (!empty($exif['Orientation'])) {
            $meta['orientation'] = $exif['Orientation'];
        }
    }
    foreach (array('title', 'caption', 'credit', 'copyright', 'camera', 'iso') as $key) {
        if ($meta[$key] && !seems_utf8($meta[$key])) {
            $meta[$key] = utf8_encode($meta[$key]);
        }
    }
    foreach ($meta['keywords'] as $key => $keyword) {
        if (!seems_utf8($keyword)) {
            $meta['keywords'][$key] = utf8_encode($keyword);
        }
    }
    $meta = wp_kses_post_deep($meta);
    /**
     * Filter the array of meta data read from an image's exif data.
     *
     * @since 2.5.0
     * @since 4.4.0 The `$iptc` parameter was added.
     *
     * @param array  $meta            Image meta data.
     * @param string $file            Path to image file.
     * @param int    $sourceImageType Type of image.
     * @param array  $iptc            IPTC data.
     */
    return apply_filters('wp_read_image_metadata', $meta, $file, $sourceImageType, $iptc);
}
Exemple #4
0
/**
 * Get extended image metadata, exif or iptc as available.
 *
 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
 * created_timestamp, focal_length, shutter_speed, and title.
 *
 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
 * and time, caption, copyright, and title. Also includes FNumber, Model,
 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
 *
 * @todo Try other exif libraries if available.
 * @since 2.5.0
 *
 * @param string $file
 * @return bool|array False on failure. Image metadata array on success.
 */
function wp_read_image_metadata($file)
{
    if (!file_exists($file)) {
        return false;
    }
    list(, , $sourceImageType) = getimagesize($file);
    // exif contains a bunch of data we'll probably never need formatted in ways
    // that are difficult to use. We'll normalize it and just extract the fields
    // that are likely to be useful.  Fractions and numbers are converted to
    // floats, dates to unix timestamps, and everything else to strings.
    $meta = array('aperture' => 0, 'credit' => '', 'camera' => '', 'caption' => '', 'created_timestamp' => 0, 'copyright' => '', 'focal_length' => 0, 'iso' => 0, 'shutter_speed' => 0, 'title' => '');
    // read iptc first, since it might contain data not available in exif such
    // as caption, description etc
    if (is_callable('iptcparse')) {
        getimagesize($file, $info);
        if (!empty($info['APP13'])) {
            $iptc = iptcparse($info['APP13']);
            if (!empty($iptc['2#110'][0])) {
                // credit
                $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
            } elseif (!empty($iptc['2#080'][0])) {
                // byline
                $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
            }
            if (!empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0])) {
                // created date and time
                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
            }
            if (!empty($iptc['2#120'][0])) {
                // caption
                $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
            }
            if (!empty($iptc['2#116'][0])) {
                // copyright
                $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
            }
            if (!empty($iptc['2#005'][0])) {
                // title
                $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));
            }
        }
    }
    // fetch additional info from exif if available
    if (is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)))) {
        $exif = @exif_read_data($file);
        if (!empty($exif['FNumber'])) {
            $meta['aperture'] = round(wp_exif_frac2dec($exif['FNumber']), 2);
        }
        if (!empty($exif['Model'])) {
            $meta['camera'] = trim($exif['Model']);
        }
        if (!empty($exif['DateTimeDigitized'])) {
            $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
        }
        if (!empty($exif['FocalLength'])) {
            $meta['focal_length'] = wp_exif_frac2dec($exif['FocalLength']);
        }
        if (!empty($exif['ISOSpeedRatings'])) {
            $meta['iso'] = $exif['ISOSpeedRatings'];
        }
        if (!empty($exif['ExposureTime'])) {
            $meta['shutter_speed'] = wp_exif_frac2dec($exif['ExposureTime']);
        }
    }
    return apply_filters('wp_read_image_metadata', $meta, $file, $sourceImageType);
}
Exemple #5
0
 function extract_metadata($remote_meta)
 {
     $meta = array('aperture' => 0, 'credit' => '', 'camera' => '', 'caption' => '', 'created_timestamp' => 0, 'copyright' => '', 'focal_length' => 0, 'iso' => 0, 'shutter_speed' => 0, 'title' => '');
     $meta['title'] = $this->extract_meta_value($remote_meta, array('Headline', 'ObjectName'));
     $caption = $this->extract_meta_value($remote_meta, array('Caption-Abstract'));
     if (!empty($caption)) {
         if (empty($meta['title'])) {
             if (strlen($caption) < 80) {
                 $meta['title'] = $caption;
             } else {
                 $meta['caption'] = $caption;
             }
         } elseif ($caption != $meta['title']) {
             $meta['caption'] = $caption;
         }
     }
     $meta['credit'] = $this->extract_meta_value($remote_meta, array('Artist', 'Author', 'Credit', 'By-line'));
     if (!empty($remote_meta["DateCreated"]) and !empty($remote_meta["TimeCreated"])) {
         // created date and time
         $meta['created_timestamp'] = strtotime($remote_meta["DateCreated"] . ' ' . $remote_meta["TimeCreated"]);
     }
     $meta['copyright'] = $this->extract_meta_value($remote_meta, array('Copyright', 'CopyrightNotice'));
     if (!empty($remote_meta['Title'])) {
         $meta['title'] = trim($remote_meta['Title']);
     }
     if (!empty($remote_meta['ImageDescription'])) {
         if (empty($meta['title']) && strlen($remote_meta['ImageDescription']) < 80) {
             // Assume the title is stored in ImageDescription
             $meta['title'] = trim($remote_meta['ImageDescription']);
             if (!empty($remote_meta['UserComment']) && trim($remote_meta['UserComment']) != $meta['title']) {
                 $meta['caption'] = trim($remote_meta['UserComment']);
             }
         } elseif (trim($remote_meta['ImageDescription']) != $meta['title']) {
             $meta['caption'] = trim($remote_meta['ImageDescription']);
         }
     } elseif (!empty($remote_meta['Comments']) && trim($remote_meta['Comments']) != $meta['title']) {
         $meta['caption'] = trim($remote_meta['Comments']);
     }
     $meta['camera'] = $this->extract_meta_value($remote_meta, array('Model'));
     if (!empty($remote_meta['DateTimeDigitized'])) {
         $meta['created_timestamp'] = wp_exif_date2ts($remote_meta['DateTimeDigitized']);
     }
     $meta['iso'] = $this->extract_meta_value($remote_meta, array('ISO'), 0);
     if (!empty($remote_meta['FNumber'])) {
         $meta['aperture'] = round(wp_exif_frac2dec($remote_meta['FNumber']), 2);
     }
     if (!empty($remote_meta['FocalLength'])) {
         $meta['focal_length'] = (string) wp_exif_frac2dec($remote_meta['FocalLength']);
     }
     if (!empty($remote_meta['ExposureTime'])) {
         $meta['shutter_speed'] = (string) wp_exif_frac2dec($remote_meta['ExposureTime']);
     }
     return $meta;
     foreach (array('title', 'caption', 'credit', 'copyright', 'camera', 'iso') as $key) {
         if ($meta[$key] && !seems_utf8($meta[$key])) {
             $meta[$key] = utf8_encode($meta[$key]);
         }
     }
 }
Exemple #6
0
 /**
  * Get extended image metadata, exif or iptc as available.
  * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
  * created_timestamp, focal_length, shutter_speed, and title.
  * The IPTC metadata that is retrieved is APP13, credit, byline, created date
  * and time, caption, copyright, and title. Also includes FNumber, Model,
  * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
  * @todo  Try other exif libraries if available.
  * @since 2.5.0
  *
  * @param string $file
  *
  * @return bool|array False on failure. Image metadata array on success.
  */
 function wp_read_image_metadata($file)
 {
     if (!is_file($file)) {
         return false;
     }
     list(, , $sourceImageType) = getimagesize($file);
     $meta = array();
     /*
      * Read IPTC first, since it might contain data not available in exif such
      * as caption, description etc.
      */
     if (is_callable('iptcparse')) {
         getimagesize($file, $info);
         if (!empty($info['APP13'])) {
             $iptc = iptcparse($info['APP13']);
             // Headline, "A brief synopsis of the caption."
             if (!empty($iptc['2#105'][0])) {
                 $meta['title'] = trim($iptc['2#105'][0]);
                 /*
                  * Title, "Many use the Title field to store the filename of the image,
                  * though the field may be used in many ways."
                  */
             } elseif (!empty($iptc['2#005'][0])) {
                 $meta['title'] = trim($iptc['2#005'][0]);
             }
             if (!empty($iptc['2#120'][0])) {
                 // description / legacy caption
                 $caption = trim($iptc['2#120'][0]);
                 if (empty($meta['title'])) {
                     mbstring_binary_safe_encoding();
                     $caption_length = strlen($caption);
                     reset_mbstring_encoding();
                     // Assume the title is stored in 2:120 if it's short.
                     if ($caption_length < 80) {
                         $meta['title'] = $caption;
                     } else {
                         $meta['caption'] = $caption;
                     }
                 } elseif ($caption != $meta['title']) {
                     $meta['caption'] = $caption;
                 }
             }
             if (!empty($iptc['2#110'][0])) {
                 $meta['credit'] = trim($iptc['2#110'][0]);
             } elseif (!empty($iptc['2#080'][0])) {
                 $meta['credit'] = trim($iptc['2#080'][0]);
             }
             if (!empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0])) {
                 $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
             }
             if (!empty($iptc['2#116'][0])) {
                 $meta['copyright'] = trim($iptc['2#116'][0]);
             }
             if (!empty($iptc['2#025'])) {
                 $meta['keywords'] = $iptc['2#025'];
             }
         }
     }
     /**
      * Filter the image types to check for exif data.
      * @since 2.5.0
      *
      * @param array $image_types Image types to check for exif data.
      */
     if (is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)))) {
         $exif = @exif_read_data($file);
         unset($exif['MakerNote']);
         // Title
         if (empty($meta['title']) && !empty($exif['Title'])) {
             $meta['title'] = trim($exif['Title']);
         }
         // Descrioption
         if (!empty($exif['ImageDescription'])) {
             mbstring_binary_safe_encoding();
             $description_length = strlen($exif['ImageDescription']);
             reset_mbstring_encoding();
             if (empty($meta['title']) && $description_length < 80) {
                 // Assume the title is stored in ImageDescription
                 $meta['title'] = trim($exif['ImageDescription']);
                 if (empty($meta['caption']) && !empty($exif['COMPUTED']['UserComment']) && trim($exif['COMPUTED']['UserComment']) != $meta['title']) {
                     $meta['caption'] = trim($exif['COMPUTED']['UserComment']);
                 }
             } elseif (empty($meta['caption']) && trim($exif['ImageDescription']) != $meta['title']) {
                 $meta['caption'] = trim($exif['ImageDescription']);
             }
         } elseif (empty($meta['caption']) && !empty($exif['Comments']) && trim($exif['Comments']) != $meta['title']) {
             $meta['caption'] = trim($exif['Comments']);
         }
         // Credit
         if (empty($meta['credit'])) {
             if (!empty($exif['Artist'])) {
                 $meta['credit'] = trim($exif['Artist']);
             } elseif (!empty($exif['Author'])) {
                 $meta['credit'] = trim($exif['Author']);
             }
         }
         // Copyright
         if (empty($meta['copyright']) && !empty($exif['Copyright'])) {
             $meta['copyright'] = trim($exif['Copyright']);
         }
         // Camera Make
         if (!empty($exif['Make'])) {
             $meta['make'] = $exif['Make'];
         }
         // Camera Model
         if (!empty($exif['Model'])) {
             $meta['model'] = trim($exif['Model']);
         }
         // Exposure Time (shutter speed)
         if (!empty($exif['ExposureTime'])) {
             $meta['exposure'] = $exif['ExposureTime'] . 's';
             $meta['shutter_speed'] = (string) wp_exif_frac2dec($exif['ExposureTime']) . 's';
         }
         // Aperture
         if (!empty($exif['COMPUTED']['ApertureFNumber'])) {
             $meta['aperture'] = $exif['COMPUTED']['ApertureFNumber'];
         } elseif (!empty($exif['FNumber'])) {
             $meta['aperture'] = 'f/' . (string) round(wp_exif_frac2dec($exif['FNumber']), 2);
         }
         // ISO
         if (!empty($exif['ISOSpeedRatings'])) {
             $meta['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
             $meta['iso'] = trim($meta['iso']);
         }
         // Date
         if (!empty($exif['DateTime'])) {
             $meta['date'] = $exif['DateTime'];
         }
         // Created TimeStamp
         if (empty($meta['created_timestamp']) && !empty($exif['DateTimeDigitized'])) {
             $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
         }
         // Lens
         if (!empty($exif['UndefinedTag:0xA434'])) {
             $meta['lens'] = $exif['UndefinedTag:0xA434'];
         }
         // Focus Distance
         if (!empty($exif['COMPUTED']['FocusDistance'])) {
             $meta['distance'] = $exif['COMPUTED']['FocusDistance'];
         }
         // Focal Length
         if (!empty($exif['FocalLength'])) {
             $meta['focallength'] = (string) round(wp_exif_frac2dec($exif['FocalLength'])) . 'mm';
         }
         // Focal Length 35mm
         if (!empty($exif['FocalLengthIn35mmFilm'])) {
             $meta['focallength35'] = $exif['FocalLengthIn35mmFilm'] . 'mm';
         }
         // Flash data
         if (!empty($exif['Flash'])) {
             // we need to interpret the result - it's given as a number and we want a human-readable description.
             $fdata = $exif['Flash'];
             switch ($fdata) {
                 case 0:
                     $fdata = 'No Flash';
                     break;
                 case 1:
                     $fdata = 'Flash';
                     break;
                 case 5:
                     $fdata = 'Flash, strobe return light not detected';
                     break;
                 case 7:
                     $fdata = 'Flash, strob return light detected';
                     break;
                 case 9:
                     $fdata = 'Compulsory Flash';
                     break;
                 case 13:
                     $fdata = 'Compulsory Flash, Return light not detected';
                     break;
                 case 15:
                     $fdata = 'Compulsory Flash, Return light detected';
                     break;
                 case 16:
                     $fdata = 'No Flash';
                     break;
                 case 24:
                     $fdata = 'No Flash';
                     break;
                 case 25:
                     $fdata = 'Flash, Auto-Mode';
                     break;
                 case 29:
                     $fdata = 'Flash, Auto-Mode, Return light not detected';
                     break;
                 case 31:
                     $fdata = 'Flash, Auto-Mode, Return light detected';
                     break;
                 case 32:
                     $fdata = 'No Flash';
                     break;
                 case 65:
                     $fdata = 'Red Eye';
                     break;
                 case 69:
                     $fdata = 'Red Eye, Return light not detected';
                     break;
                 case 71:
                     $fdata = 'Red Eye, Return light detected';
                     break;
                 case 73:
                     $fdata = 'Red Eye, Compulsory Flash';
                     break;
                 case 77:
                     $fdata = 'Red Eye, Compulsory Flash, Return light not detected';
                     break;
                 case 79:
                     $fdata = 'Red Eye, Compulsory Flash, Return light detected';
                     break;
                 case 89:
                     $fdata = 'Red Eye, Auto-Mode';
                     break;
                 case 93:
                     $fdata = 'Red Eye, Auto-Mode, Return light not detected';
                     break;
                 case 95:
                     $fdata = 'Red Eye, Auto-Mode, Return light detected';
                     break;
                 default:
                     $fdata = 'Unknown: ' . $fdata;
                     break;
             }
             $meta['flashdata'] = $fdata;
         }
         // Lens Make
         if (!empty($exif['UndefinedTag:0xA433'])) {
             $meta['lensmake'] = $exif['UndefinedTag:0xA433'];
         }
         // Software
         if (!empty($exif['Software'])) {
             $meta['software'] = $exif['Software'];
         }
         // Orientation
         if (!empty($exif['Orientation'])) {
             $meta['orientation'] = $exif['Orientation'];
         }
         $exif_sections = @exif_read_data($file, null, true);
         if (isset($exif_sections['GPS'])) {
             $meta['GPS'] = $this->getGPSfromExif($exif_sections['GPS']);
         }
         unset($exif_sections);
         //$meta['exif'] = $exif;
     }
     foreach (array('title', 'caption', 'credit', 'copyright', 'model', 'iso', 'software') as $key) {
         if (!empty($meta[$key]) && !seems_utf8($meta[$key])) {
             $meta[$key] = utf8_encode($meta[$key]);
         }
     }
     if (!empty($meta['keywords'])) {
         foreach ($meta['keywords'] as $i => $key) {
             if (!seems_utf8($key)) {
                 $meta['keywords'][$i] = utf8_encode($key);
             }
         }
     }
     foreach ($meta as &$value) {
         if (is_string($value)) {
             $value = wp_kses_post($value);
         }
     }
     /**
      * Filter the array of meta data read from an image's exif data.
      * @since 2.5.0
      *
      * @param array  $meta            Image meta data.
      * @param string $file            Path to image file.
      * @param int    $sourceImageType Type of image.
      */
     return apply_filters('wp_read_image_metadata', $meta, $file, $sourceImageType);
 }
Exemple #7
0
 /**
  * Update EXIF fields.
  *
  * @param array $post The post data
  * @param array $attachment Attachment fields from the $_POST form
  * @return array $post Modified post data
  */
 public function saveAttachmentFields($post, $attachment)
 {
     // Check if this is actually an image.
     // bail if not
     if (!wp_attachment_is_image($post['ID'])) {
         return $post;
     }
     //First read the current attachment metadata
     $meta_data = wp_get_attachment_metadata($post['ID']);
     foreach ($this->fields as $fieldName => $value) {
         //check existence just to be sure
         //WordPress will create empty entries even if the EXIF entry in not present in the image EXIF metadata
         if (isset($attachment[$this->prefix . $fieldName])) {
             //depending on each field do what WordPress does to them - a sort of normalization
             switch ($fieldName) {
                 case 'aperture':
                     $meta_data['image_meta'][$fieldName] = round(wp_exif_frac2dec($attachment[$this->prefix . $fieldName]), 2);
                     break;
                 case 'focal_length':
                 case 'shutter_speed':
                     $meta_data['image_meta'][$fieldName] = (string) wp_exif_frac2dec($attachment[$this->prefix . $fieldName]);
                     break;
                 default:
                     $meta_data['image_meta'][$fieldName] = trim($attachment[$this->prefix . $fieldName]);
             }
         }
     }
     //save the modified EXIF data into the database
     wp_update_attachment_metadata($post['ID'], $meta_data);
     return $post;
 }