首页 > 开发 > PHP > 正文

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

2024-05-04 22:59:18
字体:
来源:转载
供稿:网友

<?php
   
    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 "<img src="http://www.pushad.com/info/100_3353.jpg" width='".$tm[0]."' height='".$tm[1]."' />";
    foreach ($tm as $v)
    {
        echo $v."<br>";
    }

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

?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表