-
-
/** - * Create image thumbnail, return true if successful
- *
- * @param string $cat directory
- * @param string $oldname original image file name
- * @param string $newname new image file name
- * @param int $width abbreviation Thumbnail width
- * @param int $height Thumbnail 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;
- }
- }
- ?>
-
复制代码
|