Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created February 23, 2010 15:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebiros/312277 to your computer and use it in GitHub Desktop.
Save nebiros/312277 to your computer and use it in GitHub Desktop.
Thumbnails lib
<?php
class App_Thumb
{
protected $_width = null;
protected $_oldWidth = null;
protected $_height = null;
protected $_oldHeight = null;
protected $_imagePath = null;
protected $_resource = null;
protected $_cacheFolder = null;
protected $_output = "jpg";
protected $_thumbName = null;
/**
* Constructor.
*
* @param array $options
*/
public function __construct( Array $options = array() )
{
$this->setOptions( $options );
}
/**
* Set thumb options.
*
* @param array $options
* @return App_Thumb
*/
public function setOptions( Array $options = array() )
{
if ( false !== empty( $options ) )
{
return $this;
}
foreach ( $options AS $key => $value )
{
switch ( strtolower( $key ) )
{
case "filename":
$this->setImagePath( $value );
break;
case "width":
$this->setWidth( $value );
break;
case "height":
$this->setHeight( $value );
break;
case "cachefolder":
$this->setCacheFolder( $value );
break;
case "output":
$this->setOutput( $value );
break;
}
}
return $this;
}
/**
* Set width.
*
* @param int $width
* @return App_Thumb
*/
public function setWidth( $width )
{
$this->_width = ( int ) $width;
return $this;
}
/**
* Get width.
*
* @param int
*/
public function getWidth()
{
return $this->_width;
}
/**
* Set height.
*
* @param int $height
* @return App_Thumb
*/
public function setHeight( $height )
{
$this->_height = ( int ) $height;
return $this;
}
/**
* Get height.
*
* @return int
*/
public function getHeight()
{
return $this->_height;
}
/**
* Set filename.
*
* @param string $imagePath
* @return App_Thumb
*/
public function setImagePath( $imagePath )
{
$this->_imagePath = realpath( $imagePath );
return $this;
}
/**
* Get filename.
*
* @return string
*/
public function getImagePath()
{
return $this->_imagePath;
}
/**
* Set cache folder.
*
* @param string $cacheFolder
* @return App_Thumb
*/
public function setCacheFolder( $cacheFolder = null )
{
$this->_cacheFolder = realpath( $cacheFolder );
if ( false !== empty( $cacheFolder ) )
{
$this->_cacheFolder = realpath( APPLICATION_PATH . "/../tmp/" );
}
return $this;
}
/**
* Get cache folder.
*
* @return string
*/
public function getCacheFolder()
{
return $this->_cacheFolder;
}
/**
* Set output type.
*
* @param string $output
* @return App_Thumb
*/
public function setOutput( $output = null )
{
$this->_output = strtolower( $output );
return $this;
}
/**
* Get output type.
*
* @return string
*/
public function getOutput()
{
return $this->_output;
}
/**
* Create thumb.
*
* @return string
*/
public function create()
{
// Set default cache folder first, if none is set.
if ( false !== empty( $this->_cacheFolder ) )
{
$this->setCacheFolder();
}
$thumbFilename = $this->setThumbName()->getThumbName();
$thumbPath = $this->_cacheFolder . "/" . $thumbFilename;
if ( false !== is_file( $thumbPath ) )
{
return $thumbFilename;
}
// Set resource.
if ( false === $this->_setResource() )
{
return null;
}
// Set old values.
$this->_oldWidth = imagesx( $this->_resource );
$this->_oldHeight = imagesy( $this->_resource );
// Calculate portrait or landscape.
if ( imagesx( $this->_resource ) < imagesy( $this->_resource ) )
{
$scale = imagesy( $this->_resource ) / $this->_width;
}
else
{
$scale = imagesx( $this->_resource ) / $this->_width;
}
$thumbWidth = round( imagesx( $this->_resource ) / $scale );
$thumbHeight = round( imagesy( $this->_resource ) / $scale );
$thumbResource = imagecreatetruecolor( $thumbWidth, $thumbHeight );
// TODO: I guess I need to calculate alphas (jfas).
imagecopyresampled( $thumbResource, $this->_resource, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $this->_oldWidth, $this->_oldHeight );
switch ( strtolower( $this->_output ) )
{
case "gif":
imagegif( $thumbResource, $thumbPath );
break;
case "png":
imagepng( $thumbResource, $thumbPath );
break;
case "jpg":
default:
imagejpeg( $thumbResource, $thumbPath );
break;
}
imagedestroy( $thumbResource );
imagedestroy( $this->_resource );
return $thumbFilename;
}
/**
* Set resource.
*
* @return void
*/
protected function _setResource()
{
if ( false !== empty( $this->_imagePath ) )
{
return false;
}
$extension = $this->getExtension();
try
{
switch ( $extension )
{
case "jpg":
case "jpeg":
$this->_resource = imagecreatefromjpeg( $this->_imagePath );
break;
case "gif":
$this->_resource = imagecreatefromgif( $this->_imagePath );
break;
case "png":
$this->_resource = imagecreatefrompng( $this->_imagePath );
break;
default:
throw new Exception( "Image not supported, sorry" );
break;
}
}
catch ( Exception $exception )
{
throw new Exception( "Can't create image resource ({$exception->getMessage()})" );
}
}
/**
* Get filename extension.
*
* @return string
*/
public function getExtension()
{
if ( false !== empty( $this->_imagePath ) )
{
throw new Exception( "Please set filename" );
}
$filenameExplode = explode( ".", $this->_imagePath );
$extension = array_pop( $filenameExplode );
return strtolower( $extension );
}
/**
* Set thumb name.
*
* @return App_Thumb
*/
public function setThumbName( $thumbName = null )
{
$this->_thumbName = $thumbName;
// If we don't have a thumb name here, then set one!.
if ( false !== empty( $thumbName ) )
{
if ( false !== empty( $this->_imagePath ) )
{
return $this;
}
$this->_thumbName = "thumb_" . strtolower( preg_replace( "/\s+/", "_", basename( $this->_imagePath ) ) );
}
return $this;
}
/**
* Get thumb name.
*
* @return string
*/
public function getThumbName()
{
return $this->_thumbName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment