- <?php
-
- if (isset ($_FILES['image'])) {
- $imgFile = $_FILES['image'];
-
- $imagePathDir = '/home/www/pics/';
-
- $uptypes = array (
- 'image/jpg',
- 'image/jpeg',
- 'image/pjpeg',
- 'image/gif',
- 'image/png'
- );
-
- $imgName = md5(uniqid(rand())) . '.jpg';
-
- if ($imgFile['size'] > 2097152) {
- echo '檔案過大';
-
- } elseif(in_array($imgFile['type'], $uptypes)) {
-
- if (strstr($imgFile['type'], "jp")) {
- if(!($source = @ imageCreatefromjpeg($imgFile['tmp_name']))){
- echo '檔案類型錯誤';
- return;
- }
-
- }elseif(strstr($imgFile['type'], "png")) {
- if(!($source = @ imagecreatefrompng($imgFile['tmp_name']))){
- echo '檔案類型錯誤';
- return;
- }
-
- }elseif(strstr($imgFile['type'], "gif")) {
- if(!($source = @ imagecreatefromgif($imgFile['tmp_name']))){
- echo '檔案類型錯誤';
- return;
- }
-
- } else {
- echo '檔案類型錯誤';
- return;
- }
- $w = imagesx($source);
- $h = imagesy($source);
-
- if ($w < 160 || $h < 160) {
- echo '檔案過小';
- return;
- }
-
- imagejpeg($source, $imagePathDir . $imgName);
-
- echo $imagePathDir . $imgName;
- } else {
- echo '檔案類型錯誤';
- }
- } else {
- echo '其他錯誤';
- }
- ?>
<?php
/*檢查上傳的圖片類型,並轉存成 JPG*/
if (isset ($_FILES['image'])) {
$imgFile = $_FILES['image'];
/*檔案存放路徑(目錄權限必須可寫入)*/
$imagePathDir = '/home/www/pics/';
/*上傳圖片文件類型列表 */
$uptypes = array (
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/gif',
'image/png'
);
/*產生唯一的檔案名稱*/
$imgName = md5(uniqid(rand())) . '.jpg';
/*檢查檔案大小 2Mb*/
if ($imgFile['size'] > 2097152) {
echo '檔案過大';
/*檢查文件類型 */
} elseif(in_array($imgFile['type'], $uptypes)) {
/*上傳圖片類型為jpg,pjpeg,jpeg */
if (strstr($imgFile['type'], "jp")) {
if(!($source = @ imageCreatefromjpeg($imgFile['tmp_name']))){
echo '檔案類型錯誤';
return;
}
/*上傳圖片類型為png */
}elseif(strstr($imgFile['type'], "png")) {
if(!($source = @ imagecreatefrompng($imgFile['tmp_name']))){
echo '檔案類型錯誤';
return;
}
/*上傳圖片類型為gif */
}elseif(strstr($imgFile['type'], "gif")) {
if(!($source = @ imagecreatefromgif($imgFile['tmp_name']))){
echo '檔案類型錯誤';
return;
}
/*其他例外圖片排除 */
} else {
echo '檔案類型錯誤';
return;
}
$w = imagesx($source); /*取得圖片的寬 */
$h = imagesy($source); /*取得圖片的高 */
/*檢查檔案最小尺寸 160px*160px */
if ($w < 160 || $h < 160) {
echo '檔案過小';
return;
}
/* 儲存到檔案目錄(JPG) */
imagejpeg($source, $imagePathDir . $imgName);
/* 列出檔案路徑 */
echo $imagePathDir . $imgName;
} else {
echo '檔案類型錯誤';
}
} else {
echo '其他錯誤';
}
?>