srcimg = $srcimg; $this->resizeWidth = $resizewidth; $this->resizeHeight = $resizeheight; $this->cut = $cut; $this->usecache = $usecache; $this->hash = $hash; //图片的类型 $this->type = 'jpg';//所有的缩略图都为jpg后缀 //$this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); //目标图象地址 $this -> dstImg($dstrootdir); if ($this->usecache && file_exists($this->dstimg)){ return true; } else { //初始化图象 $this->initImg(); //尺寸 $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成图象 $this->newImg(); ImageDestroy($this->im); } } //返回缩略图相关信息 function getDstImgInfo() { $info = getimagesize($this->dstimg); $array = array('width'=>$info[0],'height'=>$info[1],'type'=>$this->type,'dstimg'=>$this->dstimg); return $array; } private function newImg() { //改变后的图象的比例 $resize_ratio = ($this->resizeWidth)/($this->resizeHeight); //实际图象的比例 $ratio = ($this->width)/($this->height); if(($this->cut)=="1") //裁图 { if($ratio>=$resize_ratio) //高度优先 { $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth,$this->resizeHeight, (($this->height)*$resize_ratio), $this->height); $this->createImg($newimg); } if($ratio<$resize_ratio) //宽度优先 { $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->width, (($this->width)/$resize_ratio)); $this->createImg($newimg); } } else if($this->cut==2)//取最核心的中间内容 { if($this->width>=$this->resizeWidth)//如果原图宽度大于目标宽度 { //目标偏移量 $src_y = 0; $src_x = 0; //以原图比例为基础,宽度优先计算出的缩略目标图尺寸 $resize_width = $this->resizeWidth; $resize_height = ceil($this->resizeWidth/$ratio); //原图以宽度优先,按照缩略图宽高比计算出的高度 $cal_height = ceil($this->width/$resize_ratio); if($this->height>$cal_height)//缩略时进行位置偏移 { $src_y = ceil(($this->height-$cal_height)/2); $final_height = $cal_height; $final_width= $this->width; } else//原图高度不足 { $resize_height = $this->height; $resize_width = ceil($resize_height*$resize_ratio); //原图以高度优先,按照缩略图宽高比计算出的宽度 $cal_width = ceil($this->height*$resize_ratio); //位置偏移 $src_x = ceil(($this->width-$cal_width)/2); $final_width = $cal_width; $final_height = $this->height; } $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); imagefill($newimg, 0, 0, 0xFFFFFF); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); $dst_x = $dst_y = 0; if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;} if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;} imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $this->resizeWidth, $this->resizeHeight,$final_width,$final_height); $this->createImg($newimg); } else { //目标偏移量 $src_y = 0; $src_x = 0; if($this->height>=$this->resizeHeight)//如果原图高度大于目标高度 { //以原图比例为基础,高度优先计算出的缩略目标图宽度 $resize_width = ceil($this->resizeHeight*$ratio); $resize_height = $this->resizeHeight; $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);imagefill($newimg, 0, 0, 0xFFFFFF); $dst_x = $dst_y = 0; if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;} if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;} imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $resize_width, $resize_height,$this->width,$this->height); $this->createImg($newimg); } else { $src_y = 0; $src_x = 0; $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);imagefill($newimg, 0, 0, 0xFFFFFF); $dst_x = $dst_y = 0; $resize_width = $this->width; $resize_height = $this->height; if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;} if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;} imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $resize_width, $resize_height,$this->width,$this->height); $this->createImg($newimg); } } } else if($this->cut==3) { if($ratio>=$resize_ratio) { $newimg = imagecreatetruecolor($this->resizeWidth,($this->resizeWidth)/$ratio); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, ($this->resizeWidth)/$ratio, $this->width, $this->height); $this->createImg($newimg); $this->srcimg = $this->dstimg; $this->initImg(); $this->width = imagesx($this->im); $this->height = imagesy($this->im); $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); $whites = imagecolorallocate($newimg,255,255,255); imagefill($newimg,0,0,$whites); $dst_x = 0; $dst_y = ($this->resizeHeight-$this->height)/2; imagecopy($newimg, $this->im, $dst_x, $dst_y, 0, 0, $this->width, $this->height); $this->createImg($newimg); } if($ratio<$resize_ratio) { $newimg = imagecreatetruecolor(($this->resizeHeight)*$ratio,$this->resizeHeight); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resizeHeight)*$ratio, $this->resizeHeight, $this->width, $this->height); $this->createImg($newimg); $this->srcimg = $this->dstimg; $this->initImg(); $this->width = imagesx($this->im); $this->height = imagesy($this->im); $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); $whites = imagecolorallocate($newimg,255,255,255); imagefill($newimg,0,0,$whites); $dst_x = ($this->resizeWidth-$this->width)/2; $dst_y = 0; imagecopy($newimg, $this->im, $dst_x, $dst_y, 0, 0, $this->width, $this->height); $this->createImg($newimg); } } else { if($ratio>=$resize_ratio) { $newimg = imagecreatetruecolor($this->resizeWidth,($this->resizeWidth)/$ratio); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, ($this->resizeWidth)/$ratio, $this->width, $this->height); $this->createImg($newimg); } if($ratio<$resize_ratio) { $newimg = imagecreatetruecolor(($this->resizeHeight)*$ratio,$this->resizeHeight); $color=imagecolorallocate($newimg,255,255,255); imagecolortransparent($newimg,$color); imagefill($newimg,0,0,$color); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resizeHeight)*$ratio, $this->resizeHeight, $this->width, $this->height); $this->createImg($newimg); } } } //初始化图象 private function initImg() { $srcInfo = getimagesize($this->srcimg); switch ($srcInfo[2]) { case 1: $this->im = imagecreatefromgif($this->srcimg); break; case 2: $this->im = imagecreatefromjpeg($this->srcimg); break; case 3: $this->im = imagecreatefrompng($this->srcimg); break; case 6: $this->im = imagecreatefrombmp($this->srcimg); break; default: die('Unsupport the file type'); exit(); } } //生成目标文件 private function createImg(&$newimg) { $srcInfo = @getimagesize($this->srcimg); switch ($srcInfo[2]) { case 1: imagegif($newimg,$this->dstimg); break; case 2: imagejpeg($newimg,$this->dstimg); break; case 3: imagepng($newimg,$this->dstimg); break; case 6: file_put_contents($this->dstimg,file_get_contents($this->srcimg)); break; default: die('unsupport file type'); exit(); } imagedestroy($newimg); } //图象目标地址 private function dstImg($dstrootdir) { /* *以源文件名基础,获取缩略图文件名 $basename = basename($this->srcimg); $this->filename = substr($basename,strrpos($basename,DIRECTORY_SEPARATOR)+1,0-(strlen($basename)-strpos($basename,'.'))); if (strlen($this->filename)<4){return false;} */ $this->filename = $this->hash; if ($this->resizeWidth<=0 || $this->resizeHeight<=0){return false;} if(!empty($dstrootdir)) { $this->dstimg = rtrim($dstrootdir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->resizeWidth.'_'.$this->resizeHeight.'_'.$this->filename.'.'.$this->type; } else { $this->dstimg = $this->resizeWidth.'_'.$this->resizeHeight.'_'.$this->filename.'.'.$this->type; } } } function imagecreatefrombmp($p_sFile) { $file = fopen($p_sFile,"rb"); $read = fread($file,10); while(!feof($file)&&($read<>"")) $read .= fread($file,1024); $temp = unpack("H*",$read); $hex = $temp[1]; $header = substr($hex,0,108); if (substr($header,0,4)=="424d") { $header_parts = str_split($header,2); $width = hexdec($header_parts[19].$header_parts[18]); $height = hexdec($header_parts[23].$header_parts[22]); unset($header_parts); } $x = 0; $y = 1; $image = imagecreatetruecolor($width,$height); $body = substr($hex,108); $body_size = (strlen($body)/2); $header_size = ($width*$height); $usePadding = ($body_size>($header_size*3)+4); for ($i=0;$i<$body_size;$i+=3) { if ($x>=$width) { if ($usePadding) $i += $width%4; $x = 0; $y++; if ($y>$height) break; } $i_pos = $i*2; $r = hexdec($body[$i_pos+4].$body[$i_pos+5]); $g = hexdec($body[$i_pos+2].$body[$i_pos+3]); $b = hexdec($body[$i_pos].$body[$i_pos+1]); $color = imagecolorallocate($image,$r,$g,$b); imagesetpixel($image,$x,$height-$y,$color); $x++; } unset($body); return $image; } ?>