按比例產生縮圖檔案及顯示縮圖

來源:互聯網
上載者:User
縮圖|顯示

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

    //CreateThumbnail("100_3353.jpg","aa.gif",100,0);

?>



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。