Reply to Re: How to only allow files to be uploaded if they are certain dimensions?

Re: How to only allow files to be uploaded if they are certain dimensions? new!
by colin, 15 years ago
Your code is quite complex, and if doing a lot of things that the class can do. You can let the class check on the MIME type, on the image dimension, on the validity of the upload, etc...
So I have rewritten it, using the class capabilities; it is much simpler.

However, I have not tested it, I just wrote it quickly. But it should be a good starting point, and you should have no difficulties fixing it up if it doesn't work right away.

if ($_POST['formaction'] == 'Submit') {
  $max_preview_image_x = 350;
  $max_thumbnail_image_x = 250;
  $imagepath = '../entry/';
  $imagename = $contestid. '-' .date('YmdHis');
  ini_set("max_execution_time",0);
  $handle = new Upload($_FILES['image']);
  if ($handle->uploaded) {
    //  the following lines restricts the uploaded files to only be pictures
    $handle->allowed('image/*');
    // the two following lines set the maximum accepted dimensions
    $handle->image_max_width    = 530px;
    $handle->image_max_height   = 280px;
    // first process the 'L' image
    $handle->image_resize       = true;
    $handle->image_ratio_y      = true;
    $handle->image_x            = $max_preview_image_x;
    $handle->file_new_name_body = 'L'.$imagename;
    $handle->Process($imagepath);
    if ($handle->processed) {		
      // then process the 'S' image
      $handle->image_resize       = true;
      $handle->image_ratio_y      = true;
      $handle->image_x            = $max_thumbnail_image_x;
      $handle->file_new_name_body = 'S'.$imagename;
      $handle->Process($imagepath);
      if ($handle->processed) {		
        // all good
        $GeneralImageName = $imagename.'.'.$handle->file_dst_name_ext;
        $timestamp = date('Y-m-d H:i:s');
        $query = "INSERT INTO entries (contestid, designerid, entryimage, entryscore, entrytimestamp, entrynew  ) VALUES ('$contestid', '$designerloginid', '$GeneralImageName', 0, '$timestamp', 'T')";
        mysql_query($query, $link);
        mysql_free_result($result);
        $actionmessage = "Entry is submitted.";
      } else {
        $actionmessage = $handle->error;
      }
    } else {
      $actionmessage = $handle->error;
    }
  } else {
    $actionmessage = 'File upload failed. Please retry.' . $handle->error;
  }
  $handle->clean();
}

If this helped you, of course feel free to donate a little bit ;)Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important