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

See all posts Reply

How to only allow files to be uploaded if they are certain dimensions? new!
by George, 14 years, 11 months ago
Hi,
I'm not much of a programmer, my programmer (who I can't get a hold of at the moment) used your script (awesome, by the way!) on one of my sites, and I am trying to modify it so it only allows images to be uploaded if their dimensions are 350x280, I've spent two days trying to make this work and would appreciate any help.

The script I use is below, if anyone can make it so it only allows images to be uploaded if they are 350x280 and displays an error message otherwise I would be SO grateful (and more then willing to PayPal them $10 or so if desired).

if ($_POST['formaction'] == 'Submit') {
  if ($_FILES['image']['name'] <> '') {
    if (($_FILES['image']['type'] == 'image/gif') || ($_FILES['image']['type'] == 'image/jpeg') || ($_FILES['image']['type'] == 'image/pjpeg')) {
      if ($_FILES['image']['error'] > 0) {
        $errormsg = 'Error: '.$_FILES['image']['error'];
      } else {
        $max_preview_image_x = 350;
        $max_thumbnail_image_x = 250;
        $imagepath = '../entry/';
        $tmpUploadPath = './imgtmp/';
        $imagename = $contestid. '-' .date('YmdHis');
        ini_set("max_execution_time",0);
        $handle = new Upload($_FILES['image']);
        if ($handle->uploaded) {
          $handle->Process($tmpUploadPath);
          $info = getimagesize($handle->file_dst_pathname);
          $PreviewImageCurrentPath = $handle->file_dst_pathname;
          if ($info[0] > $max_preview_image_x) {
            $OriginalFile = $PreviewImageCurrentPath;
            $handle->image_resize          = true;
            $handle->image_ratio_y         = true;
            $handle->image_x               = $max_preview_image_x;
            $handle->Process($tmpUploadPath);
            unlink($OriginalFile);
            //echo 'delete file'. $OriginalFile;
            $PreviewImageCurrentPath = $handle->file_dst_pathname;
          }						
          copy( $PreviewImageCurrentPath, $imagepath.'L'.$imagename.'.'.$handle->file_dst_name_ext );						
          // Prepared Preview file
          // Start prepare thumbnail
          $info = getimagesize($handle->file_dst_pathname);
          $ThumbnailCurrentPath = $handle->file_dst_pathname;
          if ($info[0] > $max_thumbnail_image_x) {
            $OriginalFile = $ThumbnailCurrentPath;
            $handle->image_resize          = true;
            $handle->image_ratio_y         = true;
            $handle->image_x               = $max_thumbnail_image_x;
            $handle->Process($tmpUploadPath);
            unlink($OriginalFile);
            //echo 'delete file'. $OriginalFile;
            $ThumbnailCurrentPath = $handle->file_dst_pathname;
          }
          copy( $ThumbnailCurrentPath, $imagepath.'S'.$imagename.'.'.$handle->file_dst_name_ext );						
          unlink($ThumbnailCurrentPath);
          $GeneralImageName = $imagename.'.'.$handle->file_dst_name_ext;
          //echo 'delete file'. $ThumbnailCurrentPath;
          // insert into entry and mark new
          $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 = 'File upload failed. Please retry.';
        }
      }
    } else {
      $actionmessage = 'File format is not accepted. Only accept: .gif, .jpg and .jpeg ';
    }				
  } else {
    $actionmessage = 'Please select the file location.';
  }
}
Reply
Re: How to only allow files to be uploaded if they are certain dimensions? new!
by George, 14 years, 11 months ago
I also wanted to say that I do realize that this is probably incredibly easy to do, which makes it all the more frustrating.

Thanks!Reply
Re: How to only allow files to be uploaded if they are certain dimensions? new!
by colin, 14 years, 11 months 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