<?PHP
class BSResizeImage
{
var $webappcfg;
function BSResizeImage()
{
global $webappcfg;
$this->webappcfg = $webappcfg;
}
function resizeimage($_request, $_response)
{
// Required Request parameter
// $_request['towidth'] = ??? px
// $_request['toheight'] = ??? px
// $_request['constraint'] = true / false
if (isset($_request['imglist']))
{
for ($i=0; $i<count($_request['imglist']); $i++)
{
// $_request['imglist'] ->
// ['filename'], ['contenttype']
// Calculate new size
$timage = $_request['imglist'][$i];
if (!isset($timage['contenttype'])
|| !isset($timage['filename']))
{
continue;
}
$tfiledirdb = $timage['filedirdb'];
$tobj = $tfiledirdb->getobj();
if (!is_dir($this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail']))
mkdir($this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail']);
if (!is_dir($this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail'] . "/" .$tfiledirdb->getownerid()))
mkdir($this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail'] . "/" .$tfiledirdb->getownerid());
list($img_type, $img_ext) = explode("/",
$timage['contenttype']);
if (!is_file($this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail']."/".
$tobj->getlocation()))
{
list($orgwidth, $orgheight) = getimagesize($timage['filename']);
if ($_request['constraint']==true)
{
if ($_request['towidth']/$orgwidth*$orgheight<$_request['toheight'])
{
$newwidth = $_request['towidth'];
$newheight = $_request['towidth']/$orgwidth*$orgheight;
}
else
{
$newheight = $_request['toheight'];
$newwidth = $_request['toheight']/$orgheight*$orgwidth;
}
}
else
{
$newwidth = $_request['towidth'];
$newheight = $_request['toheight'];
}
switch ($img_ext)
{
case 'gif':
case 'jpeg':
case 'jpg':
case 'pjpeg':
case 'png':
{
$command = $this->webappcfg['imagemagickpath'].
"/convert -sample ".$newwidth."x".$newheight." ".
$this->webappcfg['filestorage'] . "/".
$tobj->getlocation() . " " .
$this->webappcfg['filestorage'] . "/".
$this->webappcfg['thumbnail'] . "/" .
$tobj->getlocation();
exec($command);
break;
}
default:
{
$_response['result'][$i] = false;
$_response['error'][$i] = "INVALIDIMAGETYPE";
return $_response;
break;
}
}
}
// save to $_response['resizedimglist']
$_response['resizedimglist'][$i]['filepath'] = $this->webappcfg['filestorage'] .
"/".$this->webappcfg['thumbnail']."/".
$tobj->getlocation();
$_response['resizedimglist'][$i]['contenttype'] = $timage['contenttype'];
$_response['result'][$i] = true;
}
return $_response;
}
}
}
?>