修改文件phpcms/libs/classes/image.class.php的thumb()方法,添加了一个参数$per来控制剪切的部位。
修改成如下
<?php function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 200, $suffix='', $autocut = 0, $ftp = 0 ,$per = 1) { if(!$this->thumb_enable || !$this->check($image)) return false; $info = image::info($image); if($info === false) return false; $srcwidth = $info['width']; $srcheight = $info['height']; $pathinfo = pathinfo($image); $type = $pathinfo['extension']; if(!$type) $type = $info['type']; $type = strtolower($type); unset($info); if(!$maxheight)$maxheight = $maxwidth*$srcheight/srcwidth; $creat_arr = $this->getpercent($srcwidth,$srcheight,$maxwidth,$maxheight); $createwidth = $width = $creat_arr['w']; $createheight = $height = $creat_arr['h']; $psrc_x = $psrc_y = 0; if($autocut && $maxwidth > 0 && $maxheight > 0) { if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) { $width = $maxheight/$height*$width; $height = $maxheight; if($per)$psrc_x = ($width - $maxwidth)*$srcheight/$height*$per/2; //$per=0剪切开头,1中间,2结尾 }elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) { $height = $maxwidth/$width*$height; $width = $maxwidth; if($per)$psrc_y = ($height - $maxheight)*$srcwidth/$width*$per/2; } $createwidth = $maxwidth; $createheight = $maxheight; } $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); $srcimg = $createfun($image); if($type != 'gif' && function_exists('imagecreatetruecolor')) $thumbimg = imagecreatetruecolor($createwidth, $createheight); else $thumbimg = imagecreate($createwidth, $createheight); //原参数($width, $height)即使设置剪切图片、gif图片仍然不剪切 if(function_exists('imagecopyresampled')) imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight); //else // imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight); if($type=='gif' || $type=='png') { $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 指派一个绿色 imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图 } if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace); $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type); if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; $imagefun($thumbimg, $filename,100); $this->watermark($filename); //缩微图加水印 imagedestroy($thumbimg); imagedestroy($srcimg); if($ftp) { @unlink($image); } return $filename; } ?>
说明:方法添加了一个参数$per,来控制剪切的部位,如果$per=0剪切开头,1中间,2结尾,如果是宽图片那么0从左边剪切,1剪切中间,2从右边剪切。如果是长条图片,0从上开始剪切,1从中间,2从下边。
设置$per参数时,$autocut参数(是否对图片剪切,原有参数)必须设置为1.
其中修改以下代码中的100为需要设置的图片质量,如果不需要设置图片质量,把方法代码中的两行注释去掉,以及把以下代码中的100和前面逗号去掉。
$imagefun($thumbimg, $filename,100)