Skip to content

Instantly share code, notes, and snippets.

@hummer2k
Last active September 14, 2017 17:26
Show Gist options
  • Save hummer2k/c14a9a4a24adb50d6a44 to your computer and use it in GitHub Desktop.
Save hummer2k/c14a9a4a24adb50d6a44 to your computer and use it in GitHub Desktop.
Magento 1.x Image Helper
<?php
/**
* Class Acme_Module_Helper_Image
* @method $this rotate($angle)
* @method $this crop($top=0, $left=0, $right=0, $bottom=0)
* @method $this resize($width, $height = null)
* @method $this keepAspectRatio($value)
* @method $this keepFrame($value)
* @method $this keepTransparency($value)
* @method $this constrainOnly($value)
* @method $this backgroundColor($value)
* @method $this quality($value) Get/set quality, values in percentage from 0 to 100
*/
class Acme_Module_Helper_Image extends Mage_Core_Helper_Abstract
{
/**
* @var string
*/
protected $_filename;
/**
* @var string
*/
protected $_baseDir;
/**
* @var string
*/
protected $_baseUrl;
/**
* @var string
*/
protected $_fallbackImage;
/**
* @var string
*/
protected $_adapter = Varien_Image_Adapter::ADAPTER_GD2;
/**
* @var bool
*/
protected $_enableCacheBusting = false;
/**
* @var array
*/
protected $_schedule = array();
/**
* @var array|callable[]
*/
protected $_callbacks = array();
/**
* @var \ImageOptimizer\Optimizer
*/
protected $_optimizer;
/**
* @var bool
*/
protected $_optimize = false;
/**
* @var string
*/
protected $_destination;
/**
* @param string $filename
* @return $this
*/
public function init($filename)
{
$this->_reset();
$this->_filename = $filename;
return $this;
}
/**
* @param string $baseDir
* @return $this
*/
public function setBaseDir($baseDir)
{
$this->_baseDir = rtrim($baseDir, '\\/');
return $this;
}
/**
* @param string $baseUrl
* @return $this
*/
public function setBaseUrl($baseUrl)
{
$this->_baseUrl = rtrim($baseUrl, '/');
return $this;
}
/**
* @param string $adapter
* @return $this
*/
public function setAdapter($adapter)
{
$this->_adapter = $adapter;
return $this;
}
/**
* @param string $fallbackImage
* @return $this
*/
public function setFallbackImage($fallbackImage)
{
$this->_fallbackImage = $fallbackImage;
return $this;
}
/**
* @param boolean $enableCacheBusting
* @return $this
*/
public function setEnableCacheBusting($enableCacheBusting)
{
$this->_enableCacheBusting = $enableCacheBusting;
return $this;
}
/**
* @param array $config
* @return $this
*/
public function configure(array $config)
{
foreach ($config as $key => $value) {
$setter = 'set' . ucfirst($key);
if (method_exists($this, $setter)) {
$this->{$setter}($value);
}
}
return $this;
}
/**
* @return string
*/
protected function _getBaseUrl()
{
if (!$this->_baseUrl) {
$this->setBaseUrl(Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA));
}
return $this->_baseUrl;
}
/**
* @return string
*/
protected function _getBaseDir()
{
if (!$this->_baseDir) {
$this->setBaseDir(Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA));
}
return $this->_baseDir;
}
/**
* @return string
*/
protected function _getAdapter()
{
if (!$this->_adapter) {
$this->setAdapter(Varien_Image_Adapter::ADAPTER_GD2);
}
return $this->_adapter;
}
/**
* reset schedule
*/
protected function _reset()
{
$this->_schedule = array();
}
/**
* schedule image manipulation
*
* @param string $method
* @param array $params
*/
protected function _schedule($method, array $params)
{
$this->_schedule[$method] = $params;
}
/**
* proxy to _schedule
*
* @param string $name
* @param array $arguments
* @return $this
*/
public function __call($name, $arguments)
{
$this->_schedule($name, $arguments);
return $this;
}
/**
* determine destination of manipulated image
*
* @return string
*/
protected function _determineDestination()
{
$params = $this->_schedule;
if (empty($this->_schedule)) {
return $this->_getSource();
}
$filename = basename($this->_filename);
if ($this->_enableCacheBusting) {
$filename = filemtime($this->_getSource()) . '-' . $filename;
}
$dirname = dirname($this->_filename) . DS . 'cache';
$destBaseDir = $this->_getBaseDir();
$destBaseDir.= DS . $dirname;
if (isset($params['resize'])) {
$destBaseDir.= DS . implode('x', $params['resize']);
unset($params['resize']);
} else {
$destBaseDir.= DS . 'orig';
}
$hash = $this->_hash($params);
$destination = $destBaseDir . DS . $hash . DS . $filename;
return $destination;
}
/**
* @param array $params
* @return string
*/
protected function _hash(array $params)
{
return md5(json_encode($params));
}
/**
* @return string
*/
protected function _getSource()
{
$source = $this->_getBaseDir() . DS . $this->_filename;
return $source;
}
/**
* save processed image if not exists
*/
protected function _save()
{
$destination = $this->_determineDestination();
if (!is_file($destination)) {
$image = new Varien_Image($this->_getSource(), $this->_getAdapter());
foreach ($this->_schedule as $method => $params) {
if (is_callable(array($image, $method))) {
call_user_func_array(
array($image, $method),
$params
);
}
}
$image->save($destination);
$this->_optimize($destination);
}
}
/**
* @param string $filepath
*/
protected function _optimize($filepath)
{
if ($this->_optimize && ($optimizer = $this->getOptimizer())) {
$optimizer->optimize($filepath);
}
}
/**
* @return string
*/
public function getUrl()
{
if (empty($this->_schedule)) {
return $this->_getBaseUrl() . '/' . $this->_filename;
}
$this->_save();
$url = str_replace(
$this->_getBaseDir(),
$this->_getBaseUrl(),
$this->_determineDestination()
);
return $url;
}
/**
* @return string
*/
protected function _getFallbackUrl()
{
if (parse_url($this->_fallbackImage, PHP_URL_SCHEME)) {
return $this->_fallbackImage;
}
return Mage::getDesign()->getSkinUrl($this->_fallbackImage);
}
/**
* @param bool $flag
* @return $this
*/
public function optimize($flag = true)
{
$this->_schedule('optimize', [$flag]);
$this->_optimize = $flag;
return $this;
}
/**
* @return \ImageOptimizer\Optimizer
*/
public function getOptimizer()
{
if (null === $this->_optimizer && class_exists('ImageOptimizer\OptimizerFactory')) {
$optimizerFactory = new \ImageOptimizer\OptimizerFactory();
$this->_optimizer = $optimizerFactory->get();
}
return $this->_optimizer;
}
/**
* @param \ImageOptimizer\Optimizer $optimizer
* @return $this
*/
public function setOptimizer(ImageOptimizer\Optimizer $optimizer)
{
$this->_optimizer = $optimizer;
return $this;
}
/**
* @return string
*/
public function __toString()
{
try {
return $this->getUrl();
} catch (Exception $e) {
Mage::logException($e);
return $this->_getFallbackUrl();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment