欢迎来到酷熊网(kubear.cn)!

phpcms v9 缩略图特色图居中剪切


                       phpcms v9默认的缩略图不是居左剪切,phpcms缩略图居中剪切会保证图片的质量以及网站整体的美观。

修改文件phpcms/libs/classes/image.class.php的thumb()方法,添加了一个参数$per来控制剪切的部位。

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 200, $suffix='', $autocut = 0, $ftp = 0 ,$per = 0) {
                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=$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);
                $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.

如果你搞不懂在哪修改的请下载 image.class.php 

百度网盘下载地址:
http://pan.baidu.com/s/1eQJ5bKe

提取密码:2lan


                                   
           

本文为酷熊网原创/或者整理的文章,转载请注明出处!
本文链接:https://www.kubear.cn/blog-48.html by 酷熊网