Loading...

PHP使用GD函數切正方形圖像

Temperature: 0 °C

ChungChung
author_tools

基本上大都是等比例將圖片上傳....

但有時也會有需要正方形邊長的圖像需求

這時候還是得靠GD函式了...

例如要取360*360等邊的正方形

/*為亂數取名*/
$utime_img=date("YmdHis");
$flag=rand(0000,9999);//亂數取值
/*首先取得上傳的圖片*/
$src = imagecreatefromjpeg($_FILES["upload"]["tmp_name"]);
/*取得圖片的寬*/
$src_w = imagesx($src);
/*取得圖片的長*/
$src_h = imagesy($src);
/*依長與寬兩者最短的邊來算出要抓的正方形邊長*/
if( $src_w > $src_h){
$new_w = $src_h;
$new_h = $src_h;
}else{
$new_w = $src_w;
$new_h = $src_w;
}
/*以長方形的中心來取得正方形的左上方原點*/
$srt_w = ( $src_w - $new_w ) / 2;
$srt_h = ( $src_h - $new_h ) / 2;
/*定義一個圖形 ( 針對正方形圖形 )*/
$newpc = imagecreatetruecolor($new_w,$new_h);
/*抓取正方形的截圖*/
imagecopy($newpc, $src, 0, 0, $srt_w, $srt_h, $new_w, $new_h );
/*建立等比縮圖*/
$finpic = imagecreatetruecolor(360,360);
/*開始縮圖*/
imagecopyresampled($finpic, $newpc, 0, 0, 0, 0, 360, 360, $new_w, $new_h);
/*儲存縮圖到指定的目錄存放*/
imagejpeg($finpic,"URL".$_SESSION["filename"].$utime_img.$flag."s.jpg");
/*將檔名存到變數 $filename*/
$newimagename = $_SESSION["filename"].$utime_img.$flag."s.jpg";

得到新的圖像名稱後,便可將其寫入資料庫欄位備用。

台北市內湖區江南街71巷75弄 Go

https://innstory.com/story-PHP使用GD函數切正方形圖像-536
寫程式筆記

Prev
 秋蟹肥美水庫大閘蟹

Next
使用dpSyntaxHighlighter_程式碼色彩顯示工具讓紀錄程式心得更有可看性 

Nearby Attractions

台北市內湖區江南街71巷75弄-Nearby Attractions

  • 松山市場 Go
  • 齊東街日式宿舍 Go
  • 番學堂遺構 Go
  • 國立臺灣大學日式宿舍─馬廷英... Go
  • 北投文物館 Go
  • 寶藏巖 Go

About the Author

Chung

我是chung
網路工作者
主業是網站系統開發建置
副業是做夢,寫故事
作品請參考/teme.biz
做夢請參考/innstory.com
聯絡/chung.teme@gmail.com

#有人用筆寫日記,有人用歲月寫日記,有人用照片寫日記,而我,用innstory寫日記。

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

動態時報?

動態時報?

dsc5 抓meta value的功能雖然還不是很完整... 但一些網站的meta value 已經...

定義404頁面

定義404頁面

什麼是頁面? HTTP 或Not Found錯誤訊息是HTTP的其中一種「標準回應訊息」(HTTP狀...

Mysql最怕就是手賤

Mysql最怕就是手賤

今天我手賤了眼盲了@@... 錯把delete當成UPDATE....啃!!今天下午超不順的... ...

Recommended reading

Other stories happened at this address

When caterpillar grow up become butterfly

When caterpill...

When caterpillar grow up become butterfly

親子時間,​​​​​​​黏土時間。

親子時間,​​​​​​​黏土...

s 外頭滴滴答答的下著雨,正好陪著U玩黏土。 話說這個年紀的小孩最愛模仿,但這傢伙沒有模仿我做黏土的...

音樂,就像是時光機器。許茹芸-獨角戲

音樂,就像是時光機器。許茹芸...

音樂,就像是時光機器。 聽著聽著.......,不小心就跌入了記憶的漩渦裡~ 這也是一個人寫code...

紀錄故事第一步得先加入這個系統-如何加入作者群?

紀錄故事第一步得先加入這個系...

login 首先,在首頁的上方靠右,如上圖所示,有一個文字連結叫做【登入 innstory? / 或...

很久沒畫了~2015年11月29日這是我第一張嘗試畫在手機裡~

很久沒畫了~2015年11月...

s 小時候~喜歡畫畫,但其實是模仿著家姐的腳步~ 我姐姐以前畫畫很灑脫~隨便幾筆、隨便上色~看起來都...

innstory首頁設計

innstory首頁設計

年,再次幫Innstory換上了新版的首頁。 這次使用了bootstrap RWD 設計。 Boot...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format