2012-03-02 18 views
0

私はサーバー上の動的に作成されたフォルダにさまざまな画像をアップロードし、各画像を取り込んでサイズを変更してフォルダにアップロードしながら新しい画像と新しい名前を作成しようとしています。例: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); 
+0

関数の宣言は実際にはforeachループ内にありますか、それともコピー/ペーストエラーですか?それがループ内にある場合は、二重関数定義エラーに陥るでしょう。 – Corbin

+0

間違い!ちょうどそれを取り除きました:P – liveandream

答えて

0

forループ内に関数resizeImageを定義していますか?ループが繰り返されるたびに再定義されています。これは問題の一部になる可能性があります。ループ外の関数を定義し、それが機能するかどうかを確認します。

+0

申し訳ありません私はちょうどそれを取り除きました..それは単に私が遊んでいたものでした、投稿する前にそれを取ることを忘れました。 – liveandream

+1

私はそれを考え出した!関数をforループから取り出して、forループ内の関数を呼び出す必要があります。とにかくみんなありがとう! – liveandream

0

お試しいただけます。

最初の画像処理の終了時にimagedestroy()を使用すると、2番目の画像が処理されます。

+0

私は元のイメージと一時イメージで既にimagedestroyを使用しています。これを$ imgと$ imgPathにも追加する必要がありますか?本当にありがとう! – liveandream

関連する問題