首页 > php教程 > php手册 > 正文

按比例生成缩略图文件及显示缩略图

WBOY
发布: 2016-06-21 09:06:29
原创
1046 人浏览过

缩略图|显示

   
    function CreateThumbnail($path,$newFileName,$width=100,$height=100)
    {
        @$s_img = getimagesize($path);
        @$type  = $s_img[2];
       
        switch($type)
        {
            case 1 : @$im = imagecreatefromgif($path);break;
            case 2 : @$im = imagecreatefromjpeg($path); break;
            case 3 : @$im = imagecreatefrompng($path);break;
            default: $im  = false;
            break;
        }
       
        @$s_width  = imagesx($im);
        @$s_height = imagesy($im);
       
        $sizexy = getScaleImage($path,$width,$height);
        $width  = $sizexy[0];
        $height = $sizexy[1];
       
        if($im)
        {
            $im2 = imagecreatetruecolor($width,$height);
            imagecopyresized ($im2,$im,0,0,0,0,$width,$height,$s_width,$s_height);
            imagejpeg($im2,$newFileName);
        }
        return false;
    }
   
    function getScaleImage($image,$perfectWidth,$perfectHeight)
    {
        if (realpath($image) != false)
        {
            $sizeArray = getimagesize($image);
            $sizeX     = $sizeArray[ 0 ] ;
            $sizeY     = $sizeArray[ 1 ] ;
               
            if ($perfectWidth!=0 && $perfectHeight==0)
            {
                if ($sizeX>$perfectWidth)
                {
                    $scale = $sizeX/$sizeY ;
                    $sizeX = $perfectWidth;
                    $sizeY = floor($sizeX/$scale);
                } 
            }
       
            if ($perfectHeight != 0 && $perfectWidth == 0)
            {
                if ($sizeY > $perfectHeight)
                {
                    $scale = $sizeY / $sizeX ;
                    $sizeY = $perfectHeight;
                    $sizeX = floor($sizeY / $scale);
                } 
            }
           
            if ($perfectHeight != 0 && $perfectWidth != 0)
            {
                $sizeX = $perfectWidth;
                $sizeY = $perfectHeight;
            }
       
            $tmp[] = $sizeX;
            $tmp[] = $sizeY;
        }
        else
        {
            $tmp[] = 0;
        }
        return $tmp;
    }
   
    $tm = getScaleImage("http://www.pushad.com/Info/100_3353.jpg",0,0);
    echo "按比例生成缩略图文件及显示缩略图";
    foreach ($tm as $v)
    {
        echo $v."
";
    }

    //CreateThumbnail("http://www.pushad.com/Info/100_3353.jpg","aa.gif",100,0);

?>



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板