私はサーバー上の動的に作成されたフォルダにさまざまな画像をアップロードし、各画像を取り込んでサイズを変更してフォルダにアップロードしながら新しい画像と新しい名前を作成しようとしています。例:image.jpg(原画像)とimage-resized.jpg(サムネイル画像)。PHPで複数の画像のサイズを変更できません
私が理解できないことは、すべての画像のサイズを変更する方法です。私はそれをループに入れるべきかどうかはわかりません。私が必要とするのは、アップロードした各画像のためだけです(時間が5になる可能性があります)。単一のイメージだけでなく、すべてをループしてサイズを変更します。ここで私のコードはどんな助けにも感謝します!
フォルダを作成し、それらのフォルダに画像を移動するためのコード:
// Desired folder structure
$structure = './Fotos/'.$newfolder;
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0, true)) {
die('Failed to create folders...');
}else{
$placefoldername = mysql_query("INSERT INTO datefolders (FolderDate) VALUES ('$newfolder')") or die(mysql_error());
echo "<div class=\"success\">El folder fue agregado con exito.<input type=\"button\" name=\"close\" value=\"X\" class=\"close\" /></div>";
}}
// ...
}
if(isset($_POST['upload'])){
$FolderDate = $_POST['fecha-folder'];
$FolderName = $_POST['FolderName'];
$hour = $_POST['hour'];
// Desired folder structure
$structure = './Fotos/'.$FolderDate.'/'.$hour.'/'.$FolderName;
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
for($i=0;$i<count($_FILES['fileupload']['name']);$i++) {
$names = $_FILES['fileupload']['name'][$i];
$target_path = "Fotos/".$FolderDate."/".$hour."/".$FolderName."/";
$target_path = $target_path . basename($_FILES['fileupload']['name'][$i]);
if(move_uploaded_file($_FILES['fileupload']['tmp_name'][$i], $target_path)) {
$success = 1;
コード小さい(リサイズされた画像)を作成しても、既に作成したフォルダに配置する:あなたはなぜ
$img = $names;
$imgPath = $structure;
function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
//Create a thunbnail image by resizing the picture
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Determine new width and height.
$newWidth = ($width/$by);
$newHeight = ($height/$by);
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
$resize = resizeImage($img, $imgPath, "-resized", 23, 100);
関数の宣言は実際にはforeachループ内にありますか、それともコピー/ペーストエラーですか?それがループ内にある場合は、二重関数定義エラーに陥るでしょう。 – Corbin
間違い!ちょうどそれを取り除きました:P – liveandream