比例裁切缩略图类

[php]
<?php
/***************************************************************************
*                        miniature.php
*                ------------------------------
*        Date                : Feb 26, 2006
*        Copyright        : (C) 2006 - 剑枫
*        Mail                : egmax@163.com
*
*        作用:缩略图.
*        应用:
                include('miniature.php');
                $vres = miniature('源图_name', '缩图_name',缩图宽,缩图高,维持比例);
                echo $vres;
*
*
***************************************************************************/

function miniature($big_image_name, $new_name, $max_width = 400, $max_height = 400, $resize = 1)
{
        if($temp_img_type = @getimagesize($big_image_name)) {eregi('/([a-z]+)$', $temp_img_type[mime], $tpn); $img_type = $tpn[1];}
        else {eregi('.([a-z]+)$', $big_image_name, $tpn); $img_type = $tpn[1];}
        $all_type = array(
                "jpg"   => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg"  , "exn"=>".jpg"),
                "gif"   => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif"   , "exn"=>".gif"),
                "jpeg"  => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg"  , "exn"=>".jpg"),
                "png"   => array("create"=>"imagecreatefrompng" , "output"=>"imagepng"   , "exn"=>".png"),
                "wbmp"  => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp")
        );

        $func_create = $all_type[$img_type]['create'];
        if(empty($func_create) or !function_exists($func_create)) return false;
        $func_output = $all_type[$img_type]['output'];
        $func_exname = $all_type[$img_type]['exn'];
        $big_image   = $func_create($big_image_name);
        $big_width   = imagesx($big_image);
        $big_height  = imagesy($big_image);
        if($big_width <= $max_width and $big_height <= $max_height) { $func_output($big_image, $new_name.$func_exname); return true; }
        $ratiow      = $max_width  / $big_width;
        $ratioh      = $max_height / $big_height;
        if($resize == 1) {
                if($big_width >= $max_width and $big_height >= $max_height)
                {
                        if($big_width > $big_height)
                        {
                        $tempx  = $max_width / $ratioh;
                        $tempy  = $big_height;
                        $srcX   = ($big_width - $tempx) / 2;
                        $srcY   = 0;
                        } else {
                        $tempy  = $max_height / $ratiow;
                        $tempx  = $big_width;
                        $srcY   = ($big_height - $tempy) / 2;
                        $srcX   = 0;
                        }
                } else {
                        if($big_width > $big_height)
                        {
                        $tempx  = $max_width;
                        $tempy  = $big_height;
                        $srcX   = ($big_width - $tempx) / 2;
                        $srcY   = 0;
                        } else {
                        $tempy  = $max_height;
                        $tempx  = $big_width;
                        $srcY   = ($big_height - $tempy) / 2;
                        $srcX   = 0;
                        }
                }
        } else {
                $srcX      = 0;
                $srcY      = 0;
                $tempx     = $big_width;
                $tempy     = $big_height;
        }

        $new_width  = ($ratiow  > 1) ? $big_width  : $max_width;
        $new_height = ($ratioh  > 1) ? $big_height : $max_height;
        if(function_exists("imagecopyresampled"))
        {
                $temp_image = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
        } else {
                $temp_image = imagecreate($new_width, $new_height);
                imagecopyresized($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
        }
        $func_output($temp_image, $new_name.$func_exname);
        ImageDestroy($big_image);
        ImageDestroy($temp_image);
        return true;
}
?>
[/php]

按比例产生缩略图,缩小后图片效果不变形,如果超高则纵向截取,如果超长则横向截取。
附件: 您所在的用户组无法下载或查看附件
洗洗更健康.....
请问你这个原图的文件路径怎么设置?
原帖由 phlice 于 2006-3-12 08:20 PM 发表
请问你这个原图的文件路径怎么设置?
源图名字,包含路径,不单独设置路径。
路径可以为
http网址
./相对路径
不可用/这样的路径

[ 本帖最后由 剑枫 于 2006-3-12 08:32 PM 编辑 ]
洗洗更健康.....