搜尋
首頁php教程php手册php进行图片裁剪及生成缩略图程序源代码

程序|源代码|缩略图

处理方法是:
1.当原图的宽或高任一比规定的尺寸小,只进行等比缩略处理,
2.当原图的宽与高都比规定尺寸大,先进行等比缩略处理,然后算出居中位置进行裁剪

以下是源代码:
/*
*        $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);
}

}
?>



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能