圖檔上傳與尺寸自動調整

Mac OS X 平台上程式設計的相關問題討論

版主: bryanchangdigdog謝孟叡

回覆文章
內容
發表人
頭像
cjtai
冰果室水電工
文章: 2226
註冊時間: 04/19/2001 1:01 am
來自: dark side in the earth.
聯繫:

圖檔上傳與尺寸自動調整

#1 文章 cjtai »

這是小弟的初作,還請各位大哥指教!

檔案上傳與尺寸自動調整程式碼:
<BLOCKQUOTE><font size="1" face="XYZ">quote:</font><HR>
if ($file1_name != "") {
$sfileext = substr($file1_name, -3);
$sfilename = $sid . "-1." . $sfileext;
$uploadfilepath = $uploadimgdir . $sfilename;
copy($file1, $uploadfilepath);
resizeimg($uploadimgdir, $sfilename, $sfileext, $img_width);
resizeiconimg($uploadimgdir, $sfilename, $sfileext, $simg_width);
}
<HR></BLOCKQUOTE>

下面是Fuction Code:
<BLOCKQUOTE><font size="1" face="XYZ">quote:</font><HR>
function resizeimg($pathname, $filename, $fileext, $defaultsize) {
$imagefile = $pathname . $filename;
$newimagefile = $pathname . $filename;
$src_img = imagecreatefromjpeg($imagefile);
$width = imagesx($src_img);
$height = imagesy($src_img);
if ($width > $defaultsize) {
$newwidth = $defaultsize;
$tmpheight = $height * $newwidth / $width;
}
else {
$newwidth = $width;
$tmpheight = $height;
}
$newheight = $tmpheight;
$dst_img = imagecreate($newwidth,$newheight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $newwidth, $newheight, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, $newimagefile);
}

function resizeiconimg($pathname, $filename, $fileext, $defaultsize) {
$imagefile = $pathname . $filename;
$newimagefile = $pathname . "s" . $filename;
$src_img = imagecreatefromjpeg($imagefile);
$width = imagesx($src_img);
$height = imagesy($src_img);
if ($width > $defaultsize) {
$newwidth = $defaultsize;
$tmpheight = $height * $newwidth / $width;
}
else {
$newwidth = $width;
$tmpheight = $height;
}
$newheight = $tmpheight;
$dst_img = imagecreate($newwidth,$newheight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $newwidth, $newheight, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, $newimagefile);
}
<HR></BLOCKQUOTE>

開發使用平台是RedHat 7.2所安裝預設的Apache + PHP + GD
頭像
cjtai
冰果室水電工
文章: 2226
註冊時間: 04/19/2001 1:01 am
來自: dark side in the earth.
聯繫:

圖檔上傳與尺寸自動調整

#2 文章 cjtai »

主要的功能在由Form選擇一個圖檔上傳,並依照設定的目錄位置存檔,並依照設定的尺寸調整圖片大小(是實際改變圖片尺寸,而非設定顯示尺寸。)

要儲存上傳圖片的目錄權限必須是777。
頭像
cjtai
冰果室水電工
文章: 2226
註冊時間: 04/19/2001 1:01 am
來自: dark side in the earth.
聯繫:

圖檔上傳與尺寸自動調整

#3 文章 cjtai »

再補充,目前只能處理Jpeg格式圖檔!(希望有人改進到各種圖檔格式都吃)
頭像
麻辣杯麵
哇哈哈~我是神
文章: 5222
註冊時間: 04/26/2001 1:01 am
來自: 可以看到極光的地方

圖檔上傳與尺寸自動調整

#4 文章 麻辣杯麵 »

Good job!
我也想弄個網路相簿,或許這個程式可以幫到我,
這樣我就不用一張一張慢慢貼了~
頭像
cjtai
冰果室水電工
文章: 2226
註冊時間: 04/19/2001 1:01 am
來自: dark side in the earth.
聯繫:

圖檔上傳與尺寸自動調整

#5 文章 cjtai »

<BLOCKQUOTE><font size="1" face="XYZ">quote:</font><HR> Good job!
我也想弄個網路相簿,或許這個程式可以幫到我,
這樣我就不用一張一張慢慢貼了~ <HR></BLOCKQUOTE>

相信選擇一個檔案夾,然後跑for - next的方式應該是沒問題。

不過我前面的做法還是一張張選圖然後上傳,再由系統將JPEG圖片Resize(如果您上傳的圖寬度大於設定值的話,這個功能可以預防上傳的圖片撐壞網頁的版面。)。

不必動用FTP程式,一切動作在瀏覽器搞定。
回覆文章