+ -
当前位置:首页 → 问答吧 → 缩略图处理类(自动比例缩放, 背景颜色指定)

缩略图处理类(自动比例缩放, 背景颜色指定)

时间:2009-11-24

来源:互联网

本帖最后由 hljwxdn 于 2009-11-24 13:13 编辑

去年写的一个图像比例缩略处理类. 发现自己好长时间没来了, 发点东西, 希望对大家能有用.
使用效果: http://www.dewdew.com.cn/category-internal-9

[code]<?php
/**
* 缩略图处理类
*
* author: songfeng   [email protected]  http://hi.baidu.com/ccex
*/
class Songfeng_Image_Ratio
{
        /**
         * 图片大小比例调整
         *
         * @param $filename       图片路径
         * @param $w              目标宽度
         * @param $h             目标高度
         * @param $override       是否覆盖原文件
         * @param $background  是否产生背景, 如果要求产生背景则产生图像是指定的大小, 图片内容居中
         * @param $color            背影色
         */
        public function RatioAdjuct($filename = '', $w = 440, $h = 300, $override = null, $background = null, $color = '0xFFFFFF')
        {
                list ($imgWidth, $imgHeight) = getImageSize($filename);
               
               
                        
                $ratioX = $imgWidth / $w;
                $ratioY = $imgHeight / $h;
               
                if ($ratioX > $ratioY || $ratioX == $ratioY){
                        $dst_w = $w;
                        $dst_h = ceil($imgHeight / $ratioX);
                } else if ($ratioY > $ratioX){
                        $dst_h = $h;
                        $dst_w = ceil($imgWidth / $ratioY);
                }
               

                //判断图片类型
                switch (strtolower(strrchr($filename, '.')))
                {
                        case '.jpg' :
                        case '.jpeg' :
                                $im = imageCreateFromJpeg($filename);
                                break;                        
                        case '.gif' :
                                $im = imageCreateFromGif($filename);
                                break;
                                
                        case '.png' :
                                $im = imageCreateFromPng($filename);        
                }
               
               
                //是否有背景色
                if (null !== $background) {
                        //将背景色转换为十进制的红绿蓝值
                        $dec = hexdec($color);
                        $red =  0xFF & ($dec >> 0x10);
                          $green = 0xFF & ($dec >> 0x8);
                          $blue = 0xFF & $dec;
                                    
                          //居中定位并复制
                          $dst_pos = array ('d_x' => 0, 'd_y' => 0);
                          ($dst_w == $w) ? ( $dst_pos['d_y'] = (($h - $dst_h) / 2) ) : ($dst_pos['d_x'] = (($w - $dst_w) / 2));
               
                          $imBox = imageCreateTrueColor($w, $h);
                        $color_bg = imageColorAllocate($imBox, $red, $green, $blue);
                        imageFill($imBox, 0, 0, $color_bg);
                        imageCopyResized($imBox, $im, $dst_pos['d_x'], $dst_pos['d_y'], 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight);
                } else {
                        $imBox = imageCreateTrueColor($dst_w, $dst_h);
                        imageCopyResized($imBox, $im, 0, 0, 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight);
                }
               
                //不替换源图片
                if (null === $override)
                        $filename = str_replace(strrchr($filename, '.'), '', $filename) . '_thumb.png';
                        
                return imagejpeg($imBox, $filename) ? $filename : false;
        }
}
[/code] ImageRatio.rar (1.18 KB)
下载次数: 25
2009-11-24 12:59

作者: hljwxdn   发布时间: 2009-11-24

等比例支持不?
非等比例呢? 我需要固定的宽高 缩放以后多余的截掉

作者: skxc   发布时间: 2009-11-24

回复 skxc
等比例支持,
非等比例? 你是不是要固定图片的大小, 你可以指定图片的大小并指定背景色, 这样就是固定的大小, 其余的部分会用背景色填充.

作者: hljwxdn   发布时间: 2009-11-24

回复 hljwxdn


原图:
然后生成100*100缩略图
等比例:   非等比例:

作者: skxc   发布时间: 2009-11-24

你填充多余的背景颜色不好看

作者: skxc   发布时间: 2009-11-24

本帖最后由 hljwxdn 于 2009-11-24 14:17 编辑

哦, 我明白你的意思了. 但这样也许会影响原图片内容, 但这也应该是一种需求, 我抽时间把它作为选项加进去.

作者: hljwxdn   发布时间: 2009-11-24

咳咳

作者: 重楼   发布时间: 2009-11-24

不知道超大图可不可以?例如, 长宽:4000x4000px

作者: spaceplus   发布时间: 2009-11-25

好,卡看

作者: fengbo7496   发布时间: 2009-11-25

回复 spaceplus

没有问题.

作者: hljwxdn   发布时间: 2009-11-26

支持学习了,谢谢

作者: greenrock   发布时间: 2009-11-26

嗯,精简,不错。

作者: mailangel123   发布时间: 2009-11-26