Cześć, chciałbym utworzyć miniaturkę pliku jpg z metadanymi (Exif).
Mam takie kod(znaleziony w sieci):
  1. <?php
  2.  
  3. /*********************************************/
  4. /* Fonction: ImageCreateFromBMP */
  5. /* Author: DHKold */
  6. /* Contact: admin@dhkold.com */
  7. /* Date: The 15th of June 2005 */
  8. /* Version: 2.0B */
  9. /*********************************************/
  10.  
  11. function ImageCreateFromBMP($filename)
  12. {
  13. //Ouverture du fichier en mode binaire
  14. if (! $f1 = fopen($filename,"rb")) return FALSE;
  15.  
  16. //1 : Chargement des ent?tes FICHIER
  17. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  18. if ($FILE['file_type'] != 19778) return FALSE;
  19.  
  20. //2 : Chargement des ent?tes BMP
  21. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  22. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  23. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  24. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  25. if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  26. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  27. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  28. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  29. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  30. $BMP['decal'] = 4-(4*$BMP['decal']);
  31. if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  32.  
  33. //3 : Chargement des couleurs de la palette
  34. $PALETTE = array();
  35. if ($BMP['colors'] < 16777216)
  36. {
  37. $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
  38. }
  39.  
  40. //4 : Cr?ation de l'image
  41. $IMG = fread($f1,$BMP['size_bitmap']);
  42. $VIDE = chr(0);
  43.  
  44. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  45. $P = 0;
  46. $Y = $BMP['height']-1;
  47. while ($Y >= 0)
  48. {
  49. $X=0;
  50. while ($X < $BMP['width'])
  51. {
  52. if ($BMP['bits_per_pixel'] == 24)
  53. $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
  54. elseif ($BMP['bits_per_pixel'] == 16)
  55. {
  56. $COLOR = unpack("n",substr($IMG,$P,2));
  57. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  58. }
  59. elseif ($BMP['bits_per_pixel'] == 8)
  60. {
  61. $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
  62. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  63. }
  64. elseif ($BMP['bits_per_pixel'] == 4)
  65. {
  66. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  67. if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
  68. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  69. }
  70. elseif ($BMP['bits_per_pixel'] == 1)
  71. {
  72. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  73. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  74. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  75. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  76. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  77. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  78. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  79. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  80. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  81. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  82. }
  83. else
  84. return FALSE;
  85. imagesetpixel($res,$X,$Y,$COLOR[1]);
  86. $X++;
  87. $P += $BMP['bytes_per_pixel'];
  88. }
  89. $Y--;
  90. $P+=$BMP['decal'];
  91. }
  92.  
  93. //Fermeture du fichier
  94. fclose($f1);
  95.  
  96. return $res;
  97. }
  98. //********************************************************************************
    ********************
  99. function create_thumbnail( $source_file, $destination_file, $max_dimension)
  100. {
  101.  
  102. list($img_width,$img_height) = getimagesize($source_file); // Get the original dimentions
  103. $aspect_ratio = $img_width / $img_height;
  104.  
  105. if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) ) // If either dimension is too big...
  106. {
  107. if ( $img_width > $img_height ) // For wide images...
  108. {
  109. $new_width = $max_dimension;
  110. $new_height = $new_width / $aspect_ratio;
  111. }
  112. elseif ( $img_width < $img_height ) // For tall images...
  113. {
  114. $new_height = $max_dimension;
  115. $new_width = $new_height * $aspect_ratio;
  116. }
  117. elseif ( $img_width == $img_height ) // For square images...
  118. {
  119. $new_width = $max_dimension;
  120. $new_height = $max_dimension;
  121. }
  122. else { echo "Error reading image size."; return FALSE; }
  123. }
  124. else { $new_width = $img_width; $new_height = $img_height; } // If it's already smaller, don't change the size.
  125.  
  126. // Make sure these are integers.
  127. $new_width = intval($new_width);
  128. $new_height = intval($new_height);
  129.  
  130. $thumbnail = imagecreatetruecolor($new_width,$new_height); // Creates a new image in memory.
  131.  
  132. // The following block retrieves the source file. It assumes the filename extensions match the file's format.
  133. if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
  134. if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) )
  135. { $img_source = imagecreatefromjpeg($source_file); }
  136. if ( strpos($source_file,".bmp") ) { $img_source = ImageCreateFromBMP($source_file); }
  137. if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
  138.  
  139. // Here we resample and create the new jpeg.
  140. imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
  141. imagejpeg( $thumbnail, $destination_file, 100 );
  142.  
  143. // Finally, we destroy the two images in memory.
  144. imagedestroy($img_source);
  145. imagedestroy($thumbnail);
  146. }
  147. ?>


Wszystko fajnie działa, gdy chcę utworzyć miniaturkę z normalnego zdjęcia jpg.
Problem występuje wtedy, gdy ma utworzyć miniaturkę z zdjęcia jpg z metadanymi (Exif).
Tworzy się wtedy czarna miniaturka.
Czy da się temu jakoś zaradzić?
Zdjęcie jpg z metadanymi (Exif): http://s.fothost.pl/upload/10/23/c15396e9.jpg