Last active
December 14, 2015 02:09
-
-
Save marchawkins/5011370 to your computer and use it in GitHub Desktop.
Coding a photo widget from designmodo with flickr, php and javascript. Design tutorial is here: http://designmodo.com/instagram-widget-psd/ And my code tutorial is here: http://www.marchawkins.com/notes/flickr-photo-widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* -------------------------------------------- | |
START USER CONFIG -------------------------- */ | |
$flickrUser = 'YOURUSERNAME'; // look at your profile url: http://flickr.com/photos/YOURUSERNAME/ | |
$photoCount = '7'; // the # of photos for the scroller + 1 for top photo (minimum of 3, odd numbers work best) | |
$flickrAPI = 'YOURFLICKRAPIKEY'; // get yours here: http://www.flickr.com/services/api/keys/apply/ | |
/* END USER CONFIG ---------------------------- | |
-------------------------------------------- */ | |
global $flickrAPI; | |
// get the NSID from the username | |
$flickrUser = getFlickr(array('method'=>'flickr.people.findByUsername','username'=>$flickrUser)); | |
$flickrID = $flickrUser['user']['id']; | |
// setup parameters to find the latest photos | |
$photoParams = array( | |
'method' => 'flickr.photos.search', | |
'user_id' => $flickrID, | |
'per_page' => $photoCount | |
); | |
$flickrInfo = getFlickr($photoParams); | |
$userParams = array( | |
'method' => 'flickr.people.getInfo', | |
'user_id' => $flickrID | |
); | |
$userInfo = getFlickr($userParams); | |
// create a new array of photos for the scroller, starting with the second photo | |
$photos = array(); | |
for($i=1; $i <= (count($flickrInfo["photos"]["photo"])-1); $i++) { | |
$myPhoto = array( | |
// create urls for thumb and large photo sizes, more info: http://www.flickr.com/services/api/misc.urls.html | |
'thumb' => "http://farm".$flickrInfo['photos']['photo'][$i]['farm'].".static.flickr.com/".$flickrInfo['photos']['photo'][$i]['server']."/".$flickrInfo['photos']['photo'][$i]['id']."_".$flickrInfo['photos']['photo'][$i]['secret']."_m.jpg", | |
'large' => "http://farm".$flickrInfo['photos']['photo'][$i]['farm'].".static.flickr.com/".$flickrInfo['photos']['photo'][$i]['server']."/".$flickrInfo['photos']['photo'][$i]['id']."_".$flickrInfo['photos']['photo'][$i]['secret']."_z.jpg", | |
// grab the photo title, converting special characters to html entities | |
'title' => htmlentities($flickrInfo['photos']['photo'][$i]['title'],ENT_QUOTES) | |
); | |
array_push($photos,$myPhoto); | |
} | |
$photoTotal = count($photos); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>photo widget</title> | |
<script src="swipe.min.js"></script> | |
<style> | |
body {width: 100%; text-align: center; font-size: 100%; font-family: verdana,helvetica,sans-serif; background: #ccc;} | |
.container {width: 550px; height: 415px; margin: 50px auto; padding: 30px 0; background: url('img/wood-bg.jpg') 0% 0% no-repeat;} | |
a, img {border: 0;} | |
a {text-decoration: none;} | |
.widget {width: 335px; min-height: 350px; margin: 0 auto; background: url('img/widget-bg.jpg') 0% 0% no-repeat #e9e9e9; border: 1px solid #1a1a1a; overflow: hidden; -webkit-box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, .5); box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, .5);} | |
.widget .top {width: 335px; height: 170px;} | |
.widget .avatar {width: 75px; height: 75px; margin: 0 auto; position: relative; top: 25px; padding: 3px; background: #fff; -webkit-box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, .5); box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, .5);} | |
.widget .button {width: 120px; height: 30px; margin: 0 auto; position: relative; top: 40px; -webkit-border-radius: 10px; border-radius: 10px; -webkit-box-shadow: 0px 2px 3px 0px rgba(0, 0, 0, .4); box-shadow: 0px 2px 3px 0px rgba(0, 0, 0, .4); background: #f377a3; background: -moz-linear-gradient(top, #f377a3 0%, #ef5f92 22%, #e53a6c 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f377a3), color-stop(22%,#ef5f92), color-stop(100%,#e53a6c)); background: -webkit-linear-gradient(top, #f377a3 0%,#ef5f92 22%,#e53a6c 100%); background: -o-linear-gradient(top, #f377a3 0%,#ef5f92 22%,#e53a6c 100%); background: -ms-linear-gradient(top, #f377a3 0%,#ef5f92 22%,#e53a6c 100%); background: linear-gradient(to bottom, #f377a3 0%,#ef5f92 22%,#e53a6c 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f377a3', endColorstr='#e53a6c',GradientType=0 );} | |
.widget .button a {display: block; padding: 7px; font-size: 75%; color: #fff; font-weight: bold; text-decoration: none;} | |
.widget .bottom {height: 160px; padding: 10px;} | |
.widget .name {width: 130px; margin-right: 180px; text-align: left;} | |
.widget h1 {margin: 0 0 2px 0; padding: 8px 0 0 0; font-size: 90%; text-shadow: 1px 1px 1px #ffffff; filter: dropshadow(color=#ffffff, offx=1, offy=1);} | |
.widget h1 a {color: #000;} | |
.widget h2 {margin: 0; padding: 0; font-size: 75%; font-weight: normal; color: #838383;} | |
.widget h2 a {color: #838383;} | |
.widget .stats {float: right; width: 180px; height: 45px; font-size: 8pt; color: #707070; border: 1px solid #ccc; overflow: hidden; -webkit-border-radius: 10px; border-radius: 10px; background: transparent; background: -moz-linear-gradient(top, #ffffff 0%, #e6e6e6 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e6e6e6)); background: -webkit-linear-gradient(top, #ffffff 0%,#e6e6e6 100%); background: -o-linear-gradient(top, #ffffff 0%,#e6e6e6 100%); background: -ms-linear-gradient(top, #ffffff 0%,#e6e6e6 100%); background: linear-gradient(to bottom, #ffffff 0%,#e6e6e6 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#e6e6e6',GradientType=0 );} | |
.widget .stats strong {display: block; padding-top: 8px; font-size: 10pt;} | |
.widget .stats p {height: 45px; margin: 0; padding: 0; overflow: hidden;} | |
.widget .stats .left {float: left; width: 49%; border-right: 1px solid #ccc;} | |
.widget .stats .right {float: right: width: 49%;} | |
.swipe {width: 315px; position: relative; top: 15px; overflow: hidden; visibility: hidden; position: relative;} | |
.swipe-wrap {overflow: hidden; position: relative;} | |
.swipe-wrap div {float:left; width:100%; position: relative;} | |
.swipe div a {display: block; float: left; width: 150px; height: 67px; border: 2px solid #fff; overflow: hidden;} | |
.swipe div a:first-child {margin-right: 7px;} | |
#slide-pos {clear: both; position: relative; top: 25px; font-family: verdana,helvetica,sans-serif; font-size: 24px; line-height: .5em; text-align: center;} | |
#slide-pos .slider-pos {color: #fff; text-shadow: 0px 3px 3px #cccccc; filter: dropshadow(color=#cccccc, offx=0, offy=3);} | |
#slide-pos .active {color: #ef5f92; text-shadow: 0px 0px 0px #ffffff; filter: dropshadow(color=#ffffff, offx=0, offy=0);} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="widget" id="flickr"> | |
<div class="top" style="background: url('http://farm<?= $flickrInfo['photos']['photo'][0]['farm'] ?>.static.flickr.com/<?= $flickrInfo['photos']['photo'][0]['server'] ?>/<?= $flickrInfo['photos']['photo'][0]['id'] ?>_<?= $flickrInfo['photos']['photo'][0]['secret'] ?>_z.jpg') 50% 50% no-repeat;"> | |
<div class="avatar"><a href="<?= $userInfo['person']['profileurl']['_content'] ?>" title="view profile"><img src="http://www.flickr.com/buddyicons/<?= $flickrID ?>.jpg" width="75" height="75" alt="<?= $userInfo['person']['realname']['_content'] ?>"/></a></div> | |
<div class="button"><a href="<?= $userInfo['person']['photosurl']['_content'] ?>" title="View photos">view photos</a></div> | |
</div><!-- .top --> | |
<div class="bottom"> | |
<div class="stats"> | |
<p class="left"><strong><?= number_format($userInfo['person']['photos']['count']['_content']); ?></strong>photos</p> | |
<p class="right"><strong><?= date('M \&\l\s\q\u\o\;y',strtotime($userInfo['person']['photos']['firstdatetaken']['_content'])); ?></strong>first photo</p> | |
</div><!-- .stats --> | |
<div class="name"> | |
<h1><a href="<?= $userInfo['person']['profileurl']['_content'] ?>" title="<?= $userInfo['person']['realname']['_content'] ?>"> | |
<?php $maxLength = 13; | |
if(!$userInfo['person']['realname']['_content'] || $userInfo['person']['realname']['_content']=='' || strlen($userInfo['person']['realname']['_content']) > $maxLength) { | |
echo substr($userInfo['person']['username']['_content'],0,$maxLength-2)."…"; | |
} else { | |
echo $userInfo['person']['realname']['_content']; | |
} | |
?> | |
</a></h1> | |
<h2><a href="<?= $userInfo['person']['photosurl']['_content'] ?>" title="View photostream">flickr photostream</a></h2> | |
</div><!-- .name --> | |
<div class="recent"> | |
<div id="slider" class="swipe"> | |
<div class="swipe-wrap"> | |
<?php | |
// loop through photos array to build scrolling images | |
$counter = 1; // use a counter to split the photos into groups of two | |
for($i=0; $i<$photoTotal; $i++) { | |
if($counter>0) { echo '<div>'; } | |
echo '<a href="'.$photos[$i]['large'].'" title="'.$photos[$i]['title'].'"><img src="'.$photos[$i]['thumb'].'" width="150" alt="'.$photos[$i]['title'].'"/></a>'; | |
if($counter<0) { echo '</div>'; } | |
$counter *= -1; | |
} ?> | |
</div><!-- .swipe-wrap --> | |
</div><!-- #slider --> | |
<div id="slide-pos"> | |
<span id="pos-0" class="slider-pos active">•</span> | |
<?php | |
// create the bullet indicators, based on total # of photos (starting with 2nd bullet) | |
$bulletTotal = round($photoTotal/2); | |
for($i=1; $i<($bulletTotal); $i++) { | |
echo '<span id="pos-'.$i.'" class="slider-pos">•</span> '; | |
} | |
?> | |
</div><!-- #slide-pos --> | |
</div><!-- .recent --> | |
</div><!-- .bottom --> | |
</div><!-- .widget#flickr --> | |
</div><!-- .container --> | |
<script> | |
window.mySwipe = new Swipe(document.getElementById('slider'), { | |
speed: 800, | |
auto: 3000, | |
callback: function() { | |
var position = 'pos-' + mySwipe.getPos(); | |
document.getElementById("pos-0").className = "slider-pos"; | |
<?php for($i=1; $i<($bulletTotal); $i++) { ?> | |
document.getElementById("pos-<?= $i ?>").className = "slider-pos"; | |
<?php } ?> | |
document.getElementById(position).className = "slider-pos active"; | |
} | |
}); | |
</script> | |
</body> | |
</html> | |
<?php | |
// functions to interact with flickr | |
function getFlickr($params) { | |
global $flickrAPI; | |
if(count($params)>0) { | |
$params["api_key"] = $flickrAPI; | |
$params["format"] = "php_serial"; | |
$encoded_params = array(); | |
foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); } | |
$ch = curl_init(); | |
$timeout = 5; // set to zero for no timeout | |
curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&', $encoded_params)); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
$file_contents = curl_exec($ch); | |
curl_close($ch); | |
$flickrInfo = unserialize($file_contents); | |
return $flickrInfo; | |
} else { | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment