Skip to content

Instantly share code, notes, and snippets.

@danmillar
Created June 14, 2012 11:20
Show Gist options
  • Save danmillar/2929717 to your computer and use it in GitHub Desktop.
Save danmillar/2929717 to your computer and use it in GitHub Desktop.
Activate - Flickr
<?php if(count($this->blocks)): ?>
<section>
<?php foreach($this->blocks as $block): ?>
<div class="block">
<?php foreach($block->getSquares() as $square): ?>
<a href="<?php echo $square->getHref(); ?>">
<img src="<?php echo $square->getSrc(); ?>" alt="<?php echo $square->getAlt(); ?>" width="<?php echo $square->getWidth(); ?>" height="<?php echo $square->getHeight(); ?>" />
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</section>
<?php endif; ?>
<?php
namespace Icf\App\Module\Home\Area_Front;
use Icf\Core;
use Icf\Arch;
use Icf\Neon;
use Icf\Axis;
class FlickrComponent extends Arch\Component\Base {
protected $_blocks;
public function mosaic() {
$view = $this->getView('Components/Flickr.htm');
$cache = new Core\Cache\Handler('Module_Flickr_Component_Mosaic');
$cache->setLifetime(21600);
//$cache->clear();
if(!isset($cache->blocks)) {
$this->_blocks = $this->getBlocks();
$this->addSponsor('/assets/theme/images/sponsor_mailchimp.gif', 'MailChimp', 'http://mailchimp.com');
$this->addSponsor('/assets/theme/images/sponsor_8faces.gif', '8 Faces', 'http://8faces.com');
$this->addSponsor('/assets/theme/images/sponsor_ajuk.gif', 'Authentic Jobs', 'http://uk.authenticjobs.com');
$this->addSponsor('/assets/theme/images/sponsor_fss.gif', 'Five Simple Steps', 'http://fivesimplesteps.com');
$this->addSponsor('/assets/theme/images/sponsor_duuel.gif', 'Duuel', 'http://duuel.com');
$this->addSponsor('/assets/theme/images/sponsor_gowalla.gif', 'Gowalla', 'http://gowalla.com');
$this->addSponsor('/assets/theme/images/sponsor_ubelly.gif', 'Ubelly', 'http://www.ubelly.com/?ocid=soc-c-gb-AS-loc-F0-');
$this->addSponsor('/assets/theme/images/flickr_button.gif', 'Flickr', 'http://www.flickr.com/groups/teamactivate');
$cache->blocks = $this->_blocks;
}
$this->_blocks = $cache->blocks;
shuffle($this->_blocks);
$view->blocks = $this->_blocks;
return $view;
}
private function addSponsor($src, $alt, $href) {
$square = new mosaicSquare;
$square->setSrc($src);
$square->setAlt($alt);
$square->setWidth(150);
$square->setHeight(150);
$square->setHref($href);
$block = new mosaicBlock;
$block->addSquare($square);
$this->_blocks[] = $block;
}
private function getBlocks() {
$flickr = $this->getModel('Flickr');
$flickr->manager->setGroupid('1726463@N20');
$flickr->manager->setLifetime(86400);
$photos = $flickr->manager->getPhotos();
$squares = array();
$blocks = array();
foreach($photos as $photo) {
$photo = new flickrPhoto($photo);
$square = new mosaicSquare;
$square->setSrc($photo->getSrc());
$square->setAlt($photo->getAlt());
$square->setWidth(75);
$square->setHeight(75);
$square->setHref($photo->getLink());
$squares[] = $square;
}
shuffle($squares);
$squares = array_chunk($squares, 4);
foreach($squares as $b) {
$block = new mosaicBlock;
foreach($b as $square) {
$block->addSquare($square);
}
$blocks[] = $block;
}
return $blocks;
}
}
class mosaicBlock {
protected $_width;
protected $_height;
protected $_squares;
public function addSquare($square) {
$this->_squares[] = $square;
//$this->calculateDimensions();
}
public function getSquares() {
return $this->_squares;
}
private function calculateDimensions() {
foreach($this->_squares as $square) {
if($square->getWidth() > $this->_width) {
$this->_width = $square->getWidth();
}
if($square->getHeight() > $this->_height) {
$this->_height = $square->getHeight();
}
}
}
}
class mosaicSquare {
protected $_src;
protected $_alt;
protected $_width;
protected $_height;
protected $_href;
public function setSrc($src) {
$this->_src = $src;
}
public function getSrc() {
return $this->_src;
}
public function setAlt($alt) {
$this->_alt = $alt;
}
public function getAlt() {
return $this->_alt;
}
public function setWidth($width) {
$this->_width = $width;
}
public function getWidth() {
return $this->_width;
}
public function setHeight($height) {
$this->_height = $height;
}
public function getHeight() {
return $this->_height;
}
public function hasHref() {
return (bool) $this->_href;
}
public function setHref($href) {
$this->_href = $href;
}
public function getHref() {
return $this->_href;
}
}
class flickrPhoto {
protected $_photo;
public function __construct($photo) {
$this->_photo = $photo;
}
public function getSrc() {
return "http://farm".$this->_photo['farm'].".static.flickr.com/".$this->_photo['server']."/".$this->_photo['id']."_".$this->_photo['secret']."_s.jpg";
}
public function getAlt() {
return $this->_photo['title'];
}
public function getLink() {
return "http://www.flickr.com/photos/".$this->_photo['owner']."/".$this->_photo['id']."";
}
}
<?php
namespace Icf\App\Model\Flickr\Extension;
use Icf\Core;
use Icf\Axis;
class Manager extends Axis\Model\Extension {
protected $_apiKey = "10357275ce6ebb36146cf286f5c0cece";
protected $_secret = "00319c27fd311cf1";
protected $_lifetime = 300;
protected $_groupId;
public function setLifetime($miliseconds) {
return $this->_lidetime = $miliseconds;
}
public function getLifetime() {
return $this->_lifetime;
}
public function setGroupId($id) {
return $this->_groupId = $id;
}
public function getGroupId() {
return $this->_groupId;
}
public function getPhotos() {
if(!$this->getGroupId()) {
return null;
}
$cache = new Core\Cache\Handler('Model_Flickr_Extension_Manager_'.str_replace('@', '_', $this->getGroupId()));
$cache->setLifetime($this->getLifetime());
if(!isset($cache->response)) {
$options = array(
'group_id' => $this->getGroupId(),
'per_page' => 500,
);
$cache->response = $this->makeCall('flickr.groups.pools.getPhotos', $options);
}
return $cache->response['photos']['photo'];
}
private function makeCall($method, $options=array()) {
$restUrl = "http://api.flickr.com/services/rest/";
$options += array(
'api_key' => $this->_apiKey,
'format' => 'php_serial'
);
$attr = array();
foreach($options as $k => $v) {
$attr[] = $k.'='.urlencode($v);
}
$requestUrl = $restUrl.'?method='.$method;
if(count($attr)) {
$requestUrl .= '&'.implode('&', $attr);
}
$response = file_get_contents($requestUrl);
return unserialize($response);
}
}
body {
margin: 0;
padding: 0;
background: #000 url(../images/body.jpg);
}
.block {
float: left;
width: 150px;
height: 150px;
}
.block img {
border: none;
margin: 0;
padding: 0;
float: left;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment