iptcembed_002.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. --TEST--
  2. iptcembed() valid jpg stream
  3. --FILE--
  4. <?php
  5. /*
  6. # source code to generate base64 use behind as $base64_1x1_jpeg
  7. # we don't want to be gd library dependant for this test
  8. $file="1x1.jpg";
  9. $ret=imagejpeg(imagecreatetruecolor(1, 1), $file, 100);
  10. echo md5(file_get_contents($file)).PHP_EOL;
  11. echo base64_encode(file_get_contents($file)).PHP_EOL;
  12. unlink($file);
  13. */
  14. /*
  15. test description :
  16. 1) create local file 1x1 jpeg (without iptc) (use base64 content to create file)
  17. 2) generate iptcdata string with function iptc_make_tag describe behind
  18. 3) use iptcembed php function with our 1x1 jpeg file and our iptcdata string
  19. 4) write local file2 with iptcembed return content
  20. 5) various check on file2 to verify that's a valid jpeg file with our tags
  21. */
  22. #iptc_make_tag function from http://php.net/iptcembed
  23. function iptc_make_tag($rec, $data, $value)
  24. {
  25. $length = strlen($value);
  26. $retval = chr(0x1C) . chr($rec) . chr($data);
  27. if($length < 0x8000) { $retval .= chr($length >> 8) . chr($length & 0xFF); }
  28. else { $retval .= chr(0x80) . chr(0x04) . chr(($length >> 24) & 0xFF) . chr(($length >> 16) & 0xFF) . chr(($length >> 8) & 0xFF) . chr($length & 0xFF); }
  29. return $retval . $value;
  30. }
  31. $file="1x1.jpg";
  32. $file2="1x1_with_iptc_tags.jpg";
  33. $base64_1x1_jpeg="/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAQMBEQACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2gAMAwEAAhEDEQA/AP8AP/oA/9k=";
  34. #write file
  35. $fd=fopen($file,"wb");
  36. if ($fd) { fputs($fd,base64_decode($base64_1x1_jpeg)); fclose($fd); }
  37. else { echo "error can't write $file".PHP_EOL;exit(1); }
  38. #check file md5
  39. $md5=md5_file($file);
  40. if ($md5!="07dd8594450e8c18ab8a79d7cb4573c7") { echo "md5 error".PHP_EOL;exit(1); }
  41. #check jpeg properties
  42. list($width, $height, $type, $attr) = getimagesize($file,$info);
  43. if ($width!=1) { echo "width error".PHP_EOL;exit(1); }
  44. if ($height!=1) { echo "height error".PHP_EOL;exit(1); }
  45. if ($type!=2) { echo "type error".PHP_EOL;exit(1); }
  46. if (!isset($info["APP0"])) { echo "APP0 error".PHP_EOL;exit(1); }
  47. #our iptc tags
  48. $tags=array();
  49. $tags["2#105"]= "Tauren";
  50. $tags["2#120"]= "Tauren with Trunk";
  51. $tags["2#110"]= "Copyright 2004-2016, Blizzard";
  52. $tags["2#025"]= "Tauren, Chaman, Blizzard";
  53. $tags["2#090"]= "Thunder Bluffs";
  54. #feed iptc string for iptcembed
  55. $iptc='';
  56. foreach ($tags as $tag => $string) { $rec=$tag[0]; $tag = substr($tag, 2); $iptc .= iptc_make_tag($rec, $tag, $string); }
  57. #check iptc string md5
  58. if (md5(base64_encode($iptc))!="7056c4b3060f92a4f9e5b7d0caa61859") { echo "iptc md5 error".PHP_EOL;exit(1); }
  59. #use iptcembed to get jpeg stream content with iptc tags
  60. $content = iptcembed($iptc, $file,0);
  61. #write new image with iptc tags
  62. if ($content === false) {echo "iptcembed error".PHP_EOL;exit(1); }
  63. $fd=fopen($file2,"wb");
  64. if ($fd) { fputs($fd,$content); fclose($fd); }
  65. else { echo "error can't write $file2".PHP_EOL;exit(1); }
  66. #check jpeg properties for new image with iptc tags
  67. echo "new generated image with itpc tags : $file2".PHP_EOL;
  68. $ret = getimagesize($file2,$info);
  69. if ($ret===false) { echo "getimagesize error".PHP_EOL;exit(1); }
  70. list($width, $height, $type, $attr) = $ret;
  71. if ($width!=1) { echo "width error".PHP_EOL;exit(1); }
  72. if ($height!=1) { echo "height error".PHP_EOL;exit(1); }
  73. if ($type!=2) { echo "type error".PHP_EOL;exit(1); }
  74. if (!isset($info["APP0"])) { echo "APP0 error".PHP_EOL;exit(1); }
  75. if (!isset($info["APP13"])) { echo "APP13 error".PHP_EOL;exit(1); }
  76. $error=0;
  77. $iptc_data_from_created_image = iptcparse($info['APP13']);
  78. foreach ($tags as $tag => $string) {
  79. #check if tag exists
  80. if (!isset($iptc_data_from_created_image[$tag])) {
  81. echo "error iptc tag $tag not found".PHP_EOL;
  82. $error++;
  83. } else {
  84. #check value
  85. if ($iptc_data_from_created_image[$tag][0]!=$string) {
  86. echo "error tag $tag : bad value ($string != ".$iptc_data_from_created_image[$tag][0].")".PHP_EOL;
  87. $error++;
  88. }
  89. }
  90. }
  91. #clean before exit
  92. @unlink($file);
  93. @unlink($file2);
  94. if ($error==0) { echo "OK".PHP_EOL;exit(0);}
  95. echo "something wrong: $error errors".PHP_EOL;
  96. ?>
  97. --EXPECT--
  98. new generated image with itpc tags : 1x1_with_iptc_tags.jpg
  99. OK