<?php
/**
* @version 1.0.0
* @category Anahita Social Engineâ¢
* @copyright Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.anahitapolis.com
*/
class AnUtilImage extends KObject
{
static function resize( $image, $size , $options=array())
{
$options = array_merge(array('format'=>null), $options);
if ( !$image )
return false;
$height = null;
$width = null;
if ( strpos($size,'x') || count((array) $size) == 2 ) {
list($width,$height) = is_array($size) ? $size : explode('x',$size);
} else {
$size = (array) $size;
$width = (int) $size[0];
}
if ($height == 'auto' && $width == 'auto' )
return false;
$o_wd = imagesx($image);
$o_ht = imagesy($image);
$x = $y = 0;
if( $height == 'auto' )
$height = round( ( $width / $o_wd ) * $o_ht );
else if ( $width == 'auto' )
$width = round( ( $height / $o_ht ) * $o_wd );
else if ($width && !$height) {
//make square image
$height = $width;
if($o_wd> $o_ht) {
$x = ceil(($width - $height) / 2 );
$o_wd = $o_ht;
} elseif($o_ht> $o_wd) {
$y = ceil(($height - $width) / 2);
$o_ht = $o_wd;
}
} else
{
$w = round($o_wd * $height / $o_ht);
$h = round($o_ht * $width / $o_wd);
if( ($height-$h) < ($width-$w) )
$width =& $w;
else
$height =& $h;
}
$tmp = imageCreateTrueColor( $width, $height );
imagecopyresampled($tmp, $image, 0, 0, $x, $y, $width, $height, $o_wd, $o_ht);
if ( $options['format'] == null )
return $tmp;
//
if ( !isset($options['format']) || !in_array($options['format'],array('jpeg','jpg','png','gif'))) {
throw new Exception("Invalid Image Type");
}
if ( $options['format'] == 'jpg' ) {
$options['format'] = 'jpeg';
}
$func = 'image'.strtolower($options['format']);
ob_start();
$func($tmp);
$tmp = ob_get_clean();
return $tmp;
}
}