Currently a corrupt image uploaded to the drupal will fail any image operations. Imagecache then just feeds up the raw image to the browser. For some reason browsers are a bit more cooperative with corrupt images then image processing packages and I can see the uploaded image albeit with several artifacts in it.

This is a problem where I am trying to process a very large image to make it fit on my page - Because
a) there is no way to communicate to my end-users that the uploaded file is corrupt. I would like instead to display a "bad-picture" icon on it.
b) the error messages displayed by image.inc or the imageapi libraries are very technical in nature and not very friendly.

What I did was override the theme_imagecache() in my own theme and replaced with something like so - for the 5.x-1.3 version. Could similar support be added to the module?

function _is_valid_image($source) {
  $info = image_get_info($source);
  if (!$info) {
    return FALSE;
  }

  $im = image_gd_open($source, $info['extension']);
  if (!$im) {
    return FALSE;
  }

  return TRUE;
}

function phptemplate_imagecache($namespace, $path, $alt='', $title='', $attributes = NULL) {
  //Make sure that this is a valid image. This is a performance hit
  //because we have to open the file every time.. but have no choice..
  //drupals image.inc and imagecache do not handle image errors very well.
  $a = _is_valid_image(realpath($path));
  if ($a === false) {
    //clear the imagecreate error messages we know are there..
    //if there are other error messages then we lose them - look for a way to only remove image errors
    unset($_SESSION['messages']['error']);

    //Get this from a user configurable setting or use an internal image shipped with the module.. Or
    //create this image on the fly - however best if use the simplest approach during error handling and bundle
    //default "badimage" icon.
    $path = 'files/badimage.png';
  }

  //better if the link to the image can be separated from the displayed image.. i.e. clicking on the
  //displayed image below for a "bad image" will display the original version of the bad image. It should instead
  //point to the original corrupt "path".
  return theme_imagecache($namespace, $path, $alt, $title, $attributes);
}

Comments

dopry’s picture

Status: Active » Needs work

Can you propose this as a patch for easier review?

dopry’s picture

Version: 5.x-1.3 » 6.x-2.x-dev
Assigned: Unassigned » dopry
Status: Needs work » Postponed

I would actually prefer to see this as a cron driven sanitizer... There is another patch for cleaning up cached images whose source no longer exists.. I think would could probably put some gd_opens in the process and make sure corrupted images get cleaned up...

We should see this in ImageCache 2.1

fizk’s picture

Assigned: dopry » Unassigned
Status: Postponed » Closed (won't fix)

Should be added in an external module.