国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php進行圖片裁剪及生成縮略圖程序源代碼

2024-05-04 23:05:33
字體:
供稿:網(wǎng)友
處理方法是:
1.當(dāng)原圖的寬或高任一比規(guī)定的尺寸小,只進行等比縮略處理,
2.當(dāng)原圖的寬與高都比規(guī)定尺寸大,先進行等比縮略處理,然后算出居中位置進行裁剪

以下是源代碼:
<?php
/*
*        $o_photo 原圖路徑
*        $d_photo 處理后圖片路徑
*        $width   定義寬
*        $height  定義高
*        調(diào)用方法  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){        //原圖寬或高比規(guī)定的尺寸小,進行壓縮

        $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{                                                                                //原圖寬與高都比規(guī)定尺寸大,進行壓縮后裁剪

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

}
?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 虎林市| 永新县| 玉树县| 同仁县| 壶关县| 武义县| 石河子市| 平安县| 嵊泗县| 铁力市| 通州市| 清涧县| 同仁县| 田阳县| 长兴县| 永济市| 册亨县| 修水县| 陵水| 云霄县| 犍为县| 南和县| 濮阳市| 昂仁县| 江源县| 石柱| 韩城市| 台南市| 盖州市| 建始县| 南乐县| 龙江县| 于田县| 温泉县| 文成县| 马尔康县| 安义县| 宿州市| 苏州市| 苏州市| 台中市|