I just noted that it's been almost four years since I logged in to these forums! Well I'm back, and I could do with some help...
I wrote a script using Dan Coulter's PHP wrapper for the Flickr API, phpFlickr. It pulls several 'galleries' of images based on explicitly defined tags. The problem is, according to my script, it's taking over 17 seconds to execute. This is unacceptable and is almost certain to annoy people. I'm running PHP5 on Apache 1.3.33 on Linux with GoDaddy.
The purpose of the script is to allow me to show photos hosted on Flickr rather than having to get extra disk space, bandwidth, etc. on my hosting package. Due to Flickr's community guidelines, photos must link back to their Flickr photo page when hosted elsewhere, so this is provided for in the script.
The following two files are called in sequence. My API key and secret have been omitted for security purposes.
pullr-config.php:
<?php
require_once('./phpflickr/phpFlickr.php');
$username = "sam_butler";
$apiKey = "**********************";
$apiSecret = "************";
$f = new phpFlickr($apiKey);
//$f->enableCache("db","mysql://username:password@dbhost/mysql","300"); # Set caching parameters here
//$nsid = $f->people_findByUsername($username); # Gets Flickr nsid ([string]@N00) from username
$nsid= "54661716@N00";
/* Gallery variables */
$gallery[0] = array(
'title' => 'Portfolio', # Gallery title
'tag' => 'sbppf' # Tag used to create gallery
);
$gallery[1] = array(
'title' => 'Documentary, Events & Photojournalism', # Gallery title
'tag' => 'sbppj' # Tag used to create gallery
);
$gallery[2] = array(
'title' => 'Portraits', # Gallery title
'tag' => 'sbppt' # Tag used to create gallery
);
$gallery[3] = array(
'title' => 'Street Photography', # Gallery title
'tag' => 'sbpsp' # Tag used to create gallery
);
$gp = 20; # Gallery padding - this will override the CSS
$meta .= "<script type=\"text/javascript\" src=\"./js/fancybox/jquery.fancybox-1.3.1.js\"></script>
<link rel=\"stylesheet\" type=\"text/css\" href=\"./js/fancybox/jquery.fancybox-1.3.1.css\" media=\"screen\" />
<link rel=\"stylesheet\" type=\"text/css\" href=\"./pullr.css\" media=\"screen\" />
<script type=\"text/javascript\">
\$(document).ready(function() {\n";
foreach ($gallery as $key => $gal) {
$var = "\$(\"a[rel=fbgroup".$key."]\").fancybox({
'transitionIn' : 'none',
'transitionOut' : 'none',
'titlePosition' : 'inside',
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
return '<span id=\"fancybox-title-inside\">' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ': <strong><em>' + title.substring(0,title.indexOf(\"|\")) + '</em></strong> · <a href=\"http://www.flickr.com/photos/sam_butler/' +
title.substring(title.indexOf(\"|\")+1) + '\" rel=\"external\" target=\"_blank\">View on Flickr</a>' : '') + '</span>';
}
});\n";
$meta .= $var;
unset ($key);
unset ($gall);
}
$meta .= " });
</script>";
?>
pullr.php:
<h2>Portfolio & Galleries by Sam Butler, Photographer</h2>
<noscript>
<p>As Javascript is turned off in your browser, you will not see links to the Flickr photo pages for photos served on this page. To view the photos on Flickr, please visit <a href="http://www.flickr.com/photos/sam_butler/" rel="external">http://www.flickr.com/photos/sam_butler/</a></p>
</noscript>
<?php
/* Gallery pulling script below this line */
foreach ($gallery as $gkey => $g) {
$keyword = $g['tag'];
$args = array("tags"=>$keyword, "user_id"=>$nsid);
unset($photos);
$photos = $f->photos_search($args);
if (count($photos['photo']) == 0) {
echo "<!-- Gallery '".$g['title']."' had no images and was omitted. -->\n";
} else {
foreach ($photos['photo'] as $pkey => $photo) {
$s = $f->photos_getSizes($photo['id']);
$w = $s['2']['width'];
$h = $s['2']['height'];
$hi[$pkey] = $h;
$html .= "<a href='".$f->buildPhotoURL($photo, 'medium')."' title='".$photo['title']."|".$photo['id']."' rel=\"fbgroup".$gkey."\">";
$html .= "<img width='".$w."' height ='".$h."' border='0' alt='$photo[title]' "."src='".$f->buildPhotoURL($photo, 'small')."' id='photo_".$photo['id']."'></a>\n";
unset ($pkey);
unset ($photo);
}
if(isset($hi)) {
$gh = max($hi)+($gp*2); # set the height of the gallery div to the largest image height plus padding
}
echo "<h3>".$g['title']."</h3>\n<div class=\"h-gallery\" style=\"height: {$gh}px;\"><span class=\"middle\"></span>\n";
echo $html;
echo "\n</div>\n";
}
unset ($html);
unset ($hi);
unset ($gh);
unset ($gkey);
unset ($g);
} # end of galleries foreach
?>
Example: http://www.sambutlerphotography.co.uk/temp/new/portfolio.htm
Can you point out any changes that would reduce script execution time? I have been scripting with PHP for a number of years, but started before object-oriented programming was a big thing in PHP and have only intermediate knowledge. Any help would be appreciated.
Sam
P.S. The images and galleries displayed are for testing purposes and do not necessarily reflect the intended content of this website, which is currently in development. :evilgrin: --SB