Skip to content

Instantly share code, notes, and snippets.

@aghouseh
Last active December 21, 2015 09:49
Show Gist options
  • Save aghouseh/6287661 to your computer and use it in GitHub Desktop.
Save aghouseh/6287661 to your computer and use it in GitHub Desktop.
concrete5 image helper extension to allow for custom rendering of img tags. the intention is to provide a way to globally update img tag rendering in the future, pending a w3c approved responsive image solution
<?php defined('C5_EXECUTE') or die(_('Access Denied.'));
class ImageHelper extends Concrete5_Helper_Image {
/**
* Runs getThumbnail on the path, and then prints it out as an XHTML image
*/
public function outputThumbnail($obj, $maxWidth, $maxHeight, $alt = null, $return = false, $crop = false) {
$thumb = $this->getThumbnail($obj, $maxWidth, $maxHeight, $crop);
if ($return) {
return $thumb;
} else {
echo $thumb;
}
}
public function getThumbnail($obj, $maxWidth, $maxHeight, $crop = false) {
$data = parent::getThumbnail($obj, $maxWidth, $maxHeight, $crop = false);
$data->file = $obj;
$data->alt = $obj->getTitle();
$thumb = new Thumbnail($data);
return $thumb;
}
}
class Thumbnail extends Object {
public $src, $width, $height;
private $alt, $file, $properties = array();
private $is_responsive = true;
private $element_path = 'html/img';
function __construct($options) {
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
}
function __toString() {
return $this->renderTag();
}
public function renderTag() {
ob_start();
Loader::element($this->getElementPath(), $this->getTagProperties());
$tag = ob_get_contents();
ob_end_clean();
return $tag;
}
public function output() {
echo $this->renderTag();
}
public function getSrc() { return $this->src; }
public function setSrc($src) { $this->src = $src; }
public function getAlt() { return $this->alt; }
public function setAlt($alt) { $this->alt = $alt; }
public function getWidth() { return $this->width; }
public function setWidth($width) { $this->width = $width; }
public function getHeight() { return $this->height; }
public function setHeight($height) { $this->height = $height; }
public function getElementPath() { return $this->element_path; }
public function setElementPath($element_path) { $this->element_path = $element_path; }
public function setProperty($property, $value) { $this->properties[$property] = $value; }
public function getProperty($property) { return $this->properties[$property]; }
public function isResponsive() { return $this->is_responsive; }
public function setResponsive(Boolean $is_responsive) { $this->is_responsive = $is_responsive; }
public function getTagProperties() {
// set our base required properties
$properties = array(
'src' => $this->getSrc(),
'alt' => $this->getAlt()
);
// if we are not doing responsive images, output width and height
if (!$this->isResponsive()) {
$properties['width'] = $this->getWidth();
$properties['height'] = $this->getHeight();
}
// if we have any custom properties set to the image tag, merge that in as well
if (count($this->properties)) {
$properties = array_merge(array('properties' => $this->properties), $properties);
}
return $properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment