exif.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Exif Processing Library
  4. *
  5. * @package TidypicsExif
  6. */
  7. /**
  8. * Pull EXIF data from image file
  9. *
  10. * @param TidypicsImage $image
  11. */
  12. function td_get_exif($image) {
  13. // catch for those who don't have exif module loaded
  14. if (!is_callable('exif_read_data')) {
  15. return;
  16. }
  17. $mime = $image->mimetype;
  18. if ($mime != 'image/jpeg' && $mime != 'image/pjpeg') {
  19. return;
  20. }
  21. $filename = $image->getFilenameOnFilestore();
  22. $exif = exif_read_data($filename, 'IFD0,EXIF', true);
  23. if (is_array($exif)) {
  24. $data = array_merge($exif['IFD0'], $exif['EXIF']);
  25. foreach ($data as $key => $value) {
  26. if (is_string($value)) {
  27. // there are sometimes unicode characters that cause problems with serialize
  28. $data[$key] = preg_replace( '/[^[:print:]]/', '', $value);
  29. }
  30. }
  31. $image->tp_exif = serialize($data);
  32. }
  33. }
  34. /**
  35. * Grab array of EXIF data for display
  36. *
  37. * @param TidypicsImage $image
  38. * @return array|false
  39. */
  40. function tp_exif_formatted($image) {
  41. $exif = $image->tp_exif;
  42. if (!$exif) {
  43. return false;
  44. }
  45. $exif = unserialize($exif);
  46. $model = $exif['Model'];
  47. if (!$model) {
  48. $model = "N/A";
  49. }
  50. $exif_data['Model'] = $model;
  51. $exposure = $exif['ExposureTime'];
  52. if (!$exposure) {
  53. $exposure = "N/A";
  54. }
  55. $exif_data['Shutter'] = $exposure;
  56. //got the code snippet below from http://www.zenphoto.org/support/topic.php?id=17
  57. //convert the raw values to understandible values
  58. $Fnumber = explode("/", $exif['FNumber']);
  59. if ($Fnumber[1] != 0) {
  60. $Fnumber = $Fnumber[0] / $Fnumber[1];
  61. } else {
  62. $Fnumber = 0;
  63. }
  64. if (!$Fnumber) {
  65. $Fnumber = "N/A";
  66. } else {
  67. $Fnumber = "f/$Fnumber";
  68. }
  69. $exif_data['Aperture'] = $Fnumber;
  70. $iso = $exif['ISOSpeedRatings'];
  71. if (!$iso) {
  72. $iso = "N/A";
  73. }
  74. $exif_data['ISO Speed'] = $iso;
  75. $Focal = explode("/", $exif['FocalLength']);
  76. if ($Focal[1] != 0) {
  77. $Focal = $Focal[0] / $Focal[1];
  78. } else {
  79. $Focal = 0;
  80. }
  81. if (!$Focal || round($Focal) == "0") {
  82. $Focal = 0;
  83. }
  84. if (round($Focal) == 0) {
  85. $Focal = "N/A";
  86. } else {
  87. $Focal = round($Focal) . "mm";
  88. }
  89. $exif_data['Focal Length'] = $Focal;
  90. $captured = $exif['DateTime'];
  91. if (!$captured) {
  92. $captured = "N/A";
  93. }
  94. $exif_data['Captured'] = $captured;
  95. return $exif_data;
  96. }