JpegMeta.php 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263
  1. <?php
  2. /**
  3. * JPEG metadata reader/writer
  4. *
  5. * @license BSD <http://www.opensource.org/licenses/bsd-license.php>
  6. * @link http://github.com/sd/jpeg-php
  7. * @author Sebastian Delmont <sdelmont@zonageek.com>
  8. * @author Andreas Gohr <andi@splitbrain.org>
  9. * @author Hakan Sandell <hakan.sandell@mydata.se>
  10. * @todo Add support for Maker Notes, Extend for GIF and PNG metadata
  11. */
  12. // Original copyright notice:
  13. //
  14. // Copyright (c) 2003 Sebastian Delmont <sdelmont@zonageek.com>
  15. // All rights reserved.
  16. //
  17. // Redistribution and use in source and binary forms, with or without
  18. // modification, are permitted provided that the following conditions
  19. // are met:
  20. // 1. Redistributions of source code must retain the above copyright
  21. // notice, this list of conditions and the following disclaimer.
  22. // 2. Redistributions in binary form must reproduce the above copyright
  23. // notice, this list of conditions and the following disclaimer in the
  24. // documentation and/or other materials provided with the distribution.
  25. // 3. Neither the name of the author nor the names of its contributors
  26. // may be used to endorse or promote products derived from this software
  27. // without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30. // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  31. // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  32. // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  35. // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  40. class JpegMeta {
  41. var $_fileName;
  42. var $_fp = null;
  43. var $_fpout = null;
  44. var $_type = 'unknown';
  45. var $_markers;
  46. var $_info;
  47. /**
  48. * Constructor
  49. *
  50. * @author Sebastian Delmont <sdelmont@zonageek.com>
  51. *
  52. * @param $fileName
  53. */
  54. function __construct($fileName) {
  55. $this->_fileName = $fileName;
  56. $this->_fp = null;
  57. $this->_type = 'unknown';
  58. unset($this->_info);
  59. unset($this->_markers);
  60. }
  61. /**
  62. * Returns all gathered info as multidim array
  63. *
  64. * @author Sebastian Delmont <sdelmont@zonageek.com>
  65. */
  66. function & getRawInfo() {
  67. $this->_parseAll();
  68. if ($this->_markers == null) {
  69. return false;
  70. }
  71. return $this->_info;
  72. }
  73. /**
  74. * Returns basic image info
  75. *
  76. * @author Sebastian Delmont <sdelmont@zonageek.com>
  77. */
  78. function & getBasicInfo() {
  79. $this->_parseAll();
  80. $info = array();
  81. if ($this->_markers == null) {
  82. return false;
  83. }
  84. $info['Name'] = $this->_info['file']['Name'];
  85. if (isset($this->_info['file']['Url'])) {
  86. $info['Url'] = $this->_info['file']['Url'];
  87. $info['NiceSize'] = "???KB";
  88. } else {
  89. $info['Size'] = $this->_info['file']['Size'];
  90. $info['NiceSize'] = $this->_info['file']['NiceSize'];
  91. }
  92. if (@isset($this->_info['sof']['Format'])) {
  93. $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
  94. } else {
  95. $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
  96. }
  97. if (@isset($this->_info['sof']['ColorChannels'])) {
  98. $info['ColorMode'] = ($this->_info['sof']['ColorChannels'] > 1) ? "Color" : "B&W";
  99. }
  100. $info['Width'] = $this->getWidth();
  101. $info['Height'] = $this->getHeight();
  102. $info['DimStr'] = $this->getDimStr();
  103. $dates = $this->getDates();
  104. $info['DateTime'] = $dates['EarliestTime'];
  105. $info['DateTimeStr'] = $dates['EarliestTimeStr'];
  106. $info['HasThumbnail'] = $this->hasThumbnail();
  107. return $info;
  108. }
  109. /**
  110. * Convinience function to access nearly all available Data
  111. * through one function
  112. *
  113. * @author Andreas Gohr <andi@splitbrain.org>
  114. *
  115. * @param array|string $fields field name or array with field names
  116. * @return bool|string
  117. */
  118. function getField($fields) {
  119. if(!is_array($fields)) $fields = array($fields);
  120. $info = false;
  121. foreach($fields as $field){
  122. if(strtolower(substr($field,0,5)) == 'iptc.'){
  123. $info = $this->getIPTCField(substr($field,5));
  124. }elseif(strtolower(substr($field,0,5)) == 'exif.'){
  125. $info = $this->getExifField(substr($field,5));
  126. }elseif(strtolower(substr($field,0,4)) == 'xmp.'){
  127. $info = $this->getXmpField(substr($field,4));
  128. }elseif(strtolower(substr($field,0,5)) == 'file.'){
  129. $info = $this->getFileField(substr($field,5));
  130. }elseif(strtolower(substr($field,0,5)) == 'date.'){
  131. $info = $this->getDateField(substr($field,5));
  132. }elseif(strtolower($field) == 'simple.camera'){
  133. $info = $this->getCamera();
  134. }elseif(strtolower($field) == 'simple.raw'){
  135. return $this->getRawInfo();
  136. }elseif(strtolower($field) == 'simple.title'){
  137. $info = $this->getTitle();
  138. }elseif(strtolower($field) == 'simple.shutterspeed'){
  139. $info = $this->getShutterSpeed();
  140. }else{
  141. $info = $this->getExifField($field);
  142. }
  143. if($info != false) break;
  144. }
  145. if($info === false) $info = '';
  146. if(is_array($info)){
  147. if(isset($info['val'])){
  148. $info = $info['val'];
  149. }else{
  150. $arr = array();
  151. foreach($info as $part){
  152. if(is_array($part)){
  153. if(isset($part['val'])){
  154. $arr[] = $part['val'];
  155. }else{
  156. $arr[] = join(', ',$part);
  157. }
  158. }else{
  159. $arr[] = $part;
  160. }
  161. }
  162. $info = join(', ',$arr);
  163. }
  164. }
  165. return trim($info);
  166. }
  167. /**
  168. * Convinience function to set nearly all available Data
  169. * through one function
  170. *
  171. * @author Andreas Gohr <andi@splitbrain.org>
  172. *
  173. * @param string $field field name
  174. * @param string $value
  175. * @return bool success or fail
  176. */
  177. function setField($field, $value) {
  178. if(strtolower(substr($field,0,5)) == 'iptc.'){
  179. return $this->setIPTCField(substr($field,5),$value);
  180. }elseif(strtolower(substr($field,0,5)) == 'exif.'){
  181. return $this->setExifField(substr($field,5),$value);
  182. }else{
  183. return $this->setExifField($field,$value);
  184. }
  185. }
  186. /**
  187. * Convinience function to delete nearly all available Data
  188. * through one function
  189. *
  190. * @author Andreas Gohr <andi@splitbrain.org>
  191. *
  192. * @param string $field field name
  193. * @return bool
  194. */
  195. function deleteField($field) {
  196. if(strtolower(substr($field,0,5)) == 'iptc.'){
  197. return $this->deleteIPTCField(substr($field,5));
  198. }elseif(strtolower(substr($field,0,5)) == 'exif.'){
  199. return $this->deleteExifField(substr($field,5));
  200. }else{
  201. return $this->deleteExifField($field);
  202. }
  203. }
  204. /**
  205. * Return a date field
  206. *
  207. * @author Andreas Gohr <andi@splitbrain.org>
  208. *
  209. * @param string $field
  210. * @return false|string
  211. */
  212. function getDateField($field) {
  213. if (!isset($this->_info['dates'])) {
  214. $this->_info['dates'] = $this->getDates();
  215. }
  216. if (isset($this->_info['dates'][$field])) {
  217. return $this->_info['dates'][$field];
  218. }
  219. return false;
  220. }
  221. /**
  222. * Return a file info field
  223. *
  224. * @author Andreas Gohr <andi@splitbrain.org>
  225. *
  226. * @param string $field field name
  227. * @return false|string
  228. */
  229. function getFileField($field) {
  230. if (!isset($this->_info['file'])) {
  231. $this->_parseFileInfo();
  232. }
  233. if (isset($this->_info['file'][$field])) {
  234. return $this->_info['file'][$field];
  235. }
  236. return false;
  237. }
  238. /**
  239. * Return the camera info (Maker and Model)
  240. *
  241. * @author Andreas Gohr <andi@splitbrain.org>
  242. * @todo handle makernotes
  243. *
  244. * @return false|string
  245. */
  246. function getCamera(){
  247. $make = $this->getField(array('Exif.Make','Exif.TIFFMake'));
  248. $model = $this->getField(array('Exif.Model','Exif.TIFFModel'));
  249. $cam = trim("$make $model");
  250. if(empty($cam)) return false;
  251. return $cam;
  252. }
  253. /**
  254. * Return shutter speed as a ratio
  255. *
  256. * @author Joe Lapp <joe.lapp@pobox.com>
  257. *
  258. * @return string
  259. */
  260. function getShutterSpeed() {
  261. if (!isset($this->_info['exif'])) {
  262. $this->_parseMarkerExif();
  263. }
  264. if(!isset($this->_info['exif']['ExposureTime'])){
  265. return '';
  266. }
  267. $field = $this->_info['exif']['ExposureTime'];
  268. if($field['den'] == 1) return $field['num'];
  269. return $field['num'].'/'.$field['den'];
  270. }
  271. /**
  272. * Return an EXIF field
  273. *
  274. * @author Sebastian Delmont <sdelmont@zonageek.com>
  275. *
  276. * @param string $field field name
  277. * @return false|string
  278. */
  279. function getExifField($field) {
  280. if (!isset($this->_info['exif'])) {
  281. $this->_parseMarkerExif();
  282. }
  283. if ($this->_markers == null) {
  284. return false;
  285. }
  286. if (isset($this->_info['exif'][$field])) {
  287. return $this->_info['exif'][$field];
  288. }
  289. return false;
  290. }
  291. /**
  292. * Return an XMP field
  293. *
  294. * @author Hakan Sandell <hakan.sandell@mydata.se>
  295. *
  296. * @param string $field field name
  297. * @return false|string
  298. */
  299. function getXmpField($field) {
  300. if (!isset($this->_info['xmp'])) {
  301. $this->_parseMarkerXmp();
  302. }
  303. if ($this->_markers == null) {
  304. return false;
  305. }
  306. if (isset($this->_info['xmp'][$field])) {
  307. return $this->_info['xmp'][$field];
  308. }
  309. return false;
  310. }
  311. /**
  312. * Return an Adobe Field
  313. *
  314. * @author Sebastian Delmont <sdelmont@zonageek.com>
  315. *
  316. * @param string $field field name
  317. * @return false|string
  318. */
  319. function getAdobeField($field) {
  320. if (!isset($this->_info['adobe'])) {
  321. $this->_parseMarkerAdobe();
  322. }
  323. if ($this->_markers == null) {
  324. return false;
  325. }
  326. if (isset($this->_info['adobe'][$field])) {
  327. return $this->_info['adobe'][$field];
  328. }
  329. return false;
  330. }
  331. /**
  332. * Return an IPTC field
  333. *
  334. * @author Sebastian Delmont <sdelmont@zonageek.com>
  335. *
  336. * @param string $field field name
  337. * @return false|string
  338. */
  339. function getIPTCField($field) {
  340. if (!isset($this->_info['iptc'])) {
  341. $this->_parseMarkerAdobe();
  342. }
  343. if ($this->_markers == null) {
  344. return false;
  345. }
  346. if (isset($this->_info['iptc'][$field])) {
  347. return $this->_info['iptc'][$field];
  348. }
  349. return false;
  350. }
  351. /**
  352. * Set an EXIF field
  353. *
  354. * @author Sebastian Delmont <sdelmont@zonageek.com>
  355. * @author Joe Lapp <joe.lapp@pobox.com>
  356. *
  357. * @param string $field field name
  358. * @param string $value
  359. * @return bool
  360. */
  361. function setExifField($field, $value) {
  362. if (!isset($this->_info['exif'])) {
  363. $this->_parseMarkerExif();
  364. }
  365. if ($this->_markers == null) {
  366. return false;
  367. }
  368. if ($this->_info['exif'] == false) {
  369. $this->_info['exif'] = array();
  370. }
  371. // make sure datetimes are in correct format
  372. if(strlen($field) >= 8 && strtolower(substr($field, 0, 8)) == 'datetime') {
  373. if(strlen($value) < 8 || $value[4] != ':' || $value[7] != ':') {
  374. $value = date('Y:m:d H:i:s', strtotime($value));
  375. }
  376. }
  377. $this->_info['exif'][$field] = $value;
  378. return true;
  379. }
  380. /**
  381. * Set an Adobe Field
  382. *
  383. * @author Sebastian Delmont <sdelmont@zonageek.com>
  384. *
  385. * @param string $field field name
  386. * @param string $value
  387. * @return bool
  388. */
  389. function setAdobeField($field, $value) {
  390. if (!isset($this->_info['adobe'])) {
  391. $this->_parseMarkerAdobe();
  392. }
  393. if ($this->_markers == null) {
  394. return false;
  395. }
  396. if ($this->_info['adobe'] == false) {
  397. $this->_info['adobe'] = array();
  398. }
  399. $this->_info['adobe'][$field] = $value;
  400. return true;
  401. }
  402. /**
  403. * Calculates the multiplier needed to resize the image to the given
  404. * dimensions
  405. *
  406. * @author Andreas Gohr <andi@splitbrain.org>
  407. *
  408. * @param int $maxwidth
  409. * @param int $maxheight
  410. * @return float|int
  411. */
  412. function getResizeRatio($maxwidth,$maxheight=0){
  413. if(!$maxheight) $maxheight = $maxwidth;
  414. $w = $this->getField('File.Width');
  415. $h = $this->getField('File.Height');
  416. $ratio = 1;
  417. if($w >= $h){
  418. if($w >= $maxwidth){
  419. $ratio = $maxwidth/$w;
  420. }elseif($h > $maxheight){
  421. $ratio = $maxheight/$h;
  422. }
  423. }else{
  424. if($h >= $maxheight){
  425. $ratio = $maxheight/$h;
  426. }elseif($w > $maxwidth){
  427. $ratio = $maxwidth/$w;
  428. }
  429. }
  430. return $ratio;
  431. }
  432. /**
  433. * Set an IPTC field
  434. *
  435. * @author Sebastian Delmont <sdelmont@zonageek.com>
  436. *
  437. * @param string $field field name
  438. * @param string $value
  439. * @return bool
  440. */
  441. function setIPTCField($field, $value) {
  442. if (!isset($this->_info['iptc'])) {
  443. $this->_parseMarkerAdobe();
  444. }
  445. if ($this->_markers == null) {
  446. return false;
  447. }
  448. if ($this->_info['iptc'] == false) {
  449. $this->_info['iptc'] = array();
  450. }
  451. $this->_info['iptc'][$field] = $value;
  452. return true;
  453. }
  454. /**
  455. * Delete an EXIF field
  456. *
  457. * @author Sebastian Delmont <sdelmont@zonageek.com>
  458. *
  459. * @param string $field field name
  460. * @return bool
  461. */
  462. function deleteExifField($field) {
  463. if (!isset($this->_info['exif'])) {
  464. $this->_parseMarkerAdobe();
  465. }
  466. if ($this->_markers == null) {
  467. return false;
  468. }
  469. if ($this->_info['exif'] != false) {
  470. unset($this->_info['exif'][$field]);
  471. }
  472. return true;
  473. }
  474. /**
  475. * Delete an Adobe field
  476. *
  477. * @author Sebastian Delmont <sdelmont@zonageek.com>
  478. *
  479. * @param string $field field name
  480. * @return bool
  481. */
  482. function deleteAdobeField($field) {
  483. if (!isset($this->_info['adobe'])) {
  484. $this->_parseMarkerAdobe();
  485. }
  486. if ($this->_markers == null) {
  487. return false;
  488. }
  489. if ($this->_info['adobe'] != false) {
  490. unset($this->_info['adobe'][$field]);
  491. }
  492. return true;
  493. }
  494. /**
  495. * Delete an IPTC field
  496. *
  497. * @author Sebastian Delmont <sdelmont@zonageek.com>
  498. *
  499. * @param string $field field name
  500. * @return bool
  501. */
  502. function deleteIPTCField($field) {
  503. if (!isset($this->_info['iptc'])) {
  504. $this->_parseMarkerAdobe();
  505. }
  506. if ($this->_markers == null) {
  507. return false;
  508. }
  509. if ($this->_info['iptc'] != false) {
  510. unset($this->_info['iptc'][$field]);
  511. }
  512. return true;
  513. }
  514. /**
  515. * Get the image's title, tries various fields
  516. *
  517. * @param int $max maximum number chars (keeps words)
  518. * @return false|string
  519. *
  520. * @author Andreas Gohr <andi@splitbrain.org>
  521. */
  522. function getTitle($max=80){
  523. // try various fields
  524. $cap = $this->getField(array('Iptc.Headline',
  525. 'Iptc.Caption',
  526. 'Xmp.dc:title',
  527. 'Exif.UserComment',
  528. 'Exif.TIFFUserComment',
  529. 'Exif.TIFFImageDescription',
  530. 'File.Name'));
  531. if (empty($cap)) return false;
  532. if(!$max) return $cap;
  533. // Shorten to 80 chars (keeping words)
  534. $new = preg_replace('/\n.+$/','',wordwrap($cap, $max));
  535. if($new != $cap) $new .= '...';
  536. return $new;
  537. }
  538. /**
  539. * Gather various date fields
  540. *
  541. * @author Sebastian Delmont <sdelmont@zonageek.com>
  542. *
  543. * @return array|bool
  544. */
  545. function getDates() {
  546. $this->_parseAll();
  547. if ($this->_markers == null) {
  548. if (@isset($this->_info['file']['UnixTime'])) {
  549. $dates = array();
  550. $dates['FileModified'] = $this->_info['file']['UnixTime'];
  551. $dates['Time'] = $this->_info['file']['UnixTime'];
  552. $dates['TimeSource'] = 'FileModified';
  553. $dates['TimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
  554. $dates['EarliestTime'] = $this->_info['file']['UnixTime'];
  555. $dates['EarliestTimeSource'] = 'FileModified';
  556. $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
  557. $dates['LatestTime'] = $this->_info['file']['UnixTime'];
  558. $dates['LatestTimeSource'] = 'FileModified';
  559. $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
  560. return $dates;
  561. }
  562. return false;
  563. }
  564. $dates = array();
  565. $latestTime = 0;
  566. $latestTimeSource = "";
  567. $earliestTime = time();
  568. $earliestTimeSource = "";
  569. if (@isset($this->_info['exif']['DateTime'])) {
  570. $dates['ExifDateTime'] = $this->_info['exif']['DateTime'];
  571. $aux = $this->_info['exif']['DateTime'];
  572. $aux[4] = "-";
  573. $aux[7] = "-";
  574. $t = strtotime($aux);
  575. if ($t && $t > $latestTime) {
  576. $latestTime = $t;
  577. $latestTimeSource = "ExifDateTime";
  578. }
  579. if ($t && $t < $earliestTime) {
  580. $earliestTime = $t;
  581. $earliestTimeSource = "ExifDateTime";
  582. }
  583. }
  584. if (@isset($this->_info['exif']['DateTimeOriginal'])) {
  585. $dates['ExifDateTimeOriginal'] = $this->_info['exif']['DateTimeOriginal'];
  586. $aux = $this->_info['exif']['DateTimeOriginal'];
  587. $aux[4] = "-";
  588. $aux[7] = "-";
  589. $t = strtotime($aux);
  590. if ($t && $t > $latestTime) {
  591. $latestTime = $t;
  592. $latestTimeSource = "ExifDateTimeOriginal";
  593. }
  594. if ($t && $t < $earliestTime) {
  595. $earliestTime = $t;
  596. $earliestTimeSource = "ExifDateTimeOriginal";
  597. }
  598. }
  599. if (@isset($this->_info['exif']['DateTimeDigitized'])) {
  600. $dates['ExifDateTimeDigitized'] = $this->_info['exif']['DateTimeDigitized'];
  601. $aux = $this->_info['exif']['DateTimeDigitized'];
  602. $aux[4] = "-";
  603. $aux[7] = "-";
  604. $t = strtotime($aux);
  605. if ($t && $t > $latestTime) {
  606. $latestTime = $t;
  607. $latestTimeSource = "ExifDateTimeDigitized";
  608. }
  609. if ($t && $t < $earliestTime) {
  610. $earliestTime = $t;
  611. $earliestTimeSource = "ExifDateTimeDigitized";
  612. }
  613. }
  614. if (@isset($this->_info['iptc']['DateCreated'])) {
  615. $dates['IPTCDateCreated'] = $this->_info['iptc']['DateCreated'];
  616. $aux = $this->_info['iptc']['DateCreated'];
  617. $aux = substr($aux, 0, 4) . "-" . substr($aux, 4, 2) . "-" . substr($aux, 6, 2);
  618. $t = strtotime($aux);
  619. if ($t && $t > $latestTime) {
  620. $latestTime = $t;
  621. $latestTimeSource = "IPTCDateCreated";
  622. }
  623. if ($t && $t < $earliestTime) {
  624. $earliestTime = $t;
  625. $earliestTimeSource = "IPTCDateCreated";
  626. }
  627. }
  628. if (@isset($this->_info['file']['UnixTime'])) {
  629. $dates['FileModified'] = $this->_info['file']['UnixTime'];
  630. $t = $this->_info['file']['UnixTime'];
  631. if ($t && $t > $latestTime) {
  632. $latestTime = $t;
  633. $latestTimeSource = "FileModified";
  634. }
  635. if ($t && $t < $earliestTime) {
  636. $earliestTime = $t;
  637. $earliestTimeSource = "FileModified";
  638. }
  639. }
  640. $dates['Time'] = $earliestTime;
  641. $dates['TimeSource'] = $earliestTimeSource;
  642. $dates['TimeStr'] = date("Y-m-d H:i:s", $earliestTime);
  643. $dates['EarliestTime'] = $earliestTime;
  644. $dates['EarliestTimeSource'] = $earliestTimeSource;
  645. $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $earliestTime);
  646. $dates['LatestTime'] = $latestTime;
  647. $dates['LatestTimeSource'] = $latestTimeSource;
  648. $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $latestTime);
  649. return $dates;
  650. }
  651. /**
  652. * Get the image width, tries various fields
  653. *
  654. * @author Sebastian Delmont <sdelmont@zonageek.com>
  655. *
  656. * @return false|string
  657. */
  658. function getWidth() {
  659. if (!isset($this->_info['sof'])) {
  660. $this->_parseMarkerSOF();
  661. }
  662. if ($this->_markers == null) {
  663. return false;
  664. }
  665. if (isset($this->_info['sof']['ImageWidth'])) {
  666. return $this->_info['sof']['ImageWidth'];
  667. }
  668. if (!isset($this->_info['exif'])) {
  669. $this->_parseMarkerExif();
  670. }
  671. if (isset($this->_info['exif']['PixelXDimension'])) {
  672. return $this->_info['exif']['PixelXDimension'];
  673. }
  674. return false;
  675. }
  676. /**
  677. * Get the image height, tries various fields
  678. *
  679. * @author Sebastian Delmont <sdelmont@zonageek.com>
  680. *
  681. * @return false|string
  682. */
  683. function getHeight() {
  684. if (!isset($this->_info['sof'])) {
  685. $this->_parseMarkerSOF();
  686. }
  687. if ($this->_markers == null) {
  688. return false;
  689. }
  690. if (isset($this->_info['sof']['ImageHeight'])) {
  691. return $this->_info['sof']['ImageHeight'];
  692. }
  693. if (!isset($this->_info['exif'])) {
  694. $this->_parseMarkerExif();
  695. }
  696. if (isset($this->_info['exif']['PixelYDimension'])) {
  697. return $this->_info['exif']['PixelYDimension'];
  698. }
  699. return false;
  700. }
  701. /**
  702. * Get an dimension string for use in img tag
  703. *
  704. * @author Sebastian Delmont <sdelmont@zonageek.com>
  705. *
  706. * @return false|string
  707. */
  708. function getDimStr() {
  709. if ($this->_markers == null) {
  710. return false;
  711. }
  712. $w = $this->getWidth();
  713. $h = $this->getHeight();
  714. return "width='" . $w . "' height='" . $h . "'";
  715. }
  716. /**
  717. * Checks for an embedded thumbnail
  718. *
  719. * @author Sebastian Delmont <sdelmont@zonageek.com>
  720. *
  721. * @param string $which possible values: 'any', 'exif' or 'adobe'
  722. * @return false|string
  723. */
  724. function hasThumbnail($which = 'any') {
  725. if (($which == 'any') || ($which == 'exif')) {
  726. if (!isset($this->_info['exif'])) {
  727. $this->_parseMarkerExif();
  728. }
  729. if ($this->_markers == null) {
  730. return false;
  731. }
  732. if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
  733. if (isset($this->_info['exif']['JFIFThumbnail'])) {
  734. return 'exif';
  735. }
  736. }
  737. }
  738. if ($which == 'adobe') {
  739. if (!isset($this->_info['adobe'])) {
  740. $this->_parseMarkerAdobe();
  741. }
  742. if ($this->_markers == null) {
  743. return false;
  744. }
  745. if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
  746. if (isset($this->_info['adobe']['ThumbnailData'])) {
  747. return 'exif';
  748. }
  749. }
  750. }
  751. return false;
  752. }
  753. /**
  754. * Send embedded thumbnail to browser
  755. *
  756. * @author Sebastian Delmont <sdelmont@zonageek.com>
  757. *
  758. * @param string $which possible values: 'any', 'exif' or 'adobe'
  759. * @return bool
  760. */
  761. function sendThumbnail($which = 'any') {
  762. $data = null;
  763. if (($which == 'any') || ($which == 'exif')) {
  764. if (!isset($this->_info['exif'])) {
  765. $this->_parseMarkerExif();
  766. }
  767. if ($this->_markers == null) {
  768. return false;
  769. }
  770. if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
  771. if (isset($this->_info['exif']['JFIFThumbnail'])) {
  772. $data =& $this->_info['exif']['JFIFThumbnail'];
  773. }
  774. }
  775. }
  776. if (($which == 'adobe') || ($data == null)){
  777. if (!isset($this->_info['adobe'])) {
  778. $this->_parseMarkerAdobe();
  779. }
  780. if ($this->_markers == null) {
  781. return false;
  782. }
  783. if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
  784. if (isset($this->_info['adobe']['ThumbnailData'])) {
  785. $data =& $this->_info['adobe']['ThumbnailData'];
  786. }
  787. }
  788. }
  789. if ($data != null) {
  790. header("Content-type: image/jpeg");
  791. echo $data;
  792. return true;
  793. }
  794. return false;
  795. }
  796. /**
  797. * Save changed Metadata
  798. *
  799. * @author Sebastian Delmont <sdelmont@zonageek.com>
  800. * @author Andreas Gohr <andi@splitbrain.org>
  801. *
  802. * @param string $fileName file name or empty string for a random name
  803. * @return bool
  804. */
  805. function save($fileName = "") {
  806. if ($fileName == "") {
  807. $tmpName = tempnam(dirname($this->_fileName),'_metatemp_');
  808. $this->_writeJPEG($tmpName);
  809. if (file_exists($tmpName)) {
  810. return io_rename($tmpName, $this->_fileName);
  811. }
  812. } else {
  813. return $this->_writeJPEG($fileName);
  814. }
  815. return false;
  816. }
  817. /*************************************************************/
  818. /* PRIVATE FUNCTIONS (Internal Use Only!) */
  819. /*************************************************************/
  820. /*************************************************************/
  821. function _dispose($fileName = "") {
  822. $this->_fileName = $fileName;
  823. $this->_fp = null;
  824. $this->_type = 'unknown';
  825. unset($this->_markers);
  826. unset($this->_info);
  827. }
  828. /*************************************************************/
  829. function _readJPEG() {
  830. unset($this->_markers);
  831. //unset($this->_info);
  832. $this->_markers = array();
  833. //$this->_info = array();
  834. $this->_fp = @fopen($this->_fileName, 'rb');
  835. if ($this->_fp) {
  836. if (file_exists($this->_fileName)) {
  837. $this->_type = 'file';
  838. }
  839. else {
  840. $this->_type = 'url';
  841. }
  842. } else {
  843. $this->_fp = null;
  844. return false; // ERROR: Can't open file
  845. }
  846. // Check for the JPEG signature
  847. $c1 = ord(fgetc($this->_fp));
  848. $c2 = ord(fgetc($this->_fp));
  849. if ($c1 != 0xFF || $c2 != 0xD8) { // (0xFF + SOI)
  850. $this->_markers = null;
  851. return false; // ERROR: File is not a JPEG
  852. }
  853. $count = 0;
  854. $done = false;
  855. $ok = true;
  856. while (!$done) {
  857. $capture = false;
  858. // First, skip any non 0xFF bytes
  859. $discarded = 0;
  860. $c = ord(fgetc($this->_fp));
  861. while (!feof($this->_fp) && ($c != 0xFF)) {
  862. $discarded++;
  863. $c = ord(fgetc($this->_fp));
  864. }
  865. // Then skip all 0xFF until the marker byte
  866. do {
  867. $marker = ord(fgetc($this->_fp));
  868. } while (!feof($this->_fp) && ($marker == 0xFF));
  869. if (feof($this->_fp)) {
  870. return false; // ERROR: Unexpected EOF
  871. }
  872. if ($discarded != 0) {
  873. return false; // ERROR: Extraneous data
  874. }
  875. $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
  876. if (feof($this->_fp)) {
  877. return false; // ERROR: Unexpected EOF
  878. }
  879. if ($length < 2) {
  880. return false; // ERROR: Extraneous data
  881. }
  882. $length = $length - 2; // The length we got counts itself
  883. switch ($marker) {
  884. case 0xC0: // SOF0
  885. case 0xC1: // SOF1
  886. case 0xC2: // SOF2
  887. case 0xC9: // SOF9
  888. case 0xE0: // APP0: JFIF data
  889. case 0xE1: // APP1: EXIF or XMP data
  890. case 0xED: // APP13: IPTC / Photoshop data
  891. $capture = true;
  892. break;
  893. case 0xDA: // SOS: Start of scan... the image itself and the last block on the file
  894. $capture = false;
  895. $length = -1; // This field has no length... it includes all data until EOF
  896. $done = true;
  897. break;
  898. default:
  899. $capture = true;//false;
  900. break;
  901. }
  902. $this->_markers[$count] = array();
  903. $this->_markers[$count]['marker'] = $marker;
  904. $this->_markers[$count]['length'] = $length;
  905. if ($capture) {
  906. if ($length)
  907. $this->_markers[$count]['data'] = fread($this->_fp, $length);
  908. else
  909. $this->_markers[$count]['data'] = "";
  910. }
  911. elseif (!$done) {
  912. $result = @fseek($this->_fp, $length, SEEK_CUR);
  913. // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
  914. if (!($result === 0)) {
  915. for ($i = 0; $i < $length; $i++) {
  916. fgetc($this->_fp);
  917. }
  918. }
  919. }
  920. $count++;
  921. }
  922. if ($this->_fp) {
  923. fclose($this->_fp);
  924. $this->_fp = null;
  925. }
  926. return $ok;
  927. }
  928. /*************************************************************/
  929. function _parseAll() {
  930. if (!isset($this->_info['file'])) {
  931. $this->_parseFileInfo();
  932. }
  933. if (!isset($this->_markers)) {
  934. $this->_readJPEG();
  935. }
  936. if ($this->_markers == null) {
  937. return false;
  938. }
  939. if (!isset($this->_info['jfif'])) {
  940. $this->_parseMarkerJFIF();
  941. }
  942. if (!isset($this->_info['jpeg'])) {
  943. $this->_parseMarkerSOF();
  944. }
  945. if (!isset($this->_info['exif'])) {
  946. $this->_parseMarkerExif();
  947. }
  948. if (!isset($this->_info['xmp'])) {
  949. $this->_parseMarkerXmp();
  950. }
  951. if (!isset($this->_info['adobe'])) {
  952. $this->_parseMarkerAdobe();
  953. }
  954. }
  955. /*************************************************************/
  956. /**
  957. * @param string $outputName
  958. *
  959. * @return bool
  960. */
  961. function _writeJPEG($outputName) {
  962. $this->_parseAll();
  963. $wroteEXIF = false;
  964. $wroteAdobe = false;
  965. $this->_fp = @fopen($this->_fileName, 'r');
  966. if ($this->_fp) {
  967. if (file_exists($this->_fileName)) {
  968. $this->_type = 'file';
  969. }
  970. else {
  971. $this->_type = 'url';
  972. }
  973. } else {
  974. $this->_fp = null;
  975. return false; // ERROR: Can't open file
  976. }
  977. $this->_fpout = fopen($outputName, 'wb');
  978. if (!$this->_fpout) {
  979. $this->_fpout = null;
  980. fclose($this->_fp);
  981. $this->_fp = null;
  982. return false; // ERROR: Can't open output file
  983. }
  984. // Check for the JPEG signature
  985. $c1 = ord(fgetc($this->_fp));
  986. $c2 = ord(fgetc($this->_fp));
  987. if ($c1 != 0xFF || $c2 != 0xD8) { // (0xFF + SOI)
  988. return false; // ERROR: File is not a JPEG
  989. }
  990. fputs($this->_fpout, chr(0xFF), 1);
  991. fputs($this->_fpout, chr(0xD8), 1); // (0xFF + SOI)
  992. $count = 0;
  993. $done = false;
  994. $ok = true;
  995. while (!$done) {
  996. // First, skip any non 0xFF bytes
  997. $discarded = 0;
  998. $c = ord(fgetc($this->_fp));
  999. while (!feof($this->_fp) && ($c != 0xFF)) {
  1000. $discarded++;
  1001. $c = ord(fgetc($this->_fp));
  1002. }
  1003. // Then skip all 0xFF until the marker byte
  1004. do {
  1005. $marker = ord(fgetc($this->_fp));
  1006. } while (!feof($this->_fp) && ($marker == 0xFF));
  1007. if (feof($this->_fp)) {
  1008. $ok = false;
  1009. break; // ERROR: Unexpected EOF
  1010. }
  1011. if ($discarded != 0) {
  1012. $ok = false;
  1013. break; // ERROR: Extraneous data
  1014. }
  1015. $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
  1016. if (feof($this->_fp)) {
  1017. $ok = false;
  1018. break; // ERROR: Unexpected EOF
  1019. }
  1020. if ($length < 2) {
  1021. $ok = false;
  1022. break; // ERROR: Extraneous data
  1023. }
  1024. $length = $length - 2; // The length we got counts itself
  1025. unset($data);
  1026. if ($marker == 0xE1) { // APP1: EXIF data
  1027. $data =& $this->_createMarkerEXIF();
  1028. $wroteEXIF = true;
  1029. }
  1030. elseif ($marker == 0xED) { // APP13: IPTC / Photoshop data
  1031. $data =& $this->_createMarkerAdobe();
  1032. $wroteAdobe = true;
  1033. }
  1034. elseif ($marker == 0xDA) { // SOS: Start of scan... the image itself and the last block on the file
  1035. $done = true;
  1036. }
  1037. if (!$wroteEXIF && (($marker < 0xE0) || ($marker > 0xEF))) {
  1038. if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
  1039. $exif =& $this->_createMarkerEXIF();
  1040. $this->_writeJPEGMarker(0xE1, strlen($exif), $exif, 0);
  1041. unset($exif);
  1042. }
  1043. $wroteEXIF = true;
  1044. }
  1045. if (!$wroteAdobe && (($marker < 0xE0) || ($marker > 0xEF))) {
  1046. if ((isset($this->_info['adobe']) && is_array($this->_info['adobe']))
  1047. || (isset($this->_info['iptc']) && is_array($this->_info['iptc']))) {
  1048. $adobe =& $this->_createMarkerAdobe();
  1049. $this->_writeJPEGMarker(0xED, strlen($adobe), $adobe, 0);
  1050. unset($adobe);
  1051. }
  1052. $wroteAdobe = true;
  1053. }
  1054. $origLength = $length;
  1055. if (isset($data)) {
  1056. $length = strlen($data);
  1057. }
  1058. if ($marker != -1) {
  1059. $this->_writeJPEGMarker($marker, $length, $data, $origLength);
  1060. }
  1061. }
  1062. if ($this->_fp) {
  1063. fclose($this->_fp);
  1064. $this->_fp = null;
  1065. }
  1066. if ($this->_fpout) {
  1067. fclose($this->_fpout);
  1068. $this->_fpout = null;
  1069. }
  1070. return $ok;
  1071. }
  1072. /*************************************************************/
  1073. /**
  1074. * @param integer $marker
  1075. * @param integer $length
  1076. * @param string $data
  1077. * @param integer $origLength
  1078. *
  1079. * @return bool
  1080. */
  1081. function _writeJPEGMarker($marker, $length, &$data, $origLength) {
  1082. if ($length <= 0) {
  1083. return false;
  1084. }
  1085. fputs($this->_fpout, chr(0xFF), 1);
  1086. fputs($this->_fpout, chr($marker), 1);
  1087. fputs($this->_fpout, chr((($length + 2) & 0x0000FF00) >> 8), 1);
  1088. fputs($this->_fpout, chr((($length + 2) & 0x000000FF) >> 0), 1);
  1089. if (isset($data)) {
  1090. // Copy the generated data
  1091. fputs($this->_fpout, $data, $length);
  1092. if ($origLength > 0) { // Skip the original data
  1093. $result = @fseek($this->_fp, $origLength, SEEK_CUR);
  1094. // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
  1095. if ($result != 0) {
  1096. for ($i = 0; $i < $origLength; $i++) {
  1097. fgetc($this->_fp);
  1098. }
  1099. }
  1100. }
  1101. } else {
  1102. if ($marker == 0xDA) { // Copy until EOF
  1103. while (!feof($this->_fp)) {
  1104. $data = fread($this->_fp, 1024 * 16);
  1105. fputs($this->_fpout, $data, strlen($data));
  1106. }
  1107. } else { // Copy only $length bytes
  1108. $data = @fread($this->_fp, $length);
  1109. fputs($this->_fpout, $data, $length);
  1110. }
  1111. }
  1112. return true;
  1113. }
  1114. /**
  1115. * Gets basic info from the file - should work with non-JPEGs
  1116. *
  1117. * @author Sebastian Delmont <sdelmont@zonageek.com>
  1118. * @author Andreas Gohr <andi@splitbrain.org>
  1119. */
  1120. function _parseFileInfo() {
  1121. if (file_exists($this->_fileName) && is_file($this->_fileName)) {
  1122. $this->_info['file'] = array();
  1123. $this->_info['file']['Name'] = utf8_decodeFN(\dokuwiki\Utf8\PhpString::basename($this->_fileName));
  1124. $this->_info['file']['Path'] = fullpath($this->_fileName);
  1125. $this->_info['file']['Size'] = filesize($this->_fileName);
  1126. if ($this->_info['file']['Size'] < 1024) {
  1127. $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
  1128. } elseif ($this->_info['file']['Size'] < (1024 * 1024)) {
  1129. $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / 1024) . 'KB';
  1130. } elseif ($this->_info['file']['Size'] < (1024 * 1024 * 1024)) {
  1131. $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / (1024*1024)) . 'MB';
  1132. } else {
  1133. $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
  1134. }
  1135. $this->_info['file']['UnixTime'] = filemtime($this->_fileName);
  1136. // get image size directly from file
  1137. if ($size = getimagesize($this->_fileName)) {
  1138. $this->_info['file']['Width'] = $size[0];
  1139. $this->_info['file']['Height'] = $size[1];
  1140. // set mime types and formats
  1141. // http://php.net/manual/en/function.getimagesize.php
  1142. // http://php.net/manual/en/function.image-type-to-mime-type.php
  1143. switch ($size[2]) {
  1144. case 1:
  1145. $this->_info['file']['Mime'] = 'image/gif';
  1146. $this->_info['file']['Format'] = 'GIF';
  1147. break;
  1148. case 2:
  1149. $this->_info['file']['Mime'] = 'image/jpeg';
  1150. $this->_info['file']['Format'] = 'JPEG';
  1151. break;
  1152. case 3:
  1153. $this->_info['file']['Mime'] = 'image/png';
  1154. $this->_info['file']['Format'] = 'PNG';
  1155. break;
  1156. case 4:
  1157. $this->_info['file']['Mime'] = 'application/x-shockwave-flash';
  1158. $this->_info['file']['Format'] = 'SWF';
  1159. break;
  1160. case 5:
  1161. $this->_info['file']['Mime'] = 'image/psd';
  1162. $this->_info['file']['Format'] = 'PSD';
  1163. break;
  1164. case 6:
  1165. $this->_info['file']['Mime'] = 'image/bmp';
  1166. $this->_info['file']['Format'] = 'BMP';
  1167. break;
  1168. case 7:
  1169. $this->_info['file']['Mime'] = 'image/tiff';
  1170. $this->_info['file']['Format'] = 'TIFF (Intel)';
  1171. break;
  1172. case 8:
  1173. $this->_info['file']['Mime'] = 'image/tiff';
  1174. $this->_info['file']['Format'] = 'TIFF (Motorola)';
  1175. break;
  1176. case 9:
  1177. $this->_info['file']['Mime'] = 'application/octet-stream';
  1178. $this->_info['file']['Format'] = 'JPC';
  1179. break;
  1180. case 10:
  1181. $this->_info['file']['Mime'] = 'image/jp2';
  1182. $this->_info['file']['Format'] = 'JP2';
  1183. break;
  1184. case 11:
  1185. $this->_info['file']['Mime'] = 'application/octet-stream';
  1186. $this->_info['file']['Format'] = 'JPX';
  1187. break;
  1188. case 12:
  1189. $this->_info['file']['Mime'] = 'application/octet-stream';
  1190. $this->_info['file']['Format'] = 'JB2';
  1191. break;
  1192. case 13:
  1193. $this->_info['file']['Mime'] = 'application/x-shockwave-flash';
  1194. $this->_info['file']['Format'] = 'SWC';
  1195. break;
  1196. case 14:
  1197. $this->_info['file']['Mime'] = 'image/iff';
  1198. $this->_info['file']['Format'] = 'IFF';
  1199. break;
  1200. case 15:
  1201. $this->_info['file']['Mime'] = 'image/vnd.wap.wbmp';
  1202. $this->_info['file']['Format'] = 'WBMP';
  1203. break;
  1204. case 16:
  1205. $this->_info['file']['Mime'] = 'image/xbm';
  1206. $this->_info['file']['Format'] = 'XBM';
  1207. break;
  1208. default:
  1209. $this->_info['file']['Mime'] = 'image/unknown';
  1210. }
  1211. }
  1212. } else {
  1213. $this->_info['file'] = array();
  1214. $this->_info['file']['Name'] = \dokuwiki\Utf8\PhpString::basename($this->_fileName);
  1215. $this->_info['file']['Url'] = $this->_fileName;
  1216. }
  1217. return true;
  1218. }
  1219. /*************************************************************/
  1220. function _parseMarkerJFIF() {
  1221. if (!isset($this->_markers)) {
  1222. $this->_readJPEG();
  1223. }
  1224. if ($this->_markers == null || $this->_isMarkerDisabled(('jfif'))) {
  1225. return false;
  1226. }
  1227. try {
  1228. $data = null;
  1229. $count = count($this->_markers);
  1230. for ($i = 0; $i < $count; $i++) {
  1231. if ($this->_markers[$i]['marker'] == 0xE0) {
  1232. $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 4);
  1233. if ($signature == 'JFIF') {
  1234. $data =& $this->_markers[$i]['data'];
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. if ($data == null) {
  1240. $this->_info['jfif'] = false;
  1241. return false;
  1242. }
  1243. $this->_info['jfif'] = array();
  1244. $vmaj = $this->_getByte($data, 5);
  1245. $vmin = $this->_getByte($data, 6);
  1246. $this->_info['jfif']['Version'] = sprintf('%d.%02d', $vmaj, $vmin);
  1247. $units = $this->_getByte($data, 7);
  1248. switch ($units) {
  1249. case 0:
  1250. $this->_info['jfif']['Units'] = 'pixels';
  1251. break;
  1252. case 1:
  1253. $this->_info['jfif']['Units'] = 'dpi';
  1254. break;
  1255. case 2:
  1256. $this->_info['jfif']['Units'] = 'dpcm';
  1257. break;
  1258. default:
  1259. $this->_info['jfif']['Units'] = 'unknown';
  1260. break;
  1261. }
  1262. $xdens = $this->_getShort($data, 8);
  1263. $ydens = $this->_getShort($data, 10);
  1264. $this->_info['jfif']['XDensity'] = $xdens;
  1265. $this->_info['jfif']['YDensity'] = $ydens;
  1266. $thumbx = $this->_getByte($data, 12);
  1267. $thumby = $this->_getByte($data, 13);
  1268. $this->_info['jfif']['ThumbnailWidth'] = $thumbx;
  1269. $this->_info['jfif']['ThumbnailHeight'] = $thumby;
  1270. } catch(Exception $e) {
  1271. $this->_handleMarkerParsingException($e);
  1272. $this->_info['jfif'] = false;
  1273. return false;
  1274. }
  1275. return true;
  1276. }
  1277. /*************************************************************/
  1278. function _parseMarkerSOF() {
  1279. if (!isset($this->_markers)) {
  1280. $this->_readJPEG();
  1281. }
  1282. if ($this->_markers == null || $this->_isMarkerDisabled(('sof'))) {
  1283. return false;
  1284. }
  1285. try {
  1286. $data = null;
  1287. $count = count($this->_markers);
  1288. for ($i = 0; $i < $count; $i++) {
  1289. switch ($this->_markers[$i]['marker']) {
  1290. case 0xC0: // SOF0
  1291. case 0xC1: // SOF1
  1292. case 0xC2: // SOF2
  1293. case 0xC9: // SOF9
  1294. $data =& $this->_markers[$i]['data'];
  1295. $marker = $this->_markers[$i]['marker'];
  1296. break;
  1297. }
  1298. }
  1299. if ($data == null) {
  1300. $this->_info['sof'] = false;
  1301. return false;
  1302. }
  1303. $pos = 0;
  1304. $this->_info['sof'] = array();
  1305. switch ($marker) {
  1306. case 0xC0: // SOF0
  1307. $format = 'Baseline';
  1308. break;
  1309. case 0xC1: // SOF1
  1310. $format = 'Progessive';
  1311. break;
  1312. case 0xC2: // SOF2
  1313. $format = 'Non-baseline';
  1314. break;
  1315. case 0xC9: // SOF9
  1316. $format = 'Arithmetic';
  1317. break;
  1318. default:
  1319. return false;
  1320. }
  1321. $this->_info['sof']['Format'] = $format;
  1322. $this->_info['sof']['SamplePrecision'] = $this->_getByte($data, $pos + 0);
  1323. $this->_info['sof']['ImageHeight'] = $this->_getShort($data, $pos + 1);
  1324. $this->_info['sof']['ImageWidth'] = $this->_getShort($data, $pos + 3);
  1325. $this->_info['sof']['ColorChannels'] = $this->_getByte($data, $pos + 5);
  1326. } catch(Exception $e) {
  1327. $this->_handleMarkerParsingException($e);
  1328. $this->_info['sof'] = false;
  1329. return false;
  1330. }
  1331. return true;
  1332. }
  1333. /**
  1334. * Parses the XMP data
  1335. *
  1336. * @author Hakan Sandell <hakan.sandell@mydata.se>
  1337. */
  1338. function _parseMarkerXmp() {
  1339. if (!isset($this->_markers)) {
  1340. $this->_readJPEG();
  1341. }
  1342. if ($this->_markers == null || $this->_isMarkerDisabled(('xmp'))) {
  1343. return false;
  1344. }
  1345. try {
  1346. $data = null;
  1347. $count = count($this->_markers);
  1348. for ($i = 0; $i < $count; $i++) {
  1349. if ($this->_markers[$i]['marker'] == 0xE1) {
  1350. $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 29);
  1351. if ($signature == "http://ns.adobe.com/xap/1.0/\0") {
  1352. $data = substr($this->_markers[$i]['data'], 29);
  1353. break;
  1354. }
  1355. }
  1356. }
  1357. if ($data == null) {
  1358. $this->_info['xmp'] = false;
  1359. return false;
  1360. }
  1361. $parser = xml_parser_create();
  1362. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  1363. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  1364. $result = xml_parse_into_struct($parser, $data, $values, $tags);
  1365. xml_parser_free($parser);
  1366. if ($result == 0) {
  1367. $this->_info['xmp'] = false;
  1368. return false;
  1369. }
  1370. $this->_info['xmp'] = array();
  1371. $count = count($values);
  1372. for ($i = 0; $i < $count; $i++) {
  1373. if ($values[$i]['tag'] == 'rdf:Description' && $values[$i]['type'] == 'open') {
  1374. while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Description')) {
  1375. $this->_parseXmpNode($values, $i, $this->_info['xmp'][$values[$i]['tag']], $count);
  1376. }
  1377. }
  1378. }
  1379. } catch (Exception $e) {
  1380. $this->_handleMarkerParsingException($e);
  1381. $this->_info['xmp'] = false;
  1382. return false;
  1383. }
  1384. return true;
  1385. }
  1386. /**
  1387. * Parses XMP nodes by recursion
  1388. *
  1389. * @author Hakan Sandell <hakan.sandell@mydata.se>
  1390. *
  1391. * @param array $values
  1392. * @param int $i
  1393. * @param mixed $meta
  1394. * @param integer $count
  1395. */
  1396. function _parseXmpNode($values, &$i, &$meta, $count) {
  1397. if ($values[$i]['type'] == 'close') return;
  1398. if ($values[$i]['type'] == 'complete') {
  1399. // Simple Type property
  1400. $meta = $values[$i]['value'] ?? '';
  1401. return;
  1402. }
  1403. $i++;
  1404. if ($i >= $count) return;
  1405. if ($values[$i]['tag'] == 'rdf:Bag' || $values[$i]['tag'] == 'rdf:Seq') {
  1406. // Array property
  1407. $meta = array();
  1408. while ($values[++$i]['tag'] == 'rdf:li') {
  1409. $this->_parseXmpNode($values, $i, $meta[], $count);
  1410. }
  1411. $i++; // skip closing Bag/Seq tag
  1412. } elseif ($values[$i]['tag'] == 'rdf:Alt') {
  1413. // Language Alternative property, only the first (default) value is used
  1414. if ($values[$i]['type'] == 'open') {
  1415. $i++;
  1416. $this->_parseXmpNode($values, $i, $meta, $count);
  1417. while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Alt'));
  1418. $i++; // skip closing Alt tag
  1419. }
  1420. } else {
  1421. // Structure property
  1422. $meta = array();
  1423. $startTag = $values[$i-1]['tag'];
  1424. do {
  1425. $this->_parseXmpNode($values, $i, $meta[$values[$i]['tag']], $count);
  1426. } while ((++$i < $count) && ($values[$i]['tag'] != $startTag));
  1427. }
  1428. }
  1429. /*************************************************************/
  1430. function _parseMarkerExif() {
  1431. if (!isset($this->_markers)) {
  1432. $this->_readJPEG();
  1433. }
  1434. if ($this->_markers == null || $this->_isMarkerDisabled(('exif'))) {
  1435. return false;
  1436. }
  1437. try {
  1438. $data = null;
  1439. $count = count($this->_markers);
  1440. for ($i = 0; $i < $count; $i++) {
  1441. if ($this->_markers[$i]['marker'] == 0xE1) {
  1442. $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
  1443. if ($signature == "Exif\0\0") {
  1444. $data =& $this->_markers[$i]['data'];
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. if ($data == null) {
  1450. $this->_info['exif'] = false;
  1451. return false;
  1452. }
  1453. $pos = 6;
  1454. $this->_info['exif'] = array();
  1455. // We don't increment $pos after this because Exif uses offsets relative to this point
  1456. $byteAlign = $this->_getShort($data, $pos + 0);
  1457. if ($byteAlign == 0x4949) { // "II"
  1458. $isBigEndian = false;
  1459. } elseif ($byteAlign == 0x4D4D) { // "MM"
  1460. $isBigEndian = true;
  1461. } else {
  1462. return false; // Unexpected data
  1463. }
  1464. $alignCheck = $this->_getShort($data, $pos + 2, $isBigEndian);
  1465. if ($alignCheck != 0x002A) // That's the expected value
  1466. return false; // Unexpected data
  1467. if ($isBigEndian) {
  1468. $this->_info['exif']['ByteAlign'] = "Big Endian";
  1469. } else {
  1470. $this->_info['exif']['ByteAlign'] = "Little Endian";
  1471. }
  1472. $offsetIFD0 = $this->_getLong($data, $pos + 4, $isBigEndian);
  1473. if ($offsetIFD0 < 8)
  1474. return false; // Unexpected data
  1475. $offsetIFD1 = $this->_readIFD($data, $pos, $offsetIFD0, $isBigEndian, 'ifd0');
  1476. if ($offsetIFD1 != 0)
  1477. $this->_readIFD($data, $pos, $offsetIFD1, $isBigEndian, 'ifd1');
  1478. } catch(Exception $e) {
  1479. $this->_handleMarkerParsingException($e);
  1480. $this->_info['exif'] = false;
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. /*************************************************************/
  1486. /**
  1487. * @param mixed $data
  1488. * @param integer $base
  1489. * @param integer $offset
  1490. * @param boolean $isBigEndian
  1491. * @param string $mode
  1492. *
  1493. * @return int
  1494. */
  1495. function _readIFD($data, $base, $offset, $isBigEndian, $mode) {
  1496. $EXIFTags = $this->_exifTagNames($mode);
  1497. $numEntries = $this->_getShort($data, $base + $offset, $isBigEndian);
  1498. $offset += 2;
  1499. $exifTIFFOffset = 0;
  1500. $exifTIFFLength = 0;
  1501. $exifThumbnailOffset = 0;
  1502. $exifThumbnailLength = 0;
  1503. for ($i = 0; $i < $numEntries; $i++) {
  1504. $tag = $this->_getShort($data, $base + $offset, $isBigEndian);
  1505. $offset += 2;
  1506. $type = $this->_getShort($data, $base + $offset, $isBigEndian);
  1507. $offset += 2;
  1508. $count = $this->_getLong($data, $base + $offset, $isBigEndian);
  1509. $offset += 4;
  1510. if (($type < 1) || ($type > 12))
  1511. return false; // Unexpected Type
  1512. $typeLengths = array( -1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 );
  1513. $dataLength = $typeLengths[$type] * $count;
  1514. if ($dataLength > 4) {
  1515. $dataOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
  1516. $rawValue = $this->_getFixedString($data, $base + $dataOffset, $dataLength);
  1517. } else {
  1518. $rawValue = $this->_getFixedString($data, $base + $offset, $dataLength);
  1519. }
  1520. $offset += 4;
  1521. switch ($type) {
  1522. case 1: // UBYTE
  1523. if ($count == 1) {
  1524. $value = $this->_getByte($rawValue, 0);
  1525. } else {
  1526. $value = array();
  1527. for ($j = 0; $j < $count; $j++)
  1528. $value[$j] = $this->_getByte($rawValue, $j);
  1529. }
  1530. break;
  1531. case 2: // ASCII
  1532. $value = $rawValue;
  1533. break;
  1534. case 3: // USHORT
  1535. if ($count == 1) {
  1536. $value = $this->_getShort($rawValue, 0, $isBigEndian);
  1537. } else {
  1538. $value = array();
  1539. for ($j = 0; $j < $count; $j++)
  1540. $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
  1541. }
  1542. break;
  1543. case 4: // ULONG
  1544. if ($count == 1) {
  1545. $value = $this->_getLong($rawValue, 0, $isBigEndian);
  1546. } else {
  1547. $value = array();
  1548. for ($j = 0; $j < $count; $j++)
  1549. $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
  1550. }
  1551. break;
  1552. case 5: // URATIONAL
  1553. if ($count == 1) {
  1554. $a = $this->_getLong($rawValue, 0, $isBigEndian);
  1555. $b = $this->_getLong($rawValue, 4, $isBigEndian);
  1556. $value = array();
  1557. $value['val'] = 0;
  1558. $value['num'] = $a;
  1559. $value['den'] = $b;
  1560. if (($a != 0) && ($b != 0)) {
  1561. $value['val'] = $a / $b;
  1562. }
  1563. } else {
  1564. $value = array();
  1565. for ($j = 0; $j < $count; $j++) {
  1566. $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
  1567. $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
  1568. $value = array();
  1569. $value[$j]['val'] = 0;
  1570. $value[$j]['num'] = $a;
  1571. $value[$j]['den'] = $b;
  1572. if (($a != 0) && ($b != 0))
  1573. $value[$j]['val'] = $a / $b;
  1574. }
  1575. }
  1576. break;
  1577. case 6: // SBYTE
  1578. if ($count == 1) {
  1579. $value = $this->_getByte($rawValue, 0);
  1580. } else {
  1581. $value = array();
  1582. for ($j = 0; $j < $count; $j++)
  1583. $value[$j] = $this->_getByte($rawValue, $j);
  1584. }
  1585. break;
  1586. case 7: // UNDEFINED
  1587. $value = $rawValue;
  1588. break;
  1589. case 8: // SSHORT
  1590. if ($count == 1) {
  1591. $value = $this->_getShort($rawValue, 0, $isBigEndian);
  1592. } else {
  1593. $value = array();
  1594. for ($j = 0; $j < $count; $j++)
  1595. $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
  1596. }
  1597. break;
  1598. case 9: // SLONG
  1599. if ($count == 1) {
  1600. $value = $this->_getLong($rawValue, 0, $isBigEndian);
  1601. } else {
  1602. $value = array();
  1603. for ($j = 0; $j < $count; $j++)
  1604. $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
  1605. }
  1606. break;
  1607. case 10: // SRATIONAL
  1608. if ($count == 1) {
  1609. $a = $this->_getLong($rawValue, 0, $isBigEndian);
  1610. $b = $this->_getLong($rawValue, 4, $isBigEndian);
  1611. $value = array();
  1612. $value['val'] = 0;
  1613. $value['num'] = $a;
  1614. $value['den'] = $b;
  1615. if (($a != 0) && ($b != 0))
  1616. $value['val'] = $a / $b;
  1617. } else {
  1618. $value = array();
  1619. for ($j = 0; $j < $count; $j++) {
  1620. $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
  1621. $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
  1622. $value = array();
  1623. $value[$j]['val'] = 0;
  1624. $value[$j]['num'] = $a;
  1625. $value[$j]['den'] = $b;
  1626. if (($a != 0) && ($b != 0))
  1627. $value[$j]['val'] = $a / $b;
  1628. }
  1629. }
  1630. break;
  1631. case 11: // FLOAT
  1632. $value = $rawValue;
  1633. break;
  1634. case 12: // DFLOAT
  1635. $value = $rawValue;
  1636. break;
  1637. default:
  1638. return false; // Unexpected Type
  1639. }
  1640. $tagName = '';
  1641. if (($mode == 'ifd0') && ($tag == 0x8769)) { // ExifIFDOffset
  1642. $this->_readIFD($data, $base, $value, $isBigEndian, 'exif');
  1643. } elseif (($mode == 'ifd0') && ($tag == 0x8825)) { // GPSIFDOffset
  1644. $this->_readIFD($data, $base, $value, $isBigEndian, 'gps');
  1645. } elseif (($mode == 'ifd1') && ($tag == 0x0111)) { // TIFFStripOffsets
  1646. $exifTIFFOffset = $value;
  1647. } elseif (($mode == 'ifd1') && ($tag == 0x0117)) { // TIFFStripByteCounts
  1648. $exifTIFFLength = $value;
  1649. } elseif (($mode == 'ifd1') && ($tag == 0x0201)) { // TIFFJFIFOffset
  1650. $exifThumbnailOffset = $value;
  1651. } elseif (($mode == 'ifd1') && ($tag == 0x0202)) { // TIFFJFIFLength
  1652. $exifThumbnailLength = $value;
  1653. } elseif (($mode == 'exif') && ($tag == 0xA005)) { // InteropIFDOffset
  1654. $this->_readIFD($data, $base, $value, $isBigEndian, 'interop');
  1655. }
  1656. // elseif (($mode == 'exif') && ($tag == 0x927C)) { // MakerNote
  1657. // }
  1658. else {
  1659. if (isset($EXIFTags[$tag])) {
  1660. $tagName = $EXIFTags[$tag];
  1661. if (isset($this->_info['exif'][$tagName])) {
  1662. if (!is_array($this->_info['exif'][$tagName])) {
  1663. $aux = array();
  1664. $aux[0] = $this->_info['exif'][$tagName];
  1665. $this->_info['exif'][$tagName] = $aux;
  1666. }
  1667. $this->_info['exif'][$tagName][count($this->_info['exif'][$tagName])] = $value;
  1668. } else {
  1669. $this->_info['exif'][$tagName] = $value;
  1670. }
  1671. }
  1672. /*
  1673. else {
  1674. echo sprintf("<h1>Unknown tag %02x (t: %d l: %d) %s in %s</h1>", $tag, $type, $count, $mode, $this->_fileName);
  1675. // Unknown Tags will be ignored!!!
  1676. // That's because the tag might be a pointer (like the Exif tag)
  1677. // and saving it without saving the data it points to might
  1678. // create an invalid file.
  1679. }
  1680. */
  1681. }
  1682. }
  1683. if (($exifThumbnailOffset > 0) && ($exifThumbnailLength > 0)) {
  1684. $this->_info['exif']['JFIFThumbnail'] = $this->_getFixedString($data, $base + $exifThumbnailOffset, $exifThumbnailLength);
  1685. }
  1686. if (($exifTIFFOffset > 0) && ($exifTIFFLength > 0)) {
  1687. $this->_info['exif']['TIFFStrips'] = $this->_getFixedString($data, $base + $exifTIFFOffset, $exifTIFFLength);
  1688. }
  1689. $nextOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
  1690. return $nextOffset;
  1691. }
  1692. /*************************************************************/
  1693. function & _createMarkerExif() {
  1694. $data = null;
  1695. $count = count($this->_markers);
  1696. for ($i = 0; $i < $count; $i++) {
  1697. if ($this->_markers[$i]['marker'] == 0xE1) {
  1698. $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
  1699. if ($signature == "Exif\0\0") {
  1700. $data =& $this->_markers[$i]['data'];
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. if (!isset($this->_info['exif'])) {
  1706. return false;
  1707. }
  1708. $data = "Exif\0\0";
  1709. $pos = 6;
  1710. $offsetBase = 6;
  1711. if (isset($this->_info['exif']['ByteAlign']) && ($this->_info['exif']['ByteAlign'] == "Big Endian")) {
  1712. $isBigEndian = true;
  1713. $aux = "MM";
  1714. $pos = $this->_putString($data, $pos, $aux);
  1715. } else {
  1716. $isBigEndian = false;
  1717. $aux = "II";
  1718. $pos = $this->_putString($data, $pos, $aux);
  1719. }
  1720. $pos = $this->_putShort($data, $pos, 0x002A, $isBigEndian);
  1721. $pos = $this->_putLong($data, $pos, 0x00000008, $isBigEndian); // IFD0 Offset is always 8
  1722. $ifd0 =& $this->_getIFDEntries($isBigEndian, 'ifd0');
  1723. $ifd1 =& $this->_getIFDEntries($isBigEndian, 'ifd1');
  1724. $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd0, $isBigEndian, true);
  1725. $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd1, $isBigEndian, false);
  1726. return $data;
  1727. }
  1728. /*************************************************************/
  1729. /**
  1730. * @param mixed $data
  1731. * @param integer $pos
  1732. * @param integer $offsetBase
  1733. * @param array $entries
  1734. * @param boolean $isBigEndian
  1735. * @param boolean $hasNext
  1736. *
  1737. * @return mixed
  1738. */
  1739. function _writeIFD(&$data, $pos, $offsetBase, &$entries, $isBigEndian, $hasNext) {
  1740. $tiffData = null;
  1741. $tiffDataOffsetPos = -1;
  1742. $entryCount = count($entries);
  1743. $dataPos = $pos + 2 + ($entryCount * 12) + 4;
  1744. $pos = $this->_putShort($data, $pos, $entryCount, $isBigEndian);
  1745. for ($i = 0; $i < $entryCount; $i++) {
  1746. $tag = $entries[$i]['tag'];
  1747. $type = $entries[$i]['type'];
  1748. if ($type == -99) { // SubIFD
  1749. $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
  1750. $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
  1751. $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
  1752. $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
  1753. $dataPos = $this->_writeIFD($data, $dataPos, $offsetBase, $entries[$i]['value'], $isBigEndian, false);
  1754. } elseif ($type == -98) { // TIFF Data
  1755. $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
  1756. $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
  1757. $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
  1758. $tiffDataOffsetPos = $pos;
  1759. $pos = $this->_putLong($data, $pos, 0x00, $isBigEndian); // For Now
  1760. $tiffData =& $entries[$i]['value'] ;
  1761. } else { // Regular Entry
  1762. $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
  1763. $pos = $this->_putShort($data, $pos, $type, $isBigEndian);
  1764. $pos = $this->_putLong($data, $pos, $entries[$i]['count'], $isBigEndian);
  1765. if (strlen($entries[$i]['value']) > 4) {
  1766. $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
  1767. $dataPos = $this->_putString($data, $dataPos, $entries[$i]['value']);
  1768. } else {
  1769. $val = str_pad($entries[$i]['value'], 4, "\0");
  1770. $pos = $this->_putString($data, $pos, $val);
  1771. }
  1772. }
  1773. }
  1774. if ($tiffData != null) {
  1775. $this->_putLong($data, $tiffDataOffsetPos, $dataPos - $offsetBase, $isBigEndian);
  1776. $dataPos = $this->_putString($data, $dataPos, $tiffData);
  1777. }
  1778. if ($hasNext) {
  1779. $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
  1780. } else {
  1781. $pos = $this->_putLong($data, $pos, 0, $isBigEndian);
  1782. }
  1783. return $dataPos;
  1784. }
  1785. /*************************************************************/
  1786. /**
  1787. * @param boolean $isBigEndian
  1788. * @param string $mode
  1789. *
  1790. * @return array
  1791. */
  1792. function & _getIFDEntries($isBigEndian, $mode) {
  1793. $EXIFNames = $this->_exifTagNames($mode);
  1794. $EXIFTags = $this->_exifNameTags($mode);
  1795. $EXIFTypeInfo = $this->_exifTagTypes($mode);
  1796. $ifdEntries = array();
  1797. $entryCount = 0;
  1798. foreach($EXIFNames as $tag => $name) {
  1799. $type = $EXIFTypeInfo[$tag][0];
  1800. $count = $EXIFTypeInfo[$tag][1];
  1801. $value = null;
  1802. if (($mode == 'ifd0') && ($tag == 0x8769)) { // ExifIFDOffset
  1803. if (isset($this->_info['exif']['EXIFVersion'])) {
  1804. $value =& $this->_getIFDEntries($isBigEndian, "exif");
  1805. $type = -99;
  1806. }
  1807. else {
  1808. $value = null;
  1809. }
  1810. } elseif (($mode == 'ifd0') && ($tag == 0x8825)) { // GPSIFDOffset
  1811. if (isset($this->_info['exif']['GPSVersionID'])) {
  1812. $value =& $this->_getIFDEntries($isBigEndian, "gps");
  1813. $type = -99;
  1814. } else {
  1815. $value = null;
  1816. }
  1817. } elseif (($mode == 'ifd1') && ($tag == 0x0111)) { // TIFFStripOffsets
  1818. if (isset($this->_info['exif']['TIFFStrips'])) {
  1819. $value =& $this->_info['exif']['TIFFStrips'];
  1820. $type = -98;
  1821. } else {
  1822. $value = null;
  1823. }
  1824. } elseif (($mode == 'ifd1') && ($tag == 0x0117)) { // TIFFStripByteCounts
  1825. if (isset($this->_info['exif']['TIFFStrips'])) {
  1826. $value = strlen($this->_info['exif']['TIFFStrips']);
  1827. } else {
  1828. $value = null;
  1829. }
  1830. } elseif (($mode == 'ifd1') && ($tag == 0x0201)) { // TIFFJFIFOffset
  1831. if (isset($this->_info['exif']['JFIFThumbnail'])) {
  1832. $value =& $this->_info['exif']['JFIFThumbnail'];
  1833. $type = -98;
  1834. } else {
  1835. $value = null;
  1836. }
  1837. } elseif (($mode == 'ifd1') && ($tag == 0x0202)) { // TIFFJFIFLength
  1838. if (isset($this->_info['exif']['JFIFThumbnail'])) {
  1839. $value = strlen($this->_info['exif']['JFIFThumbnail']);
  1840. } else {
  1841. $value = null;
  1842. }
  1843. } elseif (($mode == 'exif') && ($tag == 0xA005)) { // InteropIFDOffset
  1844. if (isset($this->_info['exif']['InteroperabilityIndex'])) {
  1845. $value =& $this->_getIFDEntries($isBigEndian, "interop");
  1846. $type = -99;
  1847. } else {
  1848. $value = null;
  1849. }
  1850. } elseif (isset($this->_info['exif'][$name])) {
  1851. $origValue =& $this->_info['exif'][$name];
  1852. // This makes it easier to process variable size elements
  1853. if (!is_array($origValue) || isset($origValue['val'])) {
  1854. unset($origValue); // Break the reference
  1855. $origValue = array($this->_info['exif'][$name]);
  1856. }
  1857. $origCount = count($origValue);
  1858. if ($origCount == 0 ) {
  1859. $type = -1; // To ignore this field
  1860. }
  1861. $value = " ";
  1862. switch ($type) {
  1863. case 1: // UBYTE
  1864. if ($count == 0) {
  1865. $count = $origCount;
  1866. }
  1867. $j = 0;
  1868. while (($j < $count) && ($j < $origCount)) {
  1869. $this->_putByte($value, $j, $origValue[$j]);
  1870. $j++;
  1871. }
  1872. while ($j < $count) {
  1873. $this->_putByte($value, $j, 0);
  1874. $j++;
  1875. }
  1876. break;
  1877. case 2: // ASCII
  1878. $v = strval($origValue[0]);
  1879. if (($count != 0) && (strlen($v) > $count)) {
  1880. $v = substr($v, 0, $count);
  1881. }
  1882. elseif (($count > 0) && (strlen($v) < $count)) {
  1883. $v = str_pad($v, $count, "\0");
  1884. }
  1885. $count = strlen($v);
  1886. $this->_putString($value, 0, $v);
  1887. break;
  1888. case 3: // USHORT
  1889. if ($count == 0) {
  1890. $count = $origCount;
  1891. }
  1892. $j = 0;
  1893. while (($j < $count) && ($j < $origCount)) {
  1894. $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
  1895. $j++;
  1896. }
  1897. while ($j < $count) {
  1898. $this->_putShort($value, $j * 2, 0, $isBigEndian);
  1899. $j++;
  1900. }
  1901. break;
  1902. case 4: // ULONG
  1903. if ($count == 0) {
  1904. $count = $origCount;
  1905. }
  1906. $j = 0;
  1907. while (($j < $count) && ($j < $origCount)) {
  1908. $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
  1909. $j++;
  1910. }
  1911. while ($j < $count) {
  1912. $this->_putLong($value, $j * 4, 0, $isBigEndian);
  1913. $j++;
  1914. }
  1915. break;
  1916. case 5: // URATIONAL
  1917. if ($count == 0) {
  1918. $count = $origCount;
  1919. }
  1920. $j = 0;
  1921. while (($j < $count) && ($j < $origCount)) {
  1922. $v = $origValue[$j];
  1923. if (is_array($v)) {
  1924. $a = $v['num'];
  1925. $b = $v['den'];
  1926. }
  1927. else {
  1928. $a = 0;
  1929. $b = 0;
  1930. // TODO: Allow other types and convert them
  1931. }
  1932. $this->_putLong($value, $j * 8, $a, $isBigEndian);
  1933. $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
  1934. $j++;
  1935. }
  1936. while ($j < $count) {
  1937. $this->_putLong($value, $j * 8, 0, $isBigEndian);
  1938. $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
  1939. $j++;
  1940. }
  1941. break;
  1942. case 6: // SBYTE
  1943. if ($count == 0) {
  1944. $count = $origCount;
  1945. }
  1946. $j = 0;
  1947. while (($j < $count) && ($j < $origCount)) {
  1948. $this->_putByte($value, $j, $origValue[$j]);
  1949. $j++;
  1950. }
  1951. while ($j < $count) {
  1952. $this->_putByte($value, $j, 0);
  1953. $j++;
  1954. }
  1955. break;
  1956. case 7: // UNDEFINED
  1957. $v = strval($origValue[0]);
  1958. if (($count != 0) && (strlen($v) > $count)) {
  1959. $v = substr($v, 0, $count);
  1960. }
  1961. elseif (($count > 0) && (strlen($v) < $count)) {
  1962. $v = str_pad($v, $count, "\0");
  1963. }
  1964. $count = strlen($v);
  1965. $this->_putString($value, 0, $v);
  1966. break;
  1967. case 8: // SSHORT
  1968. if ($count == 0) {
  1969. $count = $origCount;
  1970. }
  1971. $j = 0;
  1972. while (($j < $count) && ($j < $origCount)) {
  1973. $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
  1974. $j++;
  1975. }
  1976. while ($j < $count) {
  1977. $this->_putShort($value, $j * 2, 0, $isBigEndian);
  1978. $j++;
  1979. }
  1980. break;
  1981. case 9: // SLONG
  1982. if ($count == 0) {
  1983. $count = $origCount;
  1984. }
  1985. $j = 0;
  1986. while (($j < $count) && ($j < $origCount)) {
  1987. $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
  1988. $j++;
  1989. }
  1990. while ($j < $count) {
  1991. $this->_putLong($value, $j * 4, 0, $isBigEndian);
  1992. $j++;
  1993. }
  1994. break;
  1995. case 10: // SRATIONAL
  1996. if ($count == 0) {
  1997. $count = $origCount;
  1998. }
  1999. $j = 0;
  2000. while (($j < $count) && ($j < $origCount)) {
  2001. $v = $origValue[$j];
  2002. if (is_array($v)) {
  2003. $a = $v['num'];
  2004. $b = $v['den'];
  2005. }
  2006. else {
  2007. $a = 0;
  2008. $b = 0;
  2009. // TODO: Allow other types and convert them
  2010. }
  2011. $this->_putLong($value, $j * 8, $a, $isBigEndian);
  2012. $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
  2013. $j++;
  2014. }
  2015. while ($j < $count) {
  2016. $this->_putLong($value, $j * 8, 0, $isBigEndian);
  2017. $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
  2018. $j++;
  2019. }
  2020. break;
  2021. case 11: // FLOAT
  2022. if ($count == 0) {
  2023. $count = $origCount;
  2024. }
  2025. $j = 0;
  2026. while (($j < $count) && ($j < $origCount)) {
  2027. $v = strval($origValue[$j]);
  2028. if (strlen($v) > 4) {
  2029. $v = substr($v, 0, 4);
  2030. }
  2031. elseif (strlen($v) < 4) {
  2032. $v = str_pad($v, 4, "\0");
  2033. }
  2034. $this->_putString($value, $j * 4, $v);
  2035. $j++;
  2036. }
  2037. while ($j < $count) {
  2038. $v = "\0\0\0\0";
  2039. $this->_putString($value, $j * 4, $v);
  2040. $j++;
  2041. }
  2042. break;
  2043. case 12: // DFLOAT
  2044. if ($count == 0) {
  2045. $count = $origCount;
  2046. }
  2047. $j = 0;
  2048. while (($j < $count) && ($j < $origCount)) {
  2049. $v = strval($origValue[$j]);
  2050. if (strlen($v) > 8) {
  2051. $v = substr($v, 0, 8);
  2052. }
  2053. elseif (strlen($v) < 8) {
  2054. $v = str_pad($v, 8, "\0");
  2055. }
  2056. $this->_putString($value, $j * 8, $v);
  2057. $j++;
  2058. }
  2059. while ($j < $count) {
  2060. $v = "\0\0\0\0\0\0\0\0";
  2061. $this->_putString($value, $j * 8, $v);
  2062. $j++;
  2063. }
  2064. break;
  2065. default:
  2066. $value = null;
  2067. break;
  2068. }
  2069. }
  2070. if ($value != null) {
  2071. $ifdEntries[$entryCount] = array();
  2072. $ifdEntries[$entryCount]['tag'] = $tag;
  2073. $ifdEntries[$entryCount]['type'] = $type;
  2074. $ifdEntries[$entryCount]['count'] = $count;
  2075. $ifdEntries[$entryCount]['value'] = $value;
  2076. $entryCount++;
  2077. }
  2078. }
  2079. return $ifdEntries;
  2080. }
  2081. /*************************************************************/
  2082. function _handleMarkerParsingException($e) {
  2083. \dokuwiki\ErrorHandler::logException($e, $this->_fileName);
  2084. }
  2085. /*************************************************************/
  2086. function _isMarkerDisabled($name) {
  2087. if (!isset($this->_info)) return false;
  2088. return isset($this->_info[$name]) && $this->_info[$name] === false;
  2089. }
  2090. /*************************************************************/
  2091. function _parseMarkerAdobe() {
  2092. if (!isset($this->_markers)) {
  2093. $this->_readJPEG();
  2094. }
  2095. if ($this->_markers == null || $this->_isMarkerDisabled('adobe')) {
  2096. return false;
  2097. }
  2098. try {
  2099. $data = null;
  2100. $count = count($this->_markers);
  2101. for ($i = 0; $i < $count; $i++) {
  2102. if ($this->_markers[$i]['marker'] == 0xED) {
  2103. $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 14);
  2104. if ($signature == "Photoshop 3.0\0") {
  2105. $data =& $this->_markers[$i]['data'];
  2106. break;
  2107. }
  2108. }
  2109. }
  2110. if ($data == null) {
  2111. $this->_info['adobe'] = false;
  2112. $this->_info['iptc'] = false;
  2113. return false;
  2114. }
  2115. $pos = 14;
  2116. $this->_info['adobe'] = array();
  2117. $this->_info['adobe']['raw'] = array();
  2118. $this->_info['iptc'] = array();
  2119. $datasize = strlen($data);
  2120. while ($pos < $datasize) {
  2121. $signature = $this->_getFixedString($data, $pos, 4);
  2122. if ($signature != '8BIM')
  2123. return false;
  2124. $pos += 4;
  2125. $type = $this->_getShort($data, $pos);
  2126. $pos += 2;
  2127. $strlen = $this->_getByte($data, $pos);
  2128. $pos += 1;
  2129. $header = '';
  2130. for ($i = 0; $i < $strlen; $i++) {
  2131. $header .= $data[$pos + $i];
  2132. }
  2133. $pos += $strlen + 1 - ($strlen % 2); // The string is padded to even length, counting the length byte itself
  2134. $length = $this->_getLong($data, $pos);
  2135. $pos += 4;
  2136. $basePos = $pos;
  2137. switch ($type) {
  2138. case 0x0404: // Caption (IPTC Data)
  2139. $pos = $this->_readIPTC($data, $pos);
  2140. if ($pos == false)
  2141. return false;
  2142. break;
  2143. case 0x040A: // CopyrightFlag
  2144. $this->_info['adobe']['CopyrightFlag'] = $this->_getByte($data, $pos);
  2145. $pos += $length;
  2146. break;
  2147. case 0x040B: // ImageURL
  2148. $this->_info['adobe']['ImageURL'] = $this->_getFixedString($data, $pos, $length);
  2149. $pos += $length;
  2150. break;
  2151. case 0x040C: // Thumbnail
  2152. $aux = $this->_getLong($data, $pos);
  2153. $pos += 4;
  2154. if ($aux == 1) {
  2155. $this->_info['adobe']['ThumbnailWidth'] = $this->_getLong($data, $pos);
  2156. $pos += 4;
  2157. $this->_info['adobe']['ThumbnailHeight'] = $this->_getLong($data, $pos);
  2158. $pos += 4;
  2159. $pos += 16; // Skip some data
  2160. $this->_info['adobe']['ThumbnailData'] = $this->_getFixedString($data, $pos, $length - 28);
  2161. $pos += $length - 28;
  2162. }
  2163. break;
  2164. default:
  2165. break;
  2166. }
  2167. // We save all blocks, even those we recognized
  2168. $label = sprintf('8BIM_0x%04x', $type);
  2169. $this->_info['adobe']['raw'][$label] = array();
  2170. $this->_info['adobe']['raw'][$label]['type'] = $type;
  2171. $this->_info['adobe']['raw'][$label]['header'] = $header;
  2172. $this->_info['adobe']['raw'][$label]['data'] =& $this->_getFixedString($data, $basePos, $length);
  2173. $pos = $basePos + $length + ($length % 2); // Even padding
  2174. }
  2175. } catch(Exception $e) {
  2176. $this->_handleMarkerParsingException($e);
  2177. $this->_info['adobe'] = false;
  2178. $this->_info['iptc'] = false;
  2179. return false;
  2180. }
  2181. }
  2182. /*************************************************************/
  2183. function _readIPTC(&$data, $pos = 0) {
  2184. $totalLength = strlen($data);
  2185. $IPTCTags = $this->_iptcTagNames();
  2186. while ($pos < ($totalLength - 5)) {
  2187. $signature = $this->_getShort($data, $pos);
  2188. if ($signature != 0x1C02)
  2189. return $pos;
  2190. $pos += 2;
  2191. $type = $this->_getByte($data, $pos);
  2192. $pos += 1;
  2193. $length = $this->_getShort($data, $pos);
  2194. $pos += 2;
  2195. $basePos = $pos;
  2196. $label = '';
  2197. if (isset($IPTCTags[$type])) {
  2198. $label = $IPTCTags[$type];
  2199. } else {
  2200. $label = sprintf('IPTC_0x%02x', $type);
  2201. }
  2202. if ($label != '') {
  2203. if (isset($this->_info['iptc'][$label])) {
  2204. if (!is_array($this->_info['iptc'][$label])) {
  2205. $aux = array();
  2206. $aux[0] = $this->_info['iptc'][$label];
  2207. $this->_info['iptc'][$label] = $aux;
  2208. }
  2209. $this->_info['iptc'][$label][ count($this->_info['iptc'][$label]) ] = $this->_getFixedString($data, $pos, $length);
  2210. } else {
  2211. $this->_info['iptc'][$label] = $this->_getFixedString($data, $pos, $length);
  2212. }
  2213. }
  2214. $pos = $basePos + $length; // No padding
  2215. }
  2216. return $pos;
  2217. }
  2218. /*************************************************************/
  2219. function & _createMarkerAdobe() {
  2220. if (isset($this->_info['iptc'])) {
  2221. if (!isset($this->_info['adobe'])) {
  2222. $this->_info['adobe'] = array();
  2223. }
  2224. if (!isset($this->_info['adobe']['raw'])) {
  2225. $this->_info['adobe']['raw'] = array();
  2226. }
  2227. if (!isset($this->_info['adobe']['raw']['8BIM_0x0404'])) {
  2228. $this->_info['adobe']['raw']['8BIM_0x0404'] = array();
  2229. }
  2230. $this->_info['adobe']['raw']['8BIM_0x0404']['type'] = 0x0404;
  2231. $this->_info['adobe']['raw']['8BIM_0x0404']['header'] = "Caption";
  2232. $this->_info['adobe']['raw']['8BIM_0x0404']['data'] =& $this->_writeIPTC();
  2233. }
  2234. if (isset($this->_info['adobe']['raw']) && (count($this->_info['adobe']['raw']) > 0)) {
  2235. $data = "Photoshop 3.0\0";
  2236. $pos = 14;
  2237. reset($this->_info['adobe']['raw']);
  2238. foreach ($this->_info['adobe']['raw'] as $value){
  2239. $pos = $this->_write8BIM(
  2240. $data,
  2241. $pos,
  2242. $value['type'],
  2243. $value['header'],
  2244. $value['data'] );
  2245. }
  2246. }
  2247. return $data;
  2248. }
  2249. /*************************************************************/
  2250. /**
  2251. * @param mixed $data
  2252. * @param integer $pos
  2253. *
  2254. * @param string $type
  2255. * @param string $header
  2256. * @param mixed $value
  2257. *
  2258. * @return int|mixed
  2259. */
  2260. function _write8BIM(&$data, $pos, $type, $header, &$value) {
  2261. $signature = "8BIM";
  2262. $pos = $this->_putString($data, $pos, $signature);
  2263. $pos = $this->_putShort($data, $pos, $type);
  2264. $len = strlen($header);
  2265. $pos = $this->_putByte($data, $pos, $len);
  2266. $pos = $this->_putString($data, $pos, $header);
  2267. if (($len % 2) == 0) { // Even padding, including the length byte
  2268. $pos = $this->_putByte($data, $pos, 0);
  2269. }
  2270. $len = strlen($value);
  2271. $pos = $this->_putLong($data, $pos, $len);
  2272. $pos = $this->_putString($data, $pos, $value);
  2273. if (($len % 2) != 0) { // Even padding
  2274. $pos = $this->_putByte($data, $pos, 0);
  2275. }
  2276. return $pos;
  2277. }
  2278. /*************************************************************/
  2279. function & _writeIPTC() {
  2280. $data = " ";
  2281. $pos = 0;
  2282. $IPTCNames =& $this->_iptcNameTags();
  2283. foreach($this->_info['iptc'] as $label => $value) {
  2284. $value =& $this->_info['iptc'][$label];
  2285. $type = -1;
  2286. if (isset($IPTCNames[$label])) {
  2287. $type = $IPTCNames[$label];
  2288. }
  2289. elseif (substr($label, 0, 7) == "IPTC_0x") {
  2290. $type = hexdec(substr($label, 7, 2));
  2291. }
  2292. if ($type != -1) {
  2293. if (is_array($value)) {
  2294. $vcnt = count($value);
  2295. for ($i = 0; $i < $vcnt; $i++) {
  2296. $pos = $this->_writeIPTCEntry($data, $pos, $type, $value[$i]);
  2297. }
  2298. }
  2299. else {
  2300. $pos = $this->_writeIPTCEntry($data, $pos, $type, $value);
  2301. }
  2302. }
  2303. }
  2304. return $data;
  2305. }
  2306. /*************************************************************/
  2307. /**
  2308. * @param mixed $data
  2309. * @param integer $pos
  2310. *
  2311. * @param string $type
  2312. * @param mixed $value
  2313. *
  2314. * @return int|mixed
  2315. */
  2316. function _writeIPTCEntry(&$data, $pos, $type, &$value) {
  2317. $pos = $this->_putShort($data, $pos, 0x1C02);
  2318. $pos = $this->_putByte($data, $pos, $type);
  2319. $pos = $this->_putShort($data, $pos, strlen($value));
  2320. $pos = $this->_putString($data, $pos, $value);
  2321. return $pos;
  2322. }
  2323. /*************************************************************/
  2324. function _exifTagNames($mode) {
  2325. $tags = array();
  2326. if ($mode == 'ifd0') {
  2327. $tags[0x010E] = 'ImageDescription';
  2328. $tags[0x010F] = 'Make';
  2329. $tags[0x0110] = 'Model';
  2330. $tags[0x0112] = 'Orientation';
  2331. $tags[0x011A] = 'XResolution';
  2332. $tags[0x011B] = 'YResolution';
  2333. $tags[0x0128] = 'ResolutionUnit';
  2334. $tags[0x0131] = 'Software';
  2335. $tags[0x0132] = 'DateTime';
  2336. $tags[0x013B] = 'Artist';
  2337. $tags[0x013E] = 'WhitePoint';
  2338. $tags[0x013F] = 'PrimaryChromaticities';
  2339. $tags[0x0211] = 'YCbCrCoefficients';
  2340. $tags[0x0212] = 'YCbCrSubSampling';
  2341. $tags[0x0213] = 'YCbCrPositioning';
  2342. $tags[0x0214] = 'ReferenceBlackWhite';
  2343. $tags[0x8298] = 'Copyright';
  2344. $tags[0x8769] = 'ExifIFDOffset';
  2345. $tags[0x8825] = 'GPSIFDOffset';
  2346. }
  2347. if ($mode == 'ifd1') {
  2348. $tags[0x00FE] = 'TIFFNewSubfileType';
  2349. $tags[0x00FF] = 'TIFFSubfileType';
  2350. $tags[0x0100] = 'TIFFImageWidth';
  2351. $tags[0x0101] = 'TIFFImageHeight';
  2352. $tags[0x0102] = 'TIFFBitsPerSample';
  2353. $tags[0x0103] = 'TIFFCompression';
  2354. $tags[0x0106] = 'TIFFPhotometricInterpretation';
  2355. $tags[0x0107] = 'TIFFThreshholding';
  2356. $tags[0x0108] = 'TIFFCellWidth';
  2357. $tags[0x0109] = 'TIFFCellLength';
  2358. $tags[0x010A] = 'TIFFFillOrder';
  2359. $tags[0x010E] = 'TIFFImageDescription';
  2360. $tags[0x010F] = 'TIFFMake';
  2361. $tags[0x0110] = 'TIFFModel';
  2362. $tags[0x0111] = 'TIFFStripOffsets';
  2363. $tags[0x0112] = 'TIFFOrientation';
  2364. $tags[0x0115] = 'TIFFSamplesPerPixel';
  2365. $tags[0x0116] = 'TIFFRowsPerStrip';
  2366. $tags[0x0117] = 'TIFFStripByteCounts';
  2367. $tags[0x0118] = 'TIFFMinSampleValue';
  2368. $tags[0x0119] = 'TIFFMaxSampleValue';
  2369. $tags[0x011A] = 'TIFFXResolution';
  2370. $tags[0x011B] = 'TIFFYResolution';
  2371. $tags[0x011C] = 'TIFFPlanarConfiguration';
  2372. $tags[0x0122] = 'TIFFGrayResponseUnit';
  2373. $tags[0x0123] = 'TIFFGrayResponseCurve';
  2374. $tags[0x0128] = 'TIFFResolutionUnit';
  2375. $tags[0x0131] = 'TIFFSoftware';
  2376. $tags[0x0132] = 'TIFFDateTime';
  2377. $tags[0x013B] = 'TIFFArtist';
  2378. $tags[0x013C] = 'TIFFHostComputer';
  2379. $tags[0x0140] = 'TIFFColorMap';
  2380. $tags[0x0152] = 'TIFFExtraSamples';
  2381. $tags[0x0201] = 'TIFFJFIFOffset';
  2382. $tags[0x0202] = 'TIFFJFIFLength';
  2383. $tags[0x0211] = 'TIFFYCbCrCoefficients';
  2384. $tags[0x0212] = 'TIFFYCbCrSubSampling';
  2385. $tags[0x0213] = 'TIFFYCbCrPositioning';
  2386. $tags[0x0214] = 'TIFFReferenceBlackWhite';
  2387. $tags[0x8298] = 'TIFFCopyright';
  2388. $tags[0x9286] = 'TIFFUserComment';
  2389. } elseif ($mode == 'exif') {
  2390. $tags[0x829A] = 'ExposureTime';
  2391. $tags[0x829D] = 'FNumber';
  2392. $tags[0x8822] = 'ExposureProgram';
  2393. $tags[0x8824] = 'SpectralSensitivity';
  2394. $tags[0x8827] = 'ISOSpeedRatings';
  2395. $tags[0x8828] = 'OECF';
  2396. $tags[0x9000] = 'EXIFVersion';
  2397. $tags[0x9003] = 'DateTimeOriginal';
  2398. $tags[0x9004] = 'DateTimeDigitized';
  2399. $tags[0x9101] = 'ComponentsConfiguration';
  2400. $tags[0x9102] = 'CompressedBitsPerPixel';
  2401. $tags[0x9201] = 'ShutterSpeedValue';
  2402. $tags[0x9202] = 'ApertureValue';
  2403. $tags[0x9203] = 'BrightnessValue';
  2404. $tags[0x9204] = 'ExposureBiasValue';
  2405. $tags[0x9205] = 'MaxApertureValue';
  2406. $tags[0x9206] = 'SubjectDistance';
  2407. $tags[0x9207] = 'MeteringMode';
  2408. $tags[0x9208] = 'LightSource';
  2409. $tags[0x9209] = 'Flash';
  2410. $tags[0x920A] = 'FocalLength';
  2411. $tags[0x927C] = 'MakerNote';
  2412. $tags[0x9286] = 'UserComment';
  2413. $tags[0x9290] = 'SubSecTime';
  2414. $tags[0x9291] = 'SubSecTimeOriginal';
  2415. $tags[0x9292] = 'SubSecTimeDigitized';
  2416. $tags[0xA000] = 'FlashPixVersion';
  2417. $tags[0xA001] = 'ColorSpace';
  2418. $tags[0xA002] = 'PixelXDimension';
  2419. $tags[0xA003] = 'PixelYDimension';
  2420. $tags[0xA004] = 'RelatedSoundFile';
  2421. $tags[0xA005] = 'InteropIFDOffset';
  2422. $tags[0xA20B] = 'FlashEnergy';
  2423. $tags[0xA20C] = 'SpatialFrequencyResponse';
  2424. $tags[0xA20E] = 'FocalPlaneXResolution';
  2425. $tags[0xA20F] = 'FocalPlaneYResolution';
  2426. $tags[0xA210] = 'FocalPlaneResolutionUnit';
  2427. $tags[0xA214] = 'SubjectLocation';
  2428. $tags[0xA215] = 'ExposureIndex';
  2429. $tags[0xA217] = 'SensingMethod';
  2430. $tags[0xA300] = 'FileSource';
  2431. $tags[0xA301] = 'SceneType';
  2432. $tags[0xA302] = 'CFAPattern';
  2433. } elseif ($mode == 'interop') {
  2434. $tags[0x0001] = 'InteroperabilityIndex';
  2435. $tags[0x0002] = 'InteroperabilityVersion';
  2436. $tags[0x1000] = 'RelatedImageFileFormat';
  2437. $tags[0x1001] = 'RelatedImageWidth';
  2438. $tags[0x1002] = 'RelatedImageLength';
  2439. } elseif ($mode == 'gps') {
  2440. $tags[0x0000] = 'GPSVersionID';
  2441. $tags[0x0001] = 'GPSLatitudeRef';
  2442. $tags[0x0002] = 'GPSLatitude';
  2443. $tags[0x0003] = 'GPSLongitudeRef';
  2444. $tags[0x0004] = 'GPSLongitude';
  2445. $tags[0x0005] = 'GPSAltitudeRef';
  2446. $tags[0x0006] = 'GPSAltitude';
  2447. $tags[0x0007] = 'GPSTimeStamp';
  2448. $tags[0x0008] = 'GPSSatellites';
  2449. $tags[0x0009] = 'GPSStatus';
  2450. $tags[0x000A] = 'GPSMeasureMode';
  2451. $tags[0x000B] = 'GPSDOP';
  2452. $tags[0x000C] = 'GPSSpeedRef';
  2453. $tags[0x000D] = 'GPSSpeed';
  2454. $tags[0x000E] = 'GPSTrackRef';
  2455. $tags[0x000F] = 'GPSTrack';
  2456. $tags[0x0010] = 'GPSImgDirectionRef';
  2457. $tags[0x0011] = 'GPSImgDirection';
  2458. $tags[0x0012] = 'GPSMapDatum';
  2459. $tags[0x0013] = 'GPSDestLatitudeRef';
  2460. $tags[0x0014] = 'GPSDestLatitude';
  2461. $tags[0x0015] = 'GPSDestLongitudeRef';
  2462. $tags[0x0016] = 'GPSDestLongitude';
  2463. $tags[0x0017] = 'GPSDestBearingRef';
  2464. $tags[0x0018] = 'GPSDestBearing';
  2465. $tags[0x0019] = 'GPSDestDistanceRef';
  2466. $tags[0x001A] = 'GPSDestDistance';
  2467. }
  2468. return $tags;
  2469. }
  2470. /*************************************************************/
  2471. function _exifTagTypes($mode) {
  2472. $tags = array();
  2473. if ($mode == 'ifd0') {
  2474. $tags[0x010E] = array(2, 0); // ImageDescription -> ASCII, Any
  2475. $tags[0x010F] = array(2, 0); // Make -> ASCII, Any
  2476. $tags[0x0110] = array(2, 0); // Model -> ASCII, Any
  2477. $tags[0x0112] = array(3, 1); // Orientation -> SHORT, 1
  2478. $tags[0x011A] = array(5, 1); // XResolution -> RATIONAL, 1
  2479. $tags[0x011B] = array(5, 1); // YResolution -> RATIONAL, 1
  2480. $tags[0x0128] = array(3, 1); // ResolutionUnit -> SHORT
  2481. $tags[0x0131] = array(2, 0); // Software -> ASCII, Any
  2482. $tags[0x0132] = array(2, 20); // DateTime -> ASCII, 20
  2483. $tags[0x013B] = array(2, 0); // Artist -> ASCII, Any
  2484. $tags[0x013E] = array(5, 2); // WhitePoint -> RATIONAL, 2
  2485. $tags[0x013F] = array(5, 6); // PrimaryChromaticities -> RATIONAL, 6
  2486. $tags[0x0211] = array(5, 3); // YCbCrCoefficients -> RATIONAL, 3
  2487. $tags[0x0212] = array(3, 2); // YCbCrSubSampling -> SHORT, 2
  2488. $tags[0x0213] = array(3, 1); // YCbCrPositioning -> SHORT, 1
  2489. $tags[0x0214] = array(5, 6); // ReferenceBlackWhite -> RATIONAL, 6
  2490. $tags[0x8298] = array(2, 0); // Copyright -> ASCII, Any
  2491. $tags[0x8769] = array(4, 1); // ExifIFDOffset -> LONG, 1
  2492. $tags[0x8825] = array(4, 1); // GPSIFDOffset -> LONG, 1
  2493. }
  2494. if ($mode == 'ifd1') {
  2495. $tags[0x00FE] = array(4, 1); // TIFFNewSubfileType -> LONG, 1
  2496. $tags[0x00FF] = array(3, 1); // TIFFSubfileType -> SHORT, 1
  2497. $tags[0x0100] = array(4, 1); // TIFFImageWidth -> LONG (or SHORT), 1
  2498. $tags[0x0101] = array(4, 1); // TIFFImageHeight -> LONG (or SHORT), 1
  2499. $tags[0x0102] = array(3, 3); // TIFFBitsPerSample -> SHORT, 3
  2500. $tags[0x0103] = array(3, 1); // TIFFCompression -> SHORT, 1
  2501. $tags[0x0106] = array(3, 1); // TIFFPhotometricInterpretation -> SHORT, 1
  2502. $tags[0x0107] = array(3, 1); // TIFFThreshholding -> SHORT, 1
  2503. $tags[0x0108] = array(3, 1); // TIFFCellWidth -> SHORT, 1
  2504. $tags[0x0109] = array(3, 1); // TIFFCellLength -> SHORT, 1
  2505. $tags[0x010A] = array(3, 1); // TIFFFillOrder -> SHORT, 1
  2506. $tags[0x010E] = array(2, 0); // TIFFImageDescription -> ASCII, Any
  2507. $tags[0x010F] = array(2, 0); // TIFFMake -> ASCII, Any
  2508. $tags[0x0110] = array(2, 0); // TIFFModel -> ASCII, Any
  2509. $tags[0x0111] = array(4, 0); // TIFFStripOffsets -> LONG (or SHORT), Any (one per strip)
  2510. $tags[0x0112] = array(3, 1); // TIFFOrientation -> SHORT, 1
  2511. $tags[0x0115] = array(3, 1); // TIFFSamplesPerPixel -> SHORT, 1
  2512. $tags[0x0116] = array(4, 1); // TIFFRowsPerStrip -> LONG (or SHORT), 1
  2513. $tags[0x0117] = array(4, 0); // TIFFStripByteCounts -> LONG (or SHORT), Any (one per strip)
  2514. $tags[0x0118] = array(3, 0); // TIFFMinSampleValue -> SHORT, Any (SamplesPerPixel)
  2515. $tags[0x0119] = array(3, 0); // TIFFMaxSampleValue -> SHORT, Any (SamplesPerPixel)
  2516. $tags[0x011A] = array(5, 1); // TIFFXResolution -> RATIONAL, 1
  2517. $tags[0x011B] = array(5, 1); // TIFFYResolution -> RATIONAL, 1
  2518. $tags[0x011C] = array(3, 1); // TIFFPlanarConfiguration -> SHORT, 1
  2519. $tags[0x0122] = array(3, 1); // TIFFGrayResponseUnit -> SHORT, 1
  2520. $tags[0x0123] = array(3, 0); // TIFFGrayResponseCurve -> SHORT, Any (2^BitsPerSample)
  2521. $tags[0x0128] = array(3, 1); // TIFFResolutionUnit -> SHORT, 1
  2522. $tags[0x0131] = array(2, 0); // TIFFSoftware -> ASCII, Any
  2523. $tags[0x0132] = array(2, 20); // TIFFDateTime -> ASCII, 20
  2524. $tags[0x013B] = array(2, 0); // TIFFArtist -> ASCII, Any
  2525. $tags[0x013C] = array(2, 0); // TIFFHostComputer -> ASCII, Any
  2526. $tags[0x0140] = array(3, 0); // TIFFColorMap -> SHORT, Any (3 * 2^BitsPerSample)
  2527. $tags[0x0152] = array(3, 0); // TIFFExtraSamples -> SHORT, Any (SamplesPerPixel - 3)
  2528. $tags[0x0201] = array(4, 1); // TIFFJFIFOffset -> LONG, 1
  2529. $tags[0x0202] = array(4, 1); // TIFFJFIFLength -> LONG, 1
  2530. $tags[0x0211] = array(5, 3); // TIFFYCbCrCoefficients -> RATIONAL, 3
  2531. $tags[0x0212] = array(3, 2); // TIFFYCbCrSubSampling -> SHORT, 2
  2532. $tags[0x0213] = array(3, 1); // TIFFYCbCrPositioning -> SHORT, 1
  2533. $tags[0x0214] = array(5, 6); // TIFFReferenceBlackWhite -> RATIONAL, 6
  2534. $tags[0x8298] = array(2, 0); // TIFFCopyright -> ASCII, Any
  2535. $tags[0x9286] = array(2, 0); // TIFFUserComment -> ASCII, Any
  2536. } elseif ($mode == 'exif') {
  2537. $tags[0x829A] = array(5, 1); // ExposureTime -> RATIONAL, 1
  2538. $tags[0x829D] = array(5, 1); // FNumber -> RATIONAL, 1
  2539. $tags[0x8822] = array(3, 1); // ExposureProgram -> SHORT, 1
  2540. $tags[0x8824] = array(2, 0); // SpectralSensitivity -> ASCII, Any
  2541. $tags[0x8827] = array(3, 0); // ISOSpeedRatings -> SHORT, Any
  2542. $tags[0x8828] = array(7, 0); // OECF -> UNDEFINED, Any
  2543. $tags[0x9000] = array(7, 4); // EXIFVersion -> UNDEFINED, 4
  2544. $tags[0x9003] = array(2, 20); // DateTimeOriginal -> ASCII, 20
  2545. $tags[0x9004] = array(2, 20); // DateTimeDigitized -> ASCII, 20
  2546. $tags[0x9101] = array(7, 4); // ComponentsConfiguration -> UNDEFINED, 4
  2547. $tags[0x9102] = array(5, 1); // CompressedBitsPerPixel -> RATIONAL, 1
  2548. $tags[0x9201] = array(10, 1); // ShutterSpeedValue -> SRATIONAL, 1
  2549. $tags[0x9202] = array(5, 1); // ApertureValue -> RATIONAL, 1
  2550. $tags[0x9203] = array(10, 1); // BrightnessValue -> SRATIONAL, 1
  2551. $tags[0x9204] = array(10, 1); // ExposureBiasValue -> SRATIONAL, 1
  2552. $tags[0x9205] = array(5, 1); // MaxApertureValue -> RATIONAL, 1
  2553. $tags[0x9206] = array(5, 1); // SubjectDistance -> RATIONAL, 1
  2554. $tags[0x9207] = array(3, 1); // MeteringMode -> SHORT, 1
  2555. $tags[0x9208] = array(3, 1); // LightSource -> SHORT, 1
  2556. $tags[0x9209] = array(3, 1); // Flash -> SHORT, 1
  2557. $tags[0x920A] = array(5, 1); // FocalLength -> RATIONAL, 1
  2558. $tags[0x927C] = array(7, 0); // MakerNote -> UNDEFINED, Any
  2559. $tags[0x9286] = array(7, 0); // UserComment -> UNDEFINED, Any
  2560. $tags[0x9290] = array(2, 0); // SubSecTime -> ASCII, Any
  2561. $tags[0x9291] = array(2, 0); // SubSecTimeOriginal -> ASCII, Any
  2562. $tags[0x9292] = array(2, 0); // SubSecTimeDigitized -> ASCII, Any
  2563. $tags[0xA000] = array(7, 4); // FlashPixVersion -> UNDEFINED, 4
  2564. $tags[0xA001] = array(3, 1); // ColorSpace -> SHORT, 1
  2565. $tags[0xA002] = array(4, 1); // PixelXDimension -> LONG (or SHORT), 1
  2566. $tags[0xA003] = array(4, 1); // PixelYDimension -> LONG (or SHORT), 1
  2567. $tags[0xA004] = array(2, 13); // RelatedSoundFile -> ASCII, 13
  2568. $tags[0xA005] = array(4, 1); // InteropIFDOffset -> LONG, 1
  2569. $tags[0xA20B] = array(5, 1); // FlashEnergy -> RATIONAL, 1
  2570. $tags[0xA20C] = array(7, 0); // SpatialFrequencyResponse -> UNDEFINED, Any
  2571. $tags[0xA20E] = array(5, 1); // FocalPlaneXResolution -> RATIONAL, 1
  2572. $tags[0xA20F] = array(5, 1); // FocalPlaneYResolution -> RATIONAL, 1
  2573. $tags[0xA210] = array(3, 1); // FocalPlaneResolutionUnit -> SHORT, 1
  2574. $tags[0xA214] = array(3, 2); // SubjectLocation -> SHORT, 2
  2575. $tags[0xA215] = array(5, 1); // ExposureIndex -> RATIONAL, 1
  2576. $tags[0xA217] = array(3, 1); // SensingMethod -> SHORT, 1
  2577. $tags[0xA300] = array(7, 1); // FileSource -> UNDEFINED, 1
  2578. $tags[0xA301] = array(7, 1); // SceneType -> UNDEFINED, 1
  2579. $tags[0xA302] = array(7, 0); // CFAPattern -> UNDEFINED, Any
  2580. } elseif ($mode == 'interop') {
  2581. $tags[0x0001] = array(2, 0); // InteroperabilityIndex -> ASCII, Any
  2582. $tags[0x0002] = array(7, 4); // InteroperabilityVersion -> UNKNOWN, 4
  2583. $tags[0x1000] = array(2, 0); // RelatedImageFileFormat -> ASCII, Any
  2584. $tags[0x1001] = array(4, 1); // RelatedImageWidth -> LONG (or SHORT), 1
  2585. $tags[0x1002] = array(4, 1); // RelatedImageLength -> LONG (or SHORT), 1
  2586. } elseif ($mode == 'gps') {
  2587. $tags[0x0000] = array(1, 4); // GPSVersionID -> BYTE, 4
  2588. $tags[0x0001] = array(2, 2); // GPSLatitudeRef -> ASCII, 2
  2589. $tags[0x0002] = array(5, 3); // GPSLatitude -> RATIONAL, 3
  2590. $tags[0x0003] = array(2, 2); // GPSLongitudeRef -> ASCII, 2
  2591. $tags[0x0004] = array(5, 3); // GPSLongitude -> RATIONAL, 3
  2592. $tags[0x0005] = array(2, 2); // GPSAltitudeRef -> ASCII, 2
  2593. $tags[0x0006] = array(5, 1); // GPSAltitude -> RATIONAL, 1
  2594. $tags[0x0007] = array(5, 3); // GPSTimeStamp -> RATIONAL, 3
  2595. $tags[0x0008] = array(2, 0); // GPSSatellites -> ASCII, Any
  2596. $tags[0x0009] = array(2, 2); // GPSStatus -> ASCII, 2
  2597. $tags[0x000A] = array(2, 2); // GPSMeasureMode -> ASCII, 2
  2598. $tags[0x000B] = array(5, 1); // GPSDOP -> RATIONAL, 1
  2599. $tags[0x000C] = array(2, 2); // GPSSpeedRef -> ASCII, 2
  2600. $tags[0x000D] = array(5, 1); // GPSSpeed -> RATIONAL, 1
  2601. $tags[0x000E] = array(2, 2); // GPSTrackRef -> ASCII, 2
  2602. $tags[0x000F] = array(5, 1); // GPSTrack -> RATIONAL, 1
  2603. $tags[0x0010] = array(2, 2); // GPSImgDirectionRef -> ASCII, 2
  2604. $tags[0x0011] = array(5, 1); // GPSImgDirection -> RATIONAL, 1
  2605. $tags[0x0012] = array(2, 0); // GPSMapDatum -> ASCII, Any
  2606. $tags[0x0013] = array(2, 2); // GPSDestLatitudeRef -> ASCII, 2
  2607. $tags[0x0014] = array(5, 3); // GPSDestLatitude -> RATIONAL, 3
  2608. $tags[0x0015] = array(2, 2); // GPSDestLongitudeRef -> ASCII, 2
  2609. $tags[0x0016] = array(5, 3); // GPSDestLongitude -> RATIONAL, 3
  2610. $tags[0x0017] = array(2, 2); // GPSDestBearingRef -> ASCII, 2
  2611. $tags[0x0018] = array(5, 1); // GPSDestBearing -> RATIONAL, 1
  2612. $tags[0x0019] = array(2, 2); // GPSDestDistanceRef -> ASCII, 2
  2613. $tags[0x001A] = array(5, 1); // GPSDestDistance -> RATIONAL, 1
  2614. }
  2615. return $tags;
  2616. }
  2617. /*************************************************************/
  2618. function _exifNameTags($mode) {
  2619. $tags = $this->_exifTagNames($mode);
  2620. return $this->_names2Tags($tags);
  2621. }
  2622. /*************************************************************/
  2623. function _iptcTagNames() {
  2624. $tags = array();
  2625. $tags[0x14] = 'SuplementalCategories';
  2626. $tags[0x19] = 'Keywords';
  2627. $tags[0x78] = 'Caption';
  2628. $tags[0x7A] = 'CaptionWriter';
  2629. $tags[0x69] = 'Headline';
  2630. $tags[0x28] = 'SpecialInstructions';
  2631. $tags[0x0F] = 'Category';
  2632. $tags[0x50] = 'Byline';
  2633. $tags[0x55] = 'BylineTitle';
  2634. $tags[0x6E] = 'Credit';
  2635. $tags[0x73] = 'Source';
  2636. $tags[0x74] = 'CopyrightNotice';
  2637. $tags[0x05] = 'ObjectName';
  2638. $tags[0x5A] = 'City';
  2639. $tags[0x5C] = 'Sublocation';
  2640. $tags[0x5F] = 'ProvinceState';
  2641. $tags[0x65] = 'CountryName';
  2642. $tags[0x67] = 'OriginalTransmissionReference';
  2643. $tags[0x37] = 'DateCreated';
  2644. $tags[0x0A] = 'CopyrightFlag';
  2645. return $tags;
  2646. }
  2647. /*************************************************************/
  2648. function & _iptcNameTags() {
  2649. $tags = $this->_iptcTagNames();
  2650. return $this->_names2Tags($tags);
  2651. }
  2652. /*************************************************************/
  2653. function _names2Tags($tags2Names) {
  2654. $names2Tags = array();
  2655. foreach($tags2Names as $tag => $name) {
  2656. $names2Tags[$name] = $tag;
  2657. }
  2658. return $names2Tags;
  2659. }
  2660. /*************************************************************/
  2661. /**
  2662. * @param $data
  2663. * @param integer $pos
  2664. *
  2665. * @return int
  2666. */
  2667. function _getByte(&$data, $pos) {
  2668. if (!isset($data[$pos])) {
  2669. throw new Exception("Requested byte at ".$pos.". Reading outside of file's boundaries.");
  2670. }
  2671. return ord($data[$pos]);
  2672. }
  2673. /*************************************************************/
  2674. /**
  2675. * @param mixed $data
  2676. * @param integer $pos
  2677. *
  2678. * @param mixed $val
  2679. *
  2680. * @return int
  2681. */
  2682. function _putByte(&$data, $pos, $val) {
  2683. $val = intval($val);
  2684. $data[$pos] = chr($val);
  2685. return $pos + 1;
  2686. }
  2687. /*************************************************************/
  2688. function _getShort(&$data, $pos, $bigEndian = true) {
  2689. if (!isset($data[$pos]) || !isset($data[$pos + 1])) {
  2690. throw new Exception("Requested short at ".$pos.". Reading outside of file's boundaries.");
  2691. }
  2692. if ($bigEndian) {
  2693. return (ord($data[$pos]) << 8)
  2694. + ord($data[$pos + 1]);
  2695. } else {
  2696. return ord($data[$pos])
  2697. + (ord($data[$pos + 1]) << 8);
  2698. }
  2699. }
  2700. /*************************************************************/
  2701. function _putShort(&$data, $pos = 0, $val = 0, $bigEndian = true) {
  2702. $val = intval($val);
  2703. if ($bigEndian) {
  2704. $data[$pos + 0] = chr(($val & 0x0000FF00) >> 8);
  2705. $data[$pos + 1] = chr(($val & 0x000000FF) >> 0);
  2706. } else {
  2707. $data[$pos + 0] = chr(($val & 0x00FF) >> 0);
  2708. $data[$pos + 1] = chr(($val & 0xFF00) >> 8);
  2709. }
  2710. return $pos + 2;
  2711. }
  2712. /*************************************************************/
  2713. /**
  2714. * @param mixed $data
  2715. * @param integer $pos
  2716. *
  2717. * @param bool $bigEndian
  2718. *
  2719. * @return int
  2720. */
  2721. function _getLong(&$data, $pos, $bigEndian = true) {
  2722. // Assume that if the start and end bytes are defined, the bytes inbetween are defined as well.
  2723. if (!isset($data[$pos]) || !isset($data[$pos + 3])){
  2724. throw new Exception("Requested long at ".$pos.". Reading outside of file's boundaries.");
  2725. }
  2726. if ($bigEndian) {
  2727. return (ord($data[$pos]) << 24)
  2728. + (ord($data[$pos + 1]) << 16)
  2729. + (ord($data[$pos + 2]) << 8)
  2730. + ord($data[$pos + 3]);
  2731. } else {
  2732. return ord($data[$pos])
  2733. + (ord($data[$pos + 1]) << 8)
  2734. + (ord($data[$pos + 2]) << 16)
  2735. + (ord($data[$pos + 3]) << 24);
  2736. }
  2737. }
  2738. /*************************************************************/
  2739. /**
  2740. * @param mixed $data
  2741. * @param integer $pos
  2742. *
  2743. * @param mixed $val
  2744. * @param bool $bigEndian
  2745. *
  2746. * @return int
  2747. */
  2748. function _putLong(&$data, $pos, $val, $bigEndian = true) {
  2749. $val = intval($val);
  2750. if ($bigEndian) {
  2751. $data[$pos + 0] = chr(($val & 0xFF000000) >> 24);
  2752. $data[$pos + 1] = chr(($val & 0x00FF0000) >> 16);
  2753. $data[$pos + 2] = chr(($val & 0x0000FF00) >> 8);
  2754. $data[$pos + 3] = chr(($val & 0x000000FF) >> 0);
  2755. } else {
  2756. $data[$pos + 0] = chr(($val & 0x000000FF) >> 0);
  2757. $data[$pos + 1] = chr(($val & 0x0000FF00) >> 8);
  2758. $data[$pos + 2] = chr(($val & 0x00FF0000) >> 16);
  2759. $data[$pos + 3] = chr(($val & 0xFF000000) >> 24);
  2760. }
  2761. return $pos + 4;
  2762. }
  2763. /*************************************************************/
  2764. function & _getNullString(&$data, $pos) {
  2765. $str = '';
  2766. $max = strlen($data);
  2767. while ($pos < $max) {
  2768. if (!isset($data[$pos])) {
  2769. throw new Exception("Requested null-terminated string at offset ".$pos.". File terminated before the null-byte.");
  2770. }
  2771. if (ord($data[$pos]) == 0) {
  2772. return $str;
  2773. } else {
  2774. $str .= $data[$pos];
  2775. }
  2776. $pos++;
  2777. }
  2778. return $str;
  2779. }
  2780. /*************************************************************/
  2781. function & _getFixedString(&$data, $pos, $length = -1) {
  2782. if ($length == -1) {
  2783. $length = strlen($data) - $pos;
  2784. }
  2785. $rv = substr($data, $pos, $length);
  2786. if (strlen($rv) != $length) {
  2787. throw new ErrorException(sprintf(
  2788. "JPEGMeta failed parsing image metadata of %s. Got %d instead of %d bytes at offset %d.",
  2789. $this->_fileName, strlen($rv), $length, $pos
  2790. ), 0, E_WARNING);
  2791. }
  2792. return $rv;
  2793. }
  2794. /*************************************************************/
  2795. function _putString(&$data, $pos, &$str) {
  2796. $len = strlen($str);
  2797. for ($i = 0; $i < $len; $i++) {
  2798. $data[$pos + $i] = $str[$i];
  2799. }
  2800. return $pos + $len;
  2801. }
  2802. /*************************************************************/
  2803. function _hexDump(&$data, $start = 0, $length = -1) {
  2804. if (($length == -1) || (($length + $start) > strlen($data))) {
  2805. $end = strlen($data);
  2806. } else {
  2807. $end = $start + $length;
  2808. }
  2809. $ascii = '';
  2810. $count = 0;
  2811. echo "<tt>\n";
  2812. while ($start < $end) {
  2813. if (($count % 16) == 0) {
  2814. echo sprintf('%04d', $count) . ': ';
  2815. }
  2816. $c = ord($data[$start]);
  2817. $count++;
  2818. $start++;
  2819. $aux = dechex($c);
  2820. if (strlen($aux) == 1)
  2821. echo '0';
  2822. echo $aux . ' ';
  2823. if ($c == 60)
  2824. $ascii .= '&lt;';
  2825. elseif ($c == 62)
  2826. $ascii .= '&gt;';
  2827. elseif ($c == 32)
  2828. $ascii .= '&#160;';
  2829. elseif ($c > 32)
  2830. $ascii .= chr($c);
  2831. else
  2832. $ascii .= '.';
  2833. if (($count % 4) == 0) {
  2834. echo ' - ';
  2835. }
  2836. if (($count % 16) == 0) {
  2837. echo ': ' . $ascii . "<br>\n";
  2838. $ascii = '';
  2839. }
  2840. }
  2841. if ($ascii != '') {
  2842. while (($count % 16) != 0) {
  2843. echo '-- ';
  2844. $count++;
  2845. if (($count % 4) == 0) {
  2846. echo ' - ';
  2847. }
  2848. }
  2849. echo ': ' . $ascii . "<br>\n";
  2850. }
  2851. echo "</tt>\n";
  2852. }
  2853. /*****************************************************************/
  2854. }
  2855. /* vim: set expandtab tabstop=4 shiftwidth=4: */