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