Results 1 to 6 of 6

Thread: Loops

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Loops

    Hi all, I am working on a image upload/resize/watermark script, but I am slightly stuck! I started with a script that only allows you to upload one file at a time and modified it to do all the stuff I needed, but now it has the option to upload more than one file, but I dont know how to tell the PHP bit to loop through for each of the files that have been selected.
    Here is the code I am using:
    PHP Code:
    <?php

    require_once 'watermark.php';

    $idir "large/";   // Path To Images Directory
    $tdir "thumbs/";   // Path To Thumbnails Directory
    $wdir "watermarked/"// Path to watermarked image
    $twidth "200";   // Maximum Width For Thumbnail Images
    $theight "150";   // Maximum Height For Thumbnail Images

    if (!isset($_GET['subpage'])) {   // Image Upload Form Below   
    ?>
    <script language="javascript">
    var upload_range = 1;
        
        // Add one upload slot
    function addUploadSlot(num){
            if(num == upload_range){
                var up = document.getElementById('upload_slots');
                var dv = document.createElement("div");

                dv.innerHTML = '<input type="file" onChange="addUploadSlot('+(upload_range + 1)+')" name="imagefile[' + (upload_range + 1) + ']" class="form">Yes<input type="checkbox" name="watermark[' + (upload_range + 1) + ']" value="yes">';
                up.appendChild(dv);
                upload_range++;
                up = null;
                dv = null;
            }
    }
    </script>
      
    <form method="post" action="index.php?subpage=upload" enctype="multipart/form-data">
       <table width="37%" border="0">
         <tr>
           <td width="60%">File</td>
           <td width="40%">Watermark?</td>
         </tr>
         <tr>
           <td colspan="2"><div id="upload_slots"><input type="file" onChange="addUploadSlot(1)" name="imagefile[1]" >Yes<input type="checkbox" name="watermark[1]" value="yes"></div></td>
         </tr>
       </table>
       <p>
         <input name="submit" type="submit" value="Upload">  
         <input type="reset" value="Clear" class="form">
          </p>
    </form>
      
    <? 
    } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script
     
    $url $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use

    if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
        
    $file_ext strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
        
    $copy copy($_FILES['imagefile']['tmp_name'], "$idir$_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
        
        
    if (isset($_POST['watermark']) && $_POST['watermark'] == 'yes') {
          
          
    $saveFile $url;
          
    $sourcefile $idir $url;
          
          
    watermark($sourcefile$saveFile);
                
          print 
    'Watermarked image created successfully<br />';   // Resize successful 
          
          
    }
        
        if (
    $copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
        
          
    print 'Image uploaded successfully<br />';   // Was Able To Successfully Upload Image
         
          
    $simg imagecreatefromjpeg("$idir$url);   // Make A New Temporary Image To Create The Thumbanil From
          
    $currwidth imagesx($simg);   // Current Image Width
          
    $currheight imagesy($simg);   // Current Image Height
          
          
    if ($currheight $currwidth) {   // If Height Is Greater Than Width
             
    $zoom $twidth $currheight;   // Length Ratio For Width
             
    $newheight $theight;   // Height Is Equal To Max Height
             
    $newwidth $currwidth $zoom;   // Creates The New Width
          
          
    } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
          
            
    $zoom $twidth $currwidth;   // Length Ratio For Height
            
    $newwidth $twidth;   // Width Is Equal To Max Width
            
    $newheight $currheight $zoom;   // Creates The New Height
          
          
    }
          
          
    $dimg imagecreate($newwidth$newheight);   // Make New Image For Thumbnail
          
    imagetruecolortopalette($simgfalse256);   // Create New Color Pallete
          
    $palsize ImageColorsTotal($simg);
          for (
    $i 0$i $palsize$i++) {   // Counting Colors In The Image
           
    $colors ImageColorsForIndex($simg$i);   // Number Of Colors Used
           
    ImageColorAllocate($dimg$colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
          
    }
          
          
    imagecopyresized($dimg$simg0000$newwidth$newheight$currwidth$currheight);   // Copy Resized Image To The New Image (So We Can Save It)
          
    imagejpeg($dimg"$tdir$url);   // Saving The Image
          
    imagedestroy($simg);   // Destroying The Temporary Image
          
    imagedestroy($dimg);   // Destroying The Other Temporary Image
          
    print 'Image thumbnail created successfully';   // Resize successful 
      
        
    } else {
        
          print 
    '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
          
        
    }
        
      } else {
      
        print 
    '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
        
    print $file_ext;   // Show The Invalid File's Extention
        
    print '.</font>';
      }
      

    ?>
    Thanks in advance for any help
    Last edited by calumogg; 04-10-2008 at 06:13 PM.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try either using a foreach loop before the actual script execution like so:

    Code:
    <? 
    } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script
     
    foreach ($_FILES['imagefile'] as $thefile) {
     $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
    Then you'll have to edit the code to point to $thefile['attribute'] instead of $_FILES['imagefile']['attribute']. Or you can use a for loop like so:

    Code:
    <? 
    } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script
     
    for ($i=1; $i<count($_FILES['imagefile']); $i++) {
     $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
    Then change the rest of the code to something like this $_FILES['imagefile'][$i]['attribute'].

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks for the reply, I have tried both of those methods and they both give errors. I have changed the code slightly, but I still don't know how to get it to process more than one file.
    PHP Code:
    <?php

    require_once 'image-functions.php';

    $odir "origional/";   // Path To Images Directory
    $ldir "large/";   // Path To Images Directory
    $tdir "thumbs/";   // Path To Thumbnails Directory
    $wdir "watermarked/"// Path to watermarked image

    if (!isset($_GET['subpage'])) {   // Image Upload Form Below   
    ?>
    <script language="javascript">
    var upload_range = 1;
        
        // Add one upload slot
    function addUploadSlot(num){
                var up = document.getElementById('file');
                var dv = document.createElement("div");
                dv.innerHTML = '<input type="file" name="imagefile" />';
                up.appendChild(dv);
                up = null;
                dv = null;
                
                var up = document.getElementById('watermark');
                var dv = document.createElement("div");
                dv.innerHTML = 'Yes<input type="checkbox" name="watermark" value="yes" />';
                up.appendChild(dv);
                up = null;
                dv = null;
                
                var up = document.getElementById('large');
                var dv = document.createElement("div");
                dv.innerHTML = 'Yes<input type="checkbox" name="large" value="yes" />';
                up.appendChild(dv);
                up = null;
                dv = null;
                
                var up = document.getElementById('thumbnail');
                var dv = document.createElement("div");
                dv.innerHTML = 'Yes<input type="checkbox" name="thumbnail" value="yes" />';
                up.appendChild(dv);
                up = null;
                dv = null;
                
                upload_range++;
    }
    </script>
      
    <form method="post" action="index.php?subpage=upload" enctype="multipart/form-data">
       <table width="80%" border="1">
         <tr>
           <td width="30%">File</td>
           <td width="12%">Watermark?</td>
           <td width="24%">Make Large copy?</td>
           <td width="34%">Make thumbnail?</td>
         </tr>
         <tr>
           <td><div id="file"><input type="file" name="imagefile" /></div></td>
           <td><div id="watermark">Yes<input type="checkbox" name="watermark" value="yes" /></div></td>
           <td><div id="large">Yes<input type="checkbox" name="large" value="yes" /></div></td>
           <td><div id="thumbnail">Yes<input type="checkbox" name="thumbnail" value="yes" /></div></td>
         </tr>
       </table>
       <p><a href="javascript:addUploadSlot(1)"> Add another</a></p>
       <p>
         <input name="submit" type="submit" value="Upload">  
         <input type="reset" value="Clear" class="form">
       </p>
    </form>
      
    <? 
    } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script

    $url $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use

    if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
        
    $file_ext strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
        
    $copy copy($_FILES['imagefile']['tmp_name'], "$odir$_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
        
        
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
        
          
    print 'Image uploaded successfully<br />';   // Was Able To Successfully Upload Image
          
          
    } else {
        
          print 
    '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
          
        
    }
          
          if (isset(
    $_POST['large']) && $_POST['large'] == 'yes') {
          
    $image $url;
          
    $dir $odir;
          
    $savedir $ldir;
          if (isset(
    $_POST['lheight']) && ($_POST['lwidth'])){
          
    $height $_POST['lheight'];
          
    $width $_POST['lwidth'];
          } else {
          
    $height 600;
          
    $width 800; }

         
    resize($dir$height$width$savedir$image);
         print 
    'large image created<br />';
         
         }
         
         if (isset(
    $_POST['thumbnail']) && $_POST['thumbnail'] == 'yes') {
          
    $image $url;
          
    $dir $odir;
          
    $savedir $tdir;
          if (isset(
    $_POST['theight']) && ($_POST['twidth'])){
          
    $height $_POST['theight'];
          
    $width $_POST['twidth'];
          } else {
          
    $height 150;
          
    $width 200; }
          
         
    resize($dir$height$width$savedir$image);
         print 
    'Thumbnail image created<br />';
         
         }
           
      if (isset(
    $_POST['watermark']) && $_POST['watermark'] == 'yes') {
          
    $image $url;
          
    $sourcefile $ldir $url;
          
    $savedir $wdir;
          
    watermark($sourcefile$image$savedir);  
          print 
    'Watermarked image created<br />';   // Resize successful 
          
          
    }
       
      } else {
      
        print 
    '<font color="#FF0000">ERROR: Wrong filetype has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
        
    print $file_ext;   // Show The Invalid File's Extention
        
    print '.</font>';
      }

    ?>

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    What's the error that they give you? If it is an unexpected $end error, did you forget to place the closing bracket on the loop (at the end of the script)? I currently don't have the time to play around with the script at the moment, but if no one else has helped you by the time I get to work, I'll take a stab at it again.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    The scrip is saying that I have uploaded the wrong type of file. But I am getting the error the same number of times as the number of files I am trying to upload so thats a start I suppose!

  6. #6
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Sorted it, I'm now using a different script.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •