|
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>Flash/PHP Uploading Images</title>
</head>
<body>
<?php
mb_internal_encoding("SJIS");
//アップロード画像、サムネイルファイルの格納フォルダが無ければ作成
if(!is_dir("./images")) mkdir("./images", 0755);
if(!is_dir("./simages")) mkdir("./simages", 0755);
//保存ファイル数の制限を設定
$max_files = 10;
//アップロードファイルの名前の取得
$source = $_FILES['Filedata']['tmp_name'];
//アップロード画像の確認・ファイル名の確定
if (is_uploaded_file($source)) {
$file = $_FILES['Filedata']['name'];
}
//画像の保存、サムネイル画像の作成と保存(画像タイプごとに分けて処理)
if ($file) {
move_uploaded_file($source, "./images/".$file);
rename("./images/".$file,"./images/L".$file);
$imgfile = "./images/L".$file;
$imgtype = exif_imagetype($imgfile);
switch($imgtype) {
case '1':
$imgSource = imageCreateFromGif($imgfile);
$dest = resizeImg($imgSource);
$sfile = "./simages/s".$file;
imageGif($dest,$sfile);
break;
case '2':
$imgSource = imageCreateFromJpeg($imgfile);
$dest = resizeImg($imgSource);
$sfile = "./simages/s".$file;
imageJpeg($dest,$sfile);
break;
case '3':
$imgSource = imageCreateFromPng($imgfile);
$dest = resizeImg($imgSource);
$sfile = "./simages/s".$file;
imagePng($dest,$sfile);
break;
}
}
//サムネイル作成関数
function resizeImg($imgSource) {
$minX = 120;
$minY = 120;
$width = min((imageSx($imgSource) / imageSy($imgSource)*$minX), $minX);
$height = min((imageSy($imgSource) / imageSx($imgSource)*$minY), $minY);
$dest = imageCreateTrueColor($width,$height);
imageCopyResampled($dest,$imgSource,0,0,0,0,$width,$height,imageSx($imgSource),imageSy($imgSource));
return $dest;
imagedestroy($source);
imagedestroy($dest);
imagedestroy($imgSource);
}
//過剰保存ファイルの削除(古いものから)
$dirs = './simages/';
$dir = './images/';
unlinkFiles( $dirs, $max_files);
unlinkFiles( $dir, $max_files);
function unlinkFiles($dir,$max_files) {
$directory = opendir($dir);
$files_arr = array();
while ($f = readdir($directory)) {
array_push($files_arr,array($dir.$f,filectime($dir.$f)));
}
usort($files_arr, 'sorter');
if (count($files_arr) > $max_files) {
$excessivefiles = array_splice($files_arr, 0, count($files_arr) - $max_files);
for ($i = 0; $i < count($excessivefiles); $i++) {
unlink($excessivefiles[$i][0]);
}
}
closedir($directory);
}
function sorter($a, $b) {
if ($a[1] == $b[1]) {
return 0;
} else {
return ($a[1] < $b[1]) ? -1 : 1;
}
}
?>
</body></html> |