@dangayle

PHP Code: Cite your photo sources!

After working on a site that uses images from a variety of different sources, I felt that it was appropriate to cite the sources. (I do come from a newspaper background, so…)

So, what was the best way to do it?

Since the image is being hotlinked from another source, I could do a string search of the domain, then display the appropriate link. Easy enough, right?

It’s not so easy, since you have to search for a variety of sources, then know the link to display. That’s where a PHP array comes in handy.

Here’s the example:

Code Example

<?php
//Creates link to image source for creative attribution
$photoSource = array(
        'wikimedia'=> '<a href="http://commons.wikimedia.org/" rel="nofollow external">Wikimedia Commons</a>',
        'flickr'=> '<a href="http://www.flickr.com" rel="nofollow external">Flickr</a>',
        'picasa'=> '<a href="http://picasaweb.google.com/" rel="nofollow external">Picasa</a>',
   );
foreach ($photoSource as $photoSource_origin => $photoSource_link) {
if (stristr($imagePath,$photoSource_origin)) { ?>
   <p class="image_source">Image courtesy of < ?php echo $photoSource_link; ?></p>
<?php break; }
}?>

Code Explained

You create your array of possible sources, the example here uses Wikimedia Commons, Flickr, and Picasa as examples.

The foreach ($photoSource as $photoSource_origin =&gt; $photoSource_link) loop associates each photo source with its corresponding link. The if (stristr($imagePath,$photoSource_origin)) searches the $imagePath variable, which is the variable that contains the link to the photo, for the terms listed as $photoSource, and if found, then display’s the found search term’s associated link, the $photoSource_link.

The break then stops the loop after a solution is found.

Cool solution huh?