<?php
/*
'#################################################################
'# 程序名称：中环CMSV2.0
'# 授权方式：免费软件
'# 作者：善
'# 联系QQ:9118847
'# 作者网站:http://www.ps173.com
'# 发布时间：2015-6-10
'# 声明：欢迎广大的PHP爱好者一起共同探讨
'# 声明：各种用途均可免费使用、自由传播，但必须保留程序代码中的版权信息。
'#################################################################
 */

//图片上传类
class Image{
	protected $new;
	protected $img;
	protected $type;
	protected $width;
	protected $height;

	public function getimage($image){
		if(!empty($image)){
			list($this->width, $this->height, $this->type) = getimagesize($image);
			$this->img = $this->outimg($image, $this->type);
		}
	}

	public function kindthumb($srcfile, $width, $height, $thumb){
		$srcinfo = getimagesize(ZH_PATH.$srcfile);
		$thumb = ZH_PATH.$thumb;
		$img = $this->outimg(ZH_PATH.$srcfile, $srcinfo[2]);
		$srcwidth = imagesx($img);
		$srcheight = imagesy($img);

        if(empty($width) && empty($height)){
            $width = $srcwidth;
            $height = $srcheight;
        }

        if(!is_numeric($width) || !is_numeric($height)){
            $width = $srcwidth;
            $height = $srcheight;
        }

        $new_width=$width;
        $new_height=$height;

        if($srcwidth < $srcheight){
            $width = ($height / $srcheight) * $srcwidth;
        }else{
            $height = ($width / $srcwidth) * $srcheight;
        }

        $new = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($new, $img, 0, 0, 0, 0, $new_width, $new_height, $srcwidth, $srcheight);

		switch ($srcinfo[2]) {
			case 1:
				imagegif($new, $thumb);
				break;
			case 2:
				imagejpeg($new, $thumb);
				break;
			case 3:
				imagejpeg($new, $thumb);
				break;
			
		}

		imagedestroy($new);
		imagedestroy($img);
	}

	public function outimg($name, $type){
		switch ($type) {
			case 1:
				$img = imagecreatefromgif($name);
				break;
			case 2:
				$img = imagecreatefromjpeg($name);
				break;
			case 3:
				$img = imagecreatefrompng($name);
				break;
			default:
				return false;
				break;
		}
		return $img;
	}

}


?>