mkoh.net beta!

Lähdekoodi

Lähdekoodi tiedostolle: 'library/MkohZf/Image.php'


<?php
/**
 * Class for managing image resizes
 * 
 * Depends on Imagick extension
 * @todo Substitute custom cache with Zend_Cache 
 * @author mkoh
 */
class MkohZf_Image 
{
	/** @var $_pathOriginal string Filesystem path to the directory where original images are */
	protected $_pathOriginal;
	/** @var $_pathCache string Filesystem path to a writable directory where cached images are stored */
	protected $_pathCache;
	/** @var $_config Zend_Config Configuration for handling media (resize dimensions per type) */
	protected $_config;
	/** @var $_cacheList Array An Array of cached files */
	protected $_cacheList = Array();
	
	public function __construct($dirOriginal, $dirCache)
	{
		if(!is_dir($dirOriginal) || !is_readable($dirOriginal)) {
			throw new MkohZf_Exception('Problem with source directory ("'.$dirOriginal.'")');
		}
		if(!is_dir($dirCache) || !is_writeable($dirCache)) {
			throw new MkohZf_Exception('Problem with target directory ("'.$dirCache.'")');
		}
		$this->_pathOriginal = $dirOriginal;
		$this->_pathCache = $dirCache;
		$this->_config = Zend_Registry::get('config')->media;
		$this->_cacheUpdate();
	}
	
	public function getResized($imagepath, $resizetype) 
	{
		if(!$path = $this->_cacheGet($imagepath, $resizetype)) {
			try {
				$path = $this->_resizeImage($imagepath, $resizetype);
			} catch(Exception $e) {
				throw new MkohZf_Exception($e);
			}
		}
		return $path;
	}
	
	protected function _resizeImage($imagepath, $resizetype)
	{
		$cacheName = $this->_getCachedName($imagepath, $resizetype);
		$filepath = $this->_pathOriginal.'/'.$imagepath;
		$cachepath = $this->_pathCache.'/'.$cacheName;
		
		if(!is_readable($filepath)) throw new MkohZf_Exception('Source ('.$filepath.') not readable');
		
		$img = new Imagick($filepath);
		$width = $this->_config->resizes->{$resizetype}->width;
		$height = $this->_config->resizes->{$resizetype}->height;
		
		$fitbyWidth = (($width/$img->getImageWidth())<($height/$img->getImageHeight())) ?true:false;

		if($fitbyWidth) {
		    $img->thumbnailImage($width, 0, false);
		} else {
		    $img->thumbnailImage(0, $height, false);
		}
		
		
		
		if(!$img->writeImage($cachepath)) throw new MkohZf_Exception('Could not write target ('.$cachepath.')');
		
		$this->_cacheSet($imagepath, $resizetype);
		
		return $cacheName;
	}
	/**
	 * Initializes cacheList from the cache dir
	 *
	 */
	protected function _cacheUpdate() 
	{
		$this->_cacheList = Array();
		foreach(scandir($this->_pathCache) as $file) {
			$this->_cacheList[] = $file;
		}
	}
	protected function _cacheGet($imagepath, $resizetype) 
	{
		$cachedName = self::_getCachedName($imagepath, $resizetype);
		if(in_array($cachedName, $this->_cacheList)) return $cachedName;
		return false;
	}
	protected function _cacheSet($imagepath, $resizetype)
	{
		$cachedName = self::_getCachedName($imagepath, $resizetype);
		$this->_cacheList[] = $cachedName;
	}
	
	protected static function _getCachedName($filepath, $resizetype) 
	{
		return $resizetype.'-'.str_replace(DIRECTORY_SEPARATOR, '_', $filepath);
	}
}

Kommentit

Ei kommentteja, mikset kirjoittaisi ensimmäistä?

Kommentoi

  ______    _____    __   _      ___      ____       ___    
 /_   _//  |  ___|| | || | ||   / _ \\   |  _ \\    / _ \\  
 `-| |,-   | ||__   | '--' ||  / //\ \\  | |_| ||  / //\ \\ 
   | ||    | ||__   | .--. || |  ___  || | .  //  |  ___  ||
   |_||    |_____|| |_|| |_|| |_||  |_|| |_|\_\\  |_||  |_||
   `-`'    `-----`  `-`  `-`  `-`   `-`  `-` --`  `-`   `-`