image_testフォルダに、画像をリサイズして保存しています。
保存する時は、
$tmpName = $_FILES['image']['tmp_name'];
ではなく、年月日時分秒+ランダムな0~10000の値を画像名にするにはどうしたらよいか教えて下さい
$imagename = date("YmdHis") . rand(0,10000)
<?php
function uploadImage($tmpName, $dir, $maxWidth, $maxHeight){
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($tmpName);
if($mime == 'image/jpeg' || $mime == 'image/pjpeg'){
$ext = '.jpg';
$image1 = imagecreatefromjpeg($tmpName);
} elseif($mime == 'image/png' || $mime == 'image/x-png'){
$ext = '.png';
$image1 = imagecreatefrompng($tmpName);
} elseif($mime == 'image/gif'){
$ext = '.gif';
$image1 = imagecreatefromgif($tmpName);
} else {
return false;
}
list($width1, $height1) = getimagesize($tmpName);
if($width1 <= $maxWidth && $height1 <= $maxHeight){
$scale = 1.0;
} else {
$scale = min($maxWidth / $width1, $maxHeight / $height1);
}
$width2 = $width1 * $scale;
$height2 = $height1 * $scale;
$image2 = imagecreatetruecolor($width2, $height2);
if($ext == '.gif'){
$transparent1 = imagecolortransparent($image1);
if($transparent1 >= 0){
$index = imagecolorsforindex($image1, $transparent1);
$transparent2 = imagecolorallocate($image2, $index['red'], $index['green'], $index['blue']);
imagefill($image2, 0, 0, $transparent2);
imagecolortransparent($image2, $transparent2);
}
} elseif($ext == '.png'){
imagealphablending($image2, false);
$transparent = imagecolorallocatealpha($image2, 0, 0, 0, 127);
imagefill($image2, 0, 0, $transparent);
imagesavealpha($image2, true);
}
imagecopyresampled($image2, $image1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);
if(!file_exists($dir)){
mkdir($dir, 0777, true);
}
$filename = sha1(microtime() . $_SERVER['REMOTE_ADDR'] . $tmpName) . $ext;
$saveTo = rtrim($dir, '/\\') . '/' . $filename;
if($ext == '.jpg'){
$quality = 80;
imagejpeg($image2, $saveTo, $quality);
} else if($ext == '.png'){
imagepng($image2, $saveTo);
} else if($ext == '.gif'){
imagegif($image2, $saveTo);
}
imagedestroy($image1);
imagedestroy($image2);
return $saveTo;
}
if($_SERVER["REQUEST_METHOD"] === 'POST'
&& !empty($_FILES['image']['tmp_name']))
{
$maxWidth = 300; // 最大幅
$maxHeight = 300; // 最大高さ
// 一時ファイルの場所
$tmpName = $_FILES['image']['tmp_name'];
// 保存先のディレクトリ
$dir = __DIR__ . '/image_test/';
$path = uploadImage($tmpName, $dir, $maxWidth, $maxHeight);
var_dump($path);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>image</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="submit">
</form>
</body>
</html>
A 回答 (1件)
- 最新から表示
- 回答順に表示
No.1
- 回答日時:
$filename = sha1( … ) . $ext;
の部分をご提示の方式に変えれば良いかと
ただし、
ランダム値で命名するならば重複時の考慮は必要かと思います
例)
for ($n = 0; $n < 命名回数最大; $n++) {
_ $filename = ファイル命名();
_ if (!file_exists($dir . '/' . $filename)) break;
}
if ($n == 1000) die("命名ルールが破綻しているので再考を");
参考)
https://www.php.net/manual/ja/function.file-exis …
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・【お題】絵本のタイトル
- ・【大喜利】世界最古のコンビニについて知ってる事を教えてください【投稿~10/10(木)】
- ・メモのコツを教えてください!
- ・CDの保有枚数を教えてください
- ・ホテルを選ぶとき、これだけは譲れない条件TOP3は?
- ・家・車以外で、人生で一番奮発した買い物
- ・人生最悪の忘れ物
- ・【コナン30周年】嘘でしょ!?と思った○○周年を教えて【ハルヒ20周年】
- ・ハマっている「お菓子」を教えて!
- ・最近、いつ泣きましたか?
- ・夏が終わったと感じる瞬間って、どんな時?
- ・10秒目をつむったら…
- ・人生のプチ美学を教えてください!!
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・都道府県穴埋めゲーム
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
最も速い画像合成処理を教えて...
-
拡張子php画像をjpg画像等に変...
-
一部だけ動くGIFアニメを作りたい
-
GDの画像変換でサーバーに負荷
-
ListViewコントロールでサムネ...
-
PythonのTkinter詳しい方へ。画...
-
「imagejpeg」(GD)で作成した...
-
PHPで画像コピーについて
-
透過PNGが透過されない!!
-
ChatGPTに作らせたい Excel VBA
-
VBSの「MsgBox」について
-
PHP ファイルアップロードの縮...
-
php、JDライブラリを用いて画像...
-
PHPのGDライブラリについて
-
PHPのif文でその処理を途中で抜...
-
3つ以上の論理積は利用可能なの...
-
「クラス関数」「メンバ関数」...
-
htmlでsubmitで指定したAction...
-
指定したURLからファイル(PDF...
-
正規表現を使い、URLで条件分岐...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
透過PNGが透過されない!!
-
VBSの「MsgBox」について
-
拡張子php画像をjpg画像等に変...
-
Illustratorで文字と画像を流し...
-
手作業で埋め込んだ、UserForm1...
-
GDI+を使ったビット数とDPIの扱い
-
C# Excelファイルへの画像挿入。
-
VBAでJPGサイズ変更
-
GD(php)での image関数 イメ...
-
ListViewコントロールでサムネ...
-
php,mysqlにて画像パス保存/表...
-
ckfinderの画像のアップロード先
-
pictureboxに表示した画像のフ...
-
改行コードのカウント
-
<ASP.NET+C#+MSDE> JPEG画像の...
-
Web画像コピー不可にする方法
-
phpで画像がどうしても文字化け...
-
iphoneで虫眼鏡の作り方
-
<img src="http://x.y.com/cgi-...
-
PHPで吐き出した画像にリンクを...
おすすめ情報