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

//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?

PHP Programming

I know this makes me a real nerd, but I’m starting to almost understand a little about web programming using PHP. (I know, sad id’nit?)

I started out learning but customizing my WordPress blog, this blog, which of course is written in PHP. As I got into it, I started learning how bits and pieces started coming together. (Forget PHP, creating this blog is a large part of where I learned HTML to begin with!)

Now with some of the stuff I do at work, I use basic PHP a lot more. Stuff like if.. else.. conditional statements, basic loops, server side includes, things of that nature. If you have no idea what any of that means, don’t worry, even I didn’t a while back. I’m still learning how arrays work, how sql queries work, cookies, etc. More intermediary concepts.

I’m reading a lot, and learning a lot about how things work on the web. With that comes some obligations, I feel, to help others. That is why I’m going to start writing some basic PHP tutorials to show how the basics work. Keep an eye out for the first tutorials soon!

In the meantime…

Until I get started writing tutorials about PHP, I highly recommend that you get the book PHP Solutions from Friends of Ed. Great book!