<?php /** * 图片缩略图类,只可以处理'jpg','gif','png'格式 * * @author pphu * */ class image { private $bigimagepath; private $smalllimagepath; private $imagetype = array('jpg','jpeg','jpe','gif','png');public function __construct($bigimagepath='./',$smallimagepath='./') { $this->bigimagepath = $bigimagepath; $this->smalllimagepath = $smallimagepath; } public function image($bigimagepath='./',$smallimagepath='./'){ $this->__construct($bigimagepath,$smallimagepath); }public function setbigimagepath($path){ $this->bigimagepath = $path; } public function setsmallimagepath($path){ $this->smalllimagepath = $path; }public function doimage($big_filename,$small_filename,$length,$quality=75) { $big_filename = $this->bigimagepath.$big_filename; $res = $this->resizeImage($big_filename,$length); if ($res==false) return false; $small_filename = $this->smalllimagepath.$small_filename; return $this->saveImage($res,$small_filename,$quality); }/** * 根据文件扩展名取得图片的类型或false(表示不是图片) * * @param $filename 图片的文件名 */ private function getImageType($filename) { $ext=strtolower(fileext($filename)); if (in_array($ext,$this->imagetype)) return $ext; else return false; }/** * 从给定的文件名创建图片(给定的图片类型只能是jpg、gif或png格式的) * * @param $filename 图片文件名 * @return 图片资源 */ private function imageCreate($filename) { $type=$this->getImageType($filename);
$p=null; //图片资源
switch ($type) { case 'jpg': case 'jpeg': case 'jpe':$p=@imagecreatefromjpeg($filename); break; case 'gif': $p=@imagecreatefromgif($filename); break; case 'png': $p=@imagecreatefrompng($filename); break; default: return false; //只能处理jpg、gif或png格式的图片 }
<?php
$filename="lanhouse234.jpg";
$newfile="div/lanhouse234.jpg";
rename($filename,$newfile);
?>
<?php
/**
* 图片缩略图类,只可以处理'jpg','gif','png'格式
*
* @author pphu
*
*/
class image
{
private $bigimagepath;
private $smalllimagepath;
private $imagetype = array('jpg','jpeg','jpe','gif','png');public function __construct($bigimagepath='./',$smallimagepath='./')
{
$this->bigimagepath = $bigimagepath;
$this->smalllimagepath = $smallimagepath;
}
public function image($bigimagepath='./',$smallimagepath='./'){
$this->__construct($bigimagepath,$smallimagepath);
}public function setbigimagepath($path){
$this->bigimagepath = $path;
}
public function setsmallimagepath($path){
$this->smalllimagepath = $path;
}public function doimage($big_filename,$small_filename,$length,$quality=75)
{
$big_filename = $this->bigimagepath.$big_filename;
$res = $this->resizeImage($big_filename,$length);
if ($res==false)
return false;
$small_filename = $this->smalllimagepath.$small_filename;
return $this->saveImage($res,$small_filename,$quality);
}/**
* 根据文件扩展名取得图片的类型或false(表示不是图片)
*
* @param $filename 图片的文件名
*/
private function getImageType($filename)
{
$ext=strtolower(fileext($filename));
if (in_array($ext,$this->imagetype))
return $ext;
else return false;
}/**
* 从给定的文件名创建图片(给定的图片类型只能是jpg、gif或png格式的)
*
* @param $filename 图片文件名
* @return 图片资源
*/
private function imageCreate($filename)
{
$type=$this->getImageType($filename);
$p=null; //图片资源
switch ($type)
{
case 'jpg':
case 'jpeg':
case 'jpe':$p=@imagecreatefromjpeg($filename); break;
case 'gif': $p=@imagecreatefromgif($filename); break;
case 'png': $p=@imagecreatefrompng($filename); break;
default: return false; //只能处理jpg、gif或png格式的图片
}
return $p;
}/**
* 把图片的长边缩小为length个像素长,短边等比缩小,只能处理jpg、gif、png格式的图片
*
* @param $filename 图片文件名
* @param $length 长边的像素数
*
* @return 图片资源
*/
private function resizeImage($filename,$length)
{
$img = $this->imageCreate($filename); //打开源图片
if ($img==false) return false;
$width=imagesx($img); //宽
$height=imagesy($img); //高
//如果原图片的尺寸比预计的缩略图的尺寸还小,直接返回原图
if ($width<=$length && $height<=$length) return $img;
//等比缩小
if ($width>$height)
{
$w=$length;
$h=($height/$width)*$length;
}
else if($width<$height)
{
$h=$length;
$w=($width/$height)*$length;
}
else
{
//正方形的图片
$w=$h=$length;
} $p=imagecreatetruecolor($w,$h); //新建缩略图
imagecopyresampled($p,$img,0,0,0,0,$w,$h,$width,$height); //缩小
return $p; //返回图片资源
}/**
* 将图片保存为文件
*
* @param $res 图片资源
* @param string $filename 将要保存的文件名
* @param int $quality 图片质量(0-100)只在生成jpg格式的图片时有效
*
* @return 成功,返回true,否则返回false
*/
private function saveImage($res,$filename,$quality=75)
{
//图片类型
$type=self::getImageType($filename);
switch($type)
{
case 'jpe':
case 'jpeg':
case 'jpg': return @imagejpeg($res,$filename,$quality); break;
case 'gif': return @imagegif($res,$filename); break;
case 'png': return @imagepng($res,$filename); break;
default: return false;
}
return false;
}
}
?>
<?php
require_once('image.class.php');$img = new image();//设置缩略图的保存目录,目录必须已经存在
$img->setsmallimagepath('./upload/');//把当前目录下的a.jpg图片生成缩略图,图片名称为aaa.jpg,图片宽、高最大为100px
$img->doimage('ok.jpg','aaa.jpg',100);?>