<?
function changeimageshowsize($imageurl,$maxwidth,$maxheight)//设定要压缩成的图片的宽,高并返回
{
$image_size=getimagesize($imageurl);
$image_width = $image_size[0];
$image_height = $image_size[1];
$reti = 1;
if($maxwidth>$image_width && $maxheight>$image_height)
{
$reti = 0;
$twidth =$image_width;
$theight =$image_height;
}
if($reti)
{
if($image_width/$maxwidth>=$image_height/$maxheight)
{
$twidth = $maxwidth;
$theight = $image_height/($image_width/$maxwidth);
}
else
{
$twidth = $image_width/($image_height/$maxheight);
$theight = $maxheight;
}
}
return $twidth.",".$theight;
}
//$srcfile 源图片路径
//$dir目标文件夹
//$thumbwidth,$thumbheight,要压缩成的宽高,可以使用上述等比例缩放计算的结果$aa=$modelstore->changeimageshowsize($screen[img_a],300,225);$aa=explode(',',$aa);
//$dir='img/upload/2010-08-19/';$cc=$modelstore->makethumb($screen[img_a],$dir,$aa[0],$aa[1],0);
function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0)
{
//判断文件是否存在
if (!file_exists($srcfile))return false;
//生成新的同名文件,但目录不同
$imgname=explode('/',$srcfile);
$arrcount=count($imgname);
$dstfile = $dir.$imgname[$arrcount-1];
//缩略图大小
$tow = $thumbwidth;
$toh = $thumbheight;
if($tow < 40) $tow = 40;
if($toh < 45) $toh = 45;
//获取图片信息
$im ='';
if($data = getimagesize($srcfile)) {
if($data[2] == 1) {
$make_max = 0;//gif不处理
if(function_exists("imagecreatefromgif")) {
$im = imagecreatefromgif($srcfile);
}
} elseif($data[2] == 2) {
if(function_exists("imagecreatefromjpeg")) {
$im = imagecreatefromjpeg($srcfile);
}
} elseif($data[2] == 3) {
if(function_exists("imagecreatefrompng")) {
$im = imagecreatefrompng($srcfile);
}
}
}
if(!$im) return '';
$srcw = imagesx($im);
$srch = imagesy($im);
$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh){
$ftow = $tow;
$ftoh = $ftow*($srch/$srcw);
} else {
$ftoh = $toh;
$ftow = $ftoh*($srcw/$srch);
}
if($ratio){
$ftow = $tow;
$ftoh = $toh;
}
//缩小图片
if($srcw > $tow || $srch > $toh || $ratio) {
if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
} else {
return '';
}
if(function_exists('imagejpeg')) {
imagejpeg($ni, $dstfile);
} elseif(function_exists('imagepng')) {
imagepng($ni, $dstfile);
}
}else {
//小于尺寸直接复制
copy($srcfile,$dstfile);
}
imagedestroy($im);
if(!file_exists($dstfile)) {
return '';
} else {
return $dstfile;
}
}
?>