Download & Extend

Wrong file size after resizing an uploaded image

Project:Drupal core
Version:5.x-dev
Component:upload.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

To reproduce:
1) Set maximum resolution for images to be uploaded.
2) Attach an image file exceeding the maximum resolution to a node. => drupal will show the file size reflecting the
original resolution. The wrong size information will persist even after submitting the node.

To fix:
Add one line calling clearstatcache in the function _upload_image. This will force the PHP to reread the
file size information after the resize operation. Otherwise, PHP will use the cached (wrong) file size.

function _upload_image($file) {
  $info = image_get_info($file->filepath);

  if ($info) {
    list($width, $height) = explode('x', variable_get('upload_max_resolution', 0));
    if ($width && $height) {
      $result = image_scale($file->filepath, $file->filepath, $width, $height);
      if ($result) {
        clearstatcache ();
        $file->filesize = filesize($file->filepath);
        drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => variable_get('upload_max_resolution', 0))));
      }
    }
  }

  return $file;
}

Comments

#1

Version:5.1» 5.x-dev
Status:active» needs review

This bug is still present in current development.

This is a very annoying bug! because if you set a quota of e.g. 1MB and resize all images to some smaller size, users should be able to upload e.g. 40 images that were originally 1MB. Due to this bug they are limited to 1 image, while the true uploaded image-size is less than 1MB.

I've tried your solution, and it works as told. Thanks!

I've attached a patch against 5.x-dev.

AttachmentSizeStatusTest resultOperations
upload_refresh_filesize.patch813 bytesIgnored: Check issue status.NoneNone

#2

Status:needs review» fixed

Committed to 5.x. Already fixed in 6.x.

#3

Thanks!

#4

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.