圖檔上傳與尺寸自動調整
發表於 : 12/12/2002 6:03 pm
這是小弟的初作,還請各位大哥指教!
檔案上傳與尺寸自動調整程式碼:
<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
檔案上傳與尺寸自動調整程式碼:
<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