/** - * 创建图片缩略图,成功返回真
- *
- * @param string $cat 目录
- * @param string $oldname 原图文件名
- * @param string $newname 新图文件名
- * @param int $width 缩略图宽
- * @param int $height 缩略图高
- * @return
- */
- function thumb($cat,$oldname,$newname,$width=160,$height=120){
- $srcFile = $cat. "/" .$oldname;
- $data = getimagesize($srcFile);
- $dscFile = $cat. "/". $newname;
switch ($data[2]) { - case 1:
- $im = imagecreatefromgif($srcFile);
- break;
case 2: - $im = imagecreatefromjpeg($srcFile);
- break;
case 3: - $im = imagecreatefrompng($srcFile);
- break;
- }
$srcW=imagesx($im); - $srcH=imagesy($im);
if(($srcW/$width)>=($srcH/$height)){ - $temp_height=$height;
- $temp_width=$srcW/($srcH/$height);
- $src_X=abs(($width-$temp_width)/2);
- $src_Y=0;
- }
- else{
- $temp_width=$width;
- $temp_height=$srcH/($srcW/$width);
- $src_X=0;
- $src_Y=abs(($height-$temp_height)/2);
- }
$temp_img=imagecreatetruecolor($temp_width,$temp_height); - imagecopyresized($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH);
$ni=imagecreatetruecolor($width,$height); - imagecopyresized($ni,$temp_img,0,0,$src_X,$src_Y,$width,$height,$width,$height);
- $cr = imagejpeg($ni,$dscFile);
if ($cr){ - chmod($dscFile, 0777);
- return true;
- }
- }
- ?>
|