Might not be helpful, this this class below could do what you're trying to do quite easily
Code: Select all
$image=new image('something.jpg');
$image->resize('100','100');
$image->save('somewhere.jpg','jpg');
new image() // you can either use a filename or a $_FILE['something'] array from an upload, you'll need to make a 'tmp' directory with write permissions for this upload thing tho
resize(h,w) // height and width, if you don't specify one of them it will work the other out proportionally, you can also use percentages.
output(location,type) //
Code: Select all
<?
class imagetype
{
public $createfunction;
public $outputfunction;
public $contenttype;
function __construct($create,$output,$contenttype)
{
$this->createfunction=$create;
$this->outputfunction=$output;
$this->contenttype=$contenttype;
}
}
class image
{
public $path;
public $type;
public $resource;
private $types;
public $height;
public $width;
function __construct($image)
{
global $application;
$this->types['jpg']=new imagetype('imagecreatefromjpeg','imagejpeg','image/jpeg');
$this->types['gif']=new imagetype('imagecreatefromgif','imagegif','image/gif');
$this->types['png']=new imagetype('imagecreatefrompng','imagepng','image/png');
if(is_array($image) && array_key_exists('tmp_name',$image))
{
move_uploaded_file($image['tmp_name'],'tmp/.$image['name']);
$this->path='tmp/'.$image['name'];
}
else
{
if(is_numeric($image))
{
$resource=new _resource($image);
if(!$resource)
{
return false;
}
$this->path='images/library/'.md5($resource->id).'/'.$resource->filename;
}
else
{
$this->path=$image;
if(!file_exists($this->path))
{
trigger_error('file '.$this->path.' not found');
return false;
}
}
}
$fileextension=explode('.',$this->path);
$this->type=$fileextension[count($fileextension)-1];
if($this->type=='jpeg')
{
$this->type='jpg';
}
if(!$this->types[$this->type])
{
trigger_error('The image type '.$this->type.' is not supported');
return false;
}
eval("\$this->resource= &".$this->types[$this->type]->createfunction."('".$this->path."');");
$this->refreshattributes();
}
function resize($width=0,$height=0)
{
if(stristr($width,'%'))
{
$widthpercent=true;
$width=str_replace('%','',$width);
}
if(stristr($height,'%'))
{
$heightpercent=true;
$height=str_replace('%','',$height);
}
if(!($width && $height))
{
if(!$height)
{
$proportion=$this->height/$this->width;
$height=$width*$proportion;
if(@$widthpercent)
{
$heightpercent=true;
$height=$width;
}
}
else
{
$proportion=$this->width/$this->height;
$width=$height*$proportion;
if(@$heightpercent)
{
$widthpercent=true;
$width=$height;
}
}
}
if(@$heightpercent) $height=$this->height*($height/100);
if(@$widthpercent) $width=$this->width*($width/100);
$temp = imagecreatetruecolor($width, $height);
imagecopyresampled($temp, $this->resource, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
$this->resource=$temp;
$this->refreshattributes();
}
function refreshattributes()
{
$this->height=imagesy($this->resource);
$this->width=imagesx($this->resource);
}
function crop($x,$y,$width,$height)
{
$temp = imagecreatetruecolor($width, $height);
imagecopyresampled($temp, $this->resource, 0, 0, $x, $y, $width, $height, $width, $height);
$this->resource=$temp;
$this->refreshattributes();
}
function save($filename=null,$type=null)
{
if(!$filename)
{
$filename=$this->path;
}
$this->output($type,$filename);
}
function output($type=null,$file=null)
{
if(!$type)
{
$type=$this->type;
}
$output=$this->types[$type];
if(!$output)
{
trigger_error('The image type '.$type.' is not supported');
return false;
}
if(!$file)
{
header("Content-type: ".$output->contenttype);
eval($output->outputfunction."(\$this->resource);");
}else{
eval($output->outputfunction."(\$this->resource,'$file');");
}
}
}
?>
?>