public class uploadimg{
public static function Uploadimg($dir='shops'){
$file = new Zend_File_Transfer_Adapter_Http();if ($file->isUploaded()) { //是否有文件上传
$_date = date('Ymd');
$img_size = Zend_Registry::get('BASECON');//获取配置参数
if($dir=='shops'){
$imgsize = $img_size['imgsize'];
}else{
$imgsize = $img_size[$dir];
}
$path = 'uploads/'.$dir. '/'; //保存路径
$small_dir = $path.'small_img/'.$_date;
$middle_dir = $path.'middle_img/'.$_date;
$big_dir = $path.'big_img/'.$_date;
$source_dir = $path.'source_img/'.$_date.'/';
$folder = new Zend_Search_Lucene_Storage_Directory_Filesystem($small_dir);
$folder1 = new Zend_Search_Lucene_Storage_Directory_Filesystem($middle_dir);
$folder2 = new Zend_Search_Lucene_Storage_Directory_Filesystem($big_dir);
$folder3 = new Zend_Search_Lucene_Storage_Directory_Filesystem($source_dir);
$file_name = $file->getFileName(null, false);
$ext = explode('.',$file_name);
$extname = $ext[count($ext)-1];
$newname = time().rand(100,200).'.'.$extname;
$file->addFilter('Rename', array('target' => $source_dir . $newname));
$file->setDestination($source_dir);
$file->addValidator('Extension', FALSE, array('jpg', 'gif', 'png', 'jpeg'));
if (!$file->receive()) { //执行上传
return false;
exit;
}else {
$source_file = $source_dir.$newname;
$small=self::show_pic_scal($imgsize['small'][0], $imgsize['small'][1], $source_file);
self::resize($source_file,$small[0],$small[1],$small_dir);
$middle=self::show_pic_scal($imgsize['middle'][0], $imgsize['middle'][1], $source_file);
self::resize($source_file,$middle[0],$middle[1],$middle_dir);
$big=self::show_pic_scal($imgsize['big'][0], $imgsize['big'][1], $source_file);
self::resize($source_file,$big[0],$big[1],$big_dir);
return $_date.'/'.$newname;
}
}
}
//更改图片大小比例
public static function show_pic_scal($width, $height, $picpath) {
$imginfo = GetImageSize ( $picpath );
$imgw = $imginfo [0];
$imgh = $imginfo [1];
$ra = number_format ( ($imgw / $imgh), 1 ); //宽高比
$ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比
if ($imgw > $width or $imgh > $height) {
if ($imgw > $imgh) {
$newWidth = $width;
$newHeight = round ( $newWidth / $ra );
} elseif ($imgw < $imgh) {
$newHeight = $height;
$newWidth = round ( $newHeight / $ra2 );
} else {
$newWidth = $width;
$newHeight = round ( $newWidth / $ra );
}
} else {
$newHeight = $imgh;
$newWidth = $imgw;
}
$newsize [0] = $newWidth;
$newsize [1] = $newHeight;
return $newsize;
}
//获取图片信息
public static function getImageInfo($src)
{
return GetImageSize($src);
}
//创建新图片
public static function create($src)
{
$info=self::getImageInfo($src);
switch ($info[2])
{
case 1:
$im=imagecreatefromgif($src);
break;
case 2:
$im=imagecreatefromjpeg($src);
break;
case 3:
$im=imagecreatefrompng($src);
break;
}
return $im;
}
//重新定义图片大小
public static function resize($src,$w,$h,$path)
{
$temp=pathinfo($src);
$name=$temp["basename"];//文件名
// $dir=$temp["dirname"];//文件所在的文件夹
$extension=$temp["extension"];//文件扩展名
$savepath="{$path}/{$name}";
$info=self::getImageInfo($src);
$width=$info[0];
$height=$info[1];
$per1=round($width/$height,2);
$per2=round($w/$h,2);
//计算缩放比例
if($per1>$per2||$per1==$per2)
{
$per=$w/$width;
}
if($per1<$per2)
{
$per=$h/$height;
}
$temp_w=intval($width*$per);
$temp_h=intval($height*$per);
$temp_img=@imagecreatetruecolor($temp_w,$temp_h);
$im=self::create($src);
if($width>300){
//加水印
$w_info=self::getImageInfo('uploads/w_img.png');
$w_width=$w_info[0];
$w_height=$w_info[1];
$w_img=self::create('uploads/w_img.png');
imagecopy($im,$w_img,$width-$w_width,$height-$w_height,0,0,$w_width,$w_height);
@imagedestroy($w_img);
}
@imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
if($per1>$per2)
{
@imagejpeg($temp_img,$savepath, 100);
@imagedestroy($im);
return self::addBg($savepath,$w,$h,"w");
}
if($per1==$per2)
{
@imagejpeg($temp_img,$savepath, 100);
@imagedestroy($im);
return self::addBg($savepath,$w,$h,"h");//$savepath;
//等比缩放
}
if($per1<$per2)
{
@imagejpeg($temp_img,$savepath, 100);
@imagedestroy($im);
return self::addBg($savepath,$w,$h,"h");
}
}
public static function addBg($src,$w,$h,$fisrt="w")
{
$bg=imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);
$info=self::getImageInfo($src);
$width=$info[0];
$height=$info[1];
$img=self::create($src);
if($fisrt=="wh")
{
//等比缩放
return $src;
}
else
{
if($fisrt=="w")
{
$x=0;
$y=($h-$height)/2;//垂直居中
}
if($fisrt=="h")
{
$x=($w-$width)/2;//水平居中
$y=0;
}
@imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
@imagejpeg($bg,$src,100);
@imagedestroy($bg);
@imagedestroy($img);
return $src;
}
}
}