圖片裁剪+縮圖的處理常式

來源:互聯網
上載者:User
處理方法是:
1.當原圖的寬或高任一比規定的尺寸小,只進行等比縮減處理,
2.當原圖的寬與高都比規定尺寸大,先進行等比縮減處理,然後算出置中位置進行裁剪

<?php
/*
*        $o_photo 原圖路徑
*        $d_photo 處理後圖片路徑
*        $width   定義寬
*        $height  定義高
*        調用方法  cutphoto("test.jpg","temp.jpg",256,146);
*/

function cutphoto($o_photo,$d_photo,$width,$height){

$temp_img = imagecreatefromjpeg($o_photo);
$o_width  = imagesx($temp_img);                                //取得原圖寬
$o_height = imagesy($temp_img);                                //取得原圖高

//判斷處理方法
if($width>$o_width || $height>$o_height){        //原圖寬或高比規定的尺寸小,進行壓縮

        $newwidth=$o_width;
        $newheight=$o_height;

        if($o_width>$width){
                $newwidth=$width;
                $newheight=$o_height*$width/$o_width;
        }

        if($newheight>$height){
                $newwidth=$newwidth*$height/$newheight;
                $newheight=$height;
        }

        //縮圖片
        $new_img = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
        imagejpeg($new_img , $d_photo);               
        imagedestroy($new_img);


}else{                                                                                //原圖寬與高都比規定尺寸大,進行壓縮後裁剪

        if($o_height*$width/$o_width>$height){        //先確定width與規定相同,如果height比規定大,則ok
                $newwidth=$width;
                $newheight=$o_height*$width/$o_width;
                $x=0;
                $y=($newheight-$height)/2;
        }else{                                                                        //否則確定height與規定相同,width自適應
                $newwidth=$o_width*$height/$o_height;
                $newheight=$height;
                $x=($newwidth-$width)/2;
                $y=0;
        }

        //縮圖片
        $new_img = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
        imagejpeg($new_img , $d_photo);               
        imagedestroy($new_img);
       
        $temp_img = imagecreatefromjpeg($d_photo);
        $o_width  = imagesx($temp_img);                                //取得縮圖寬
        $o_height = imagesy($temp_img);                                //取得縮圖高

        //裁剪圖片
        $new_imgx = imagecreatetruecolor($width,$height);
        imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
        imagejpeg($new_imgx , $d_photo);
        imagedestroy($new_imgx);
}

}
?>


聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.