Agent Smith
Сообщений: 976
Оценки: 0
Присоединился: 2007-04-10 21:56:49.593333
|
Ему до движка, как свинье до неба. Вот я наваял. <?
class Downloader{
var $smwidth=120;
var $smheight=120;
var $bigwidth=550;
var $bigheight=550;
var $min_width=300;
var $min_height=300;
var $quality=100;
var $small_tumb=true;
var $wotermark = false;
var $wotermark_file="";
var $mark_x_coord=-20;
var $mark_y_coord=-20;
var $datadir="foto";
var $error="";
var $max_weight=0;
var $fileinfo="";
var $output_type="0";
var $filename="0";
function Downloader($params=array())
{
}
function error($error)
{ if (!is_array($this->error))
{
$this->error=array($error);
}
else
{
$this->error=array_merge($error, $this->error);
}
return false;
}
function initialise($vars)
{ if (count($vars)!="0")
foreach($vars as $key=>$value)
{
$this->$key=$value;
}
}
function new_name($ext)
{
for ($n=48,$chars=array();$n<=122;$n++)
{if (($n>=48 and $n<=57) or ($n>=65 and $n<=90) or ($n>=97 and $n<=122))
{$chars[]=chr($n);}}
do
{for ($cnt=count($chars)-1,$rend=0,$somename="";$rend<=16;$rend++)
{$result=mt_rand(0,$cnt);
$somename=$somename.$chars[$result];
}
$name=$somename.".".$ext;
}
While(is_file($this->datadir.$name));
return $name;
}
function upload($file, $id=null)
{
if ($id==null)
{$up_filename=$_FILES['file']['name'];
$up_tmpfilename=$_FILES['file']['tmp_name'];
}
else
{$up_filename=$_FILES['file']['name'][$id];
$up_tmpfilename=$_FILES['file']['tmp_name'][$id];
}
if (isset($up_filename) and $up_filename!="")
{$this->datadir=rtrim($this->datadir, "/\\");
if (!is_dir($this->datadir))
{if (!mkdir($this->datadir,777))
return $this->error(_LANG("note_dir_is_not_exist"));
}
$this->datadir .="/";
$mimetype=exif_imagetype($up_tmpfilename);
switch ($mimetype)
{case IMAGETYPE_GIF:
$ext="gif";
break;
case IMAGETYPE_JPEG:
$ext="jpg";
break;
case IMAGETYPE_PNG:
$ext="png";
break;
default:
return $this->error(_LANG("note_not_valid"));
}
if (!$size=getimagesize($up_tmpfilename))
{return $this->error(_LANG("note_no_size"));}
if (!$fotoksize=filesize($up_tmpfilename))
{return $this->error(_LANG("note_no_size"));}
If ($this->max_weight>0 and ($fotoksize/1048576)>$this->max_weight)
{return $this->error(sprintf(_LANG("note_file_limit_error"), $this->max_weight));}
if ($this->min_width>0 and intval($size[0])<$this->min_width)
{return $this->error(sprintf(_LANG("note_file_width_error"), $this->min_width));}
if ($this->min_height>0 and intval($size[1])<$this->min_height)
{return $this->error(sprintf(_LANG("note_file_height_error"), $this->min_height));}
$foto=$this->datadir.$this->new_name ($ext);
if (!copy($up_tmpfilename, $foto))
{return $this->error(_LANG("note_file_server_error"));}
$fotoname=$this->resize($foto, $ext);
@unlink ($foto);
return $fotoname;
}
else
{$this->error(_LANG("note_file_uploadding_error"));}
}
function resize($file, $ext)
{list($width, $height) = $this->ration($file, $this->bigwidth, $this->bigheight);
$smallfoto=$this->new_name($ext);
$this->filename = $smallfoto;
$to=$this->datadir.$smallfoto;
$result=$this->get_image($file, $to, $width, $height, $ext, 1);
if (!$result)
{return $result;}
elseif($this->small_tumb)
{list($width, $height) = $this->ration($file, $this->smwidth, $this->smheight);
$result=$this->get_image($file, $this->datadir."tumb".$smallfoto, $width, $height, $ext, 0);
if (!$result)
{return $result;}
else
{$this->fileinfo=getimagesize($to);
@unlink($file);
return true;}
}
}
function ration ($file, $re_width, $re_height)
{list($width, $height)=getimagesize($file);
$ratio_h=$re_height/$height;
$ratio_w=$re_width/$width;
if ($ratio_h>=1 and $ratio_w>=1)
{$sm_width=$width;
$sm_height=$height;
}
else
{if ($ratio_h<$ratio_w)
{
$sm_height=$re_height;
$sm_width=intval($ratio_h*$width);
}
else
{
$sm_width=$re_width;
$sm_height=intval($ratio_w*$height);
}
}
return array($sm_width, $sm_height);
}
function get_image($file, $distination, $sm_width, $sm_height, $ext, $copyright=0)
{list($width, $height)=getimagesize($file);
switch ($ext)
{case "gif": if (!$im=ImageCreateFromGif($file)) return $this->error(_LANG("note_file_processing"));break;
case "jpg": if (!$im=ImageCreateFromJpeg($file)) return $this->error(_LANG("note_file_processing"));break;
case "png": if(!$im=ImageCreateFromPng($file)) return $this->error(_LANG("note_file_processing"));
}
$dast = imagecreatetruecolor($sm_width, $sm_height);
$tc = imageColorClosest($dast,0,255,0);
imagecolorTransparent($dast, $tc);
if (!imagecopyresampled($dast, $im, 0, 0, 0, 0, $sm_width, $sm_height, $width, $height))
{
@unlink($file);
return $this->error(_LANG("note_file_processing"));
}
if ($copyright and $this->wotermark_file!="")
{if (is_file($this->wotermark_file))
{$mimetype=exif_imagetype($this->wotermark_file);
switch ($mimetype)
{case IMAGETYPE_GIF: if (!$wm=ImageCreateFromGif($this->wotermark_file)) return $this->error(_LANG("note_file_processing"));break;
case IMAGETYPE_JPEG: if (!$wm=ImageCreateFromJpeg($this->wotermark_file)) return $this->error(_LANG("note_file_processing"));break;
case IMAGETYPE_PNG: if (!$wm=ImageCreateFromPng($this->wotermark_file)) return $this->error(_LANG("note_file_processing"));
}
if (isset($im))
{$wm_image=getimagesize($this->wotermark_file);
if ($wm_image[0]!=0 and $wm_image[1]!=0)
{if ($this->mark_x_coord<0)
{$x=$sm_width-$wm_image[0]+$this->mark_x_coord;}
else
{$x=$this->mark_x_coord;}
if ($this->mark_y_coord<0)
{$y=$sm_height-$wm_image[1]+$this->mark_y_coord;}
else
{$y=$this->mark_y_coord;}
if (!imagecopyresampled($dast, $wm, $x, $y, 0, 0, $wm_image[0], $wm_image[1], $wm_image[0], $wm_image[1]))
{
@unlink($file);
return $this->error(_LANG("note_file_processing"));
}
}
}
}
}
if ($this->output_type!=0)
$ext=$this->output_type;
switch ($ext)
{
case "gif": imagegif ($dast,$distination);break;
case "jpg": imageJpeg($dast,$distination, $this->quality);break;
case "png": imagepng ($dast,$distination);break;
default: imageJpeg($dast,$distination, $this->quality);break;
}
imageDestroy($im);
imageDestroy($dast);
return true;
}
}
?>
|