title: php image upload with class.upload
body: <p>I am using <a href="http://jasny.github.io/bootstrap/javascript.html#fileupload" rel="nofollow">boostrap file upload</a> with <a href="http://www.verot.net/php_class_upload.htm" rel="nofollow">class.upload</a> from verot. All my code works fine, however I want to add the additional functionality of having a checkbox to determine how the script crops the images.</p>
  
  <pre><code>$handle-&gt;image_ratio_crop = true; // basically zooms in on the middle (default)
  $handle-&gt;image_ratio_fill = true; // preserves ratio and adds white bars where needed
  </code></pre>
  
  <p>If the user checks image_ratio_fill next to the image then I want this to be used instead of the default in the script. Currently I have 4 image uploads available on the page. The following script iterates through each file and adds performs the necessary cropping .etc.</p>
  
  <p>PHP:</p>
  
  <pre><code>function upImageSpec($table, $m_x, $m_y, $t_x, $t_y)
  {
      $id = $_GET['id'];
      $img_db_name = str_replace('s', '', $table); // this will replace the s with nothing so that it is user_image_1, project_image_1 for the db insertion
      // unchanging variables
      $ext = 'jpg';
      $upload_path = FRONTEND_IMAGE_UPLOAD_PATH . slash_item($table) . slash_item($id); // will not work with /images/
      // end unchanging variables
      $files = array();
      foreach ($_FILES['userfile'] as $k =&gt; $l) {
          foreach ($l as $i =&gt; $v) {
              if (!array_key_exists($i, $files))
                  $files[$i] = array();
              $files[$i][$k] = $v;
          }
      }
      $counter = 1;
      foreach ($files as $file) {
          // foreach variables
          $main_name = 'm_' . $id;
          $thumb_name = 't_' . $id;
          $count = $counter++;
          // end foreach variables
          $handle = new upload($file);
          if ($handle-&gt;uploaded) {
              // save uploaded image $m_x, $m_y
              $mi = sprintf("%s_%d", $main_name, $count);
              $full_src = REL_FRONTEND_IMAGE_UPLOAD_PATH . slash_item($table) . slash_item($id) . $mi . '.' . $ext;
              $handle-&gt;file_new_name_body = $mi;
              $handle-&gt;image_convert = $ext;
              $handle-&gt;allowed = array(
                  'image/*'
              );
              $handle-&gt;file_max_size = MAX_IMAGE_FILE_SIZE;
              $handle-&gt;jpeg_quality = 95;
              $handle-&gt;image_resize = true;
              //$handle-&gt;image_ratio_crop = true;
              $handle-&gt;image_ratio_fill = true;
              $handle-&gt;image_x = $m_x;
              $handle-&gt;image_y = $m_y;
              $handle-&gt;file_overwrite = true;
              $handle-&gt;auto_create_dir = true;
              $handle-&gt;process($upload_path);
              if ($handle-&gt;processed) {
                  Nemesis::update($table, "last_modified = NOW(), last_modified_by = '{$_SESSION[user_id]}', {$img_db_name}_image_{$count} = '{$full_src}'", "id = '{$id}'");
              } else {
                  $msg = new Messages();
                  $msg-&gt;add('e', $handle-&gt;error);
              }
              // thumbnail part, same as above, just w/ diff dimensions
          }
          unset($handle);
      }
  }
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>    &lt;?php if ($totalRows_projects &gt; 0) { ?&gt;
      &lt;?php $msg = new Messages(); echo $msg-&gt;display(); ?&gt;
      &lt;h2&gt;Project Images&lt;?php if (!empty($row_projects['project_name'])) { echo ': ' . $row_projects['project_name']; }?&gt;&lt;/h2&gt;
      &lt;form action="framework/helpers/image_handler.php?type=upload&amp;id=&lt;?php echo $_GET['id']; ?&gt;" method="post" enctype="multipart/form-data"&gt;
         &lt;?php $counter = 1; while ($row = $resultImages-&gt;fetch_assoc()) { $count = $counter++; ?&gt;
         &lt;div class="fileupload fileupload-new" data-provides="fileupload"&gt;
            &lt;input type="hidden"&gt;
            &lt;div class="fileupload-new thumbnail" style="width: 104px; height: 76px;"&gt;&lt;span id="img&lt;?php echo $count; ?&gt;"&gt;&lt;img src="&lt;?php getThumb($row_projects["project_image_$count"]); ?&gt;"&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div class="fileupload-preview fileupload-exists thumbnail" style="width: 104px; height: 76px; line-height: 50px; "&gt;&lt;/div&gt;
            &lt;span class="btn btn-file"&gt;&lt;span class="fileupload-new"&gt;Select image&lt;/span&gt;&lt;span class="fileupload-exists"&gt;Change&lt;/span&gt;&lt;input type="file" name="userfile[]" id="file&lt;?php echo $count; ?&gt;" class="search" multiple&gt;&lt;/span&gt;
            &lt;a href="#" class="btn fileupload-exists" data-dismiss="fileupload"&gt;Remove&lt;/a&gt;
            &lt;?php if (is_file(ROOT . $row_projects["project_image_$count"])) { ?&gt;&lt;a href="javascript:void(0);" onclick="$.get('framework/helpers/image_remove.php',{ cmd: 'deleteImage', image: '&lt;?php echo $row_projects["project_image_$count"]; ?&gt;' } ,function(data){ $('#img&lt;?php echo $count; ?&gt;').html(data); });" class="fileupload-controls btn btn-danger" title="Deletion is permanent!"&gt;Remove Existing&lt;/a&gt;&lt;?php } ?&gt;
            &lt;select name="cropfactor" class="select-ms"&gt;
               &lt;option value="1"&gt;Ratio Crop&lt;/option&gt;
               &lt;option value="2"&gt;Ratio Fill&lt;/option&gt;
            &lt;/select&gt;
         &lt;/div&gt;
         &lt;?php } ?&gt;
         &lt;br&gt;&lt;input name="submit" type="submit" id="submit" value="Add Project Image" class="btn btn-success"&gt;&lt;a id="back" href="projects.php" class="btn btn-space" title="No changes will be made"&gt;Skip to Projects&lt;/a&gt;&lt;?php if (!empty($row_projects['youtube_link'])) { ?&gt;&lt;a id="yt_image" href="framework/helpers/image_handler.php?type=youtube&amp;id=&lt;?php echo $id; ?&gt;&amp;link=&lt;?php echo $row_projects['youtube_link']; ?&gt;" class="btn btn-space" title="Add Youtube still as a main image"&gt;Get YouTube Thumb&lt;/a&gt;&lt;?php } ?&gt;
         &lt;input type="hidden" name="MAX_FILE_SIZE" value="&lt;?php echo $MAX_IMAGE_FILE_SIZE ?&gt;"&gt;
      &lt;/form&gt;
      &lt;?php } ?&gt;
  </code></pre>
  
  <p>On post the upper code submits and is processed through the function above via image_handler.php</p>
  
  <p>Code snippet:</p>
  
  <pre><code>case 'upload':
      if (checkIDExists()) {
          upImageSpec('projects', 458, 332, 104, 76);
          redirect('', true);
      } else {
          $msg = new Messages();
          $msg-&gt;add('e', QUERY_ID_ERROR);
          redirect('', true);
      }
      break;
  </code></pre>
  
  <p>How would one user the <code>cropfactor</code> field so that if changed to ratio fill the default (ratio crop) is switched off, and the <code>image_ratio_fill</code> is used. Assuming that other images on the page might want to be kept at the default. I have tried a simple if else statement which didn't work.</p>