I am using the image.module 4.7.x to have images posted on my site. If a user uploads an image with the image.module that is larger than my php.ini settings (currently 2mb) I get the warning:

The selected file /var/www/files could not be copied.

Subsequently, within my /files/images/ folder this creates files, files_0, files_1, etc... with 0kb file sizes.

Also, a new node is created with just a title (and any body text if it was entered), but no image.

I would like to check the file to be uploaded and if it is larger than my max file size limit I would like to throw an error to the user letting them know the image needs to be scaled down. Also, I don't want a node created if the file size is too large.

I entered this code within the image.module after line 156 without any luck:

// check to see if the file size is less than php.ini max limit
if (filesize($file->filepath) / 1000000 > 2 ) {
//the number "2" is for the number of megabytes
form_set_error($field_name, t('The uploaded image is too large Scale it down under 2 megabytes'));
return;
}

Has anybody got something similar working???

Comments

v1nce’s picture

Perhaps I should use the hook_validate function within image.module. Not sure how I could do this.

Here's what I have been trying without hook_validate. I'm able to pull the current php.ini max_upload_size using:

$uploadsize = file_upload_max_size($file) * 1000000;

Now i need to somehow pull the uploaded image's filesize, but I'm having trouble. Here's what I've tried:

$filesize = image_get_info($file->file_size);

So then I would do:

	  if($filesize > $uploadsize) {
  	  form_set_error($field_name, t('Uploaded file is too large scale it down under $uploadsize'));
  	  return;
  	  }

Does anybody know how to validate the uploaded image and pull it's filesize?
-------------------------------------
CommunityTraveler

v1nce’s picture

Any suggestions/thoughts?
-------------------------------------
CommunityTraveler

fersman4’s picture

Hi v1nce. I've got this exact same problem. It's hard to believe nobody else seems to have a problem with this. Anyway, did you find an answer?

vm’s picture

there is an enhanced scaling module available in the downloads area that should solve this type of problem.

have a look at the image enhanced scaling module

with it you should be able to scale images down automatically after they are uploaded so that you never have empty image files.

v1nce’s picture

I patched the image module with the below:

line 70:

<?php
$form['image_max_upload_size'] = array('#type' => 'textfield', '#title' => t('Maximum upload size'), '#default_value' => variable_get('image_max_upload_size', 800), '#size' => 12, '#description' => t('Maximum size of uploads per file, in kilobytes'));
?>

line 156-162

<?php
 	  // start code patch to image module and add max file size limit
	  if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) {
        form_set_error($field_name, t('The image you uploaded was too big. You are only allowed to upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize))));
        file_delete($file->filepath);
        return;
      }
?>

-----------------------------------------------------------------
Petafoo - We Love Animals