JamesWood Posted August 18, 2021 Share Posted August 18, 2021 Hi guys I'm super new to PHP, so bear with me! But I have a super simple code here that will display all images in a folder, and print the 'header' IPTC metadata and then print the filename. My query is this -- it currently repeats the last available IPTC header, even if the next image doesn't have any metadata stored in it. So how can I tell the code to simply not print anything at all if the IPTC data isn't stored in the file? Thanks a lot James (here's the code) <?php $files = glob("*.*"); for ($i=0; $i<count($files); $i++) { $image = $files[$i]; echo '<img src="'.$image .'" title="'.$image .'" alt="'.$image .'" width="5%"/>'."<br />"; $picinfo = array(); getimagesize($image, $picinfo); if(isset($picinfo['APP13'])) { $iptc = iptcparse($picinfo["APP13"]); } if (is_array($iptc)) { $description = $iptc['2#105'][0]; /* IPTC 'Header' metadata */ } print_r($description); print "<br />"; print $image ."<br /><br />"; /* Print filename */ } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2021 Share Posted August 18, 2021 You already have something that will print information if it's available. What's more, it makes sense: if you have $iptc data then print what's in it. Logical. Obviously, that then means that if you don't have $iptc data then it won't print. Also logical. So then, if you don't want it to print anything, you need to make sure that "you don't have $iptc data" is true. Thoughts on how you can make that happen? Quote Link to comment Share on other sites More sharing options...
JamesWood Posted August 19, 2021 Author Share Posted August 19, 2021 12 hours ago, requinix said: You already have something that will print information if it's available. What's more, it makes sense: if you have $iptc data then print what's in it. Logical. Obviously, that then means that if you don't have $iptc data then it won't print. Also logical. So then, if you don't want it to print anything, you need to make sure that "you don't have $iptc data" is true. Thoughts on how you can make that happen? Ahh I figured it out with an 'if else' statement Just wasn't sure how to phrase it correctly, but it's working now. <?php $files = glob("*.*"); for ($i=0; $i<count($files); $i++) { $image = $files[$i]; echo '<img src="'.$image .'" title="'.$image .'" alt="'.$image .'" width="5%"/>'."<br />"; $picinfo = array(); getimagesize($image, $picinfo); if(isset($picinfo['APP13'])) { $iptc = iptcparse($picinfo["APP13"]); } if (empty($picinfo["APP13"])) { $description = ""; } else { $description = $iptc['2#105'][0]; /* IPTC 'Header' metadata */ print_r($description); print "<br />"; } print $image ."<br /><br />"; /* Print filename */ } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.