You are here

function feeds_imagegrabber_is_image in Feeds Image Grabber 7

Same name and namespace in other branches
  1. 6 feeds_imagegrabber.module \feeds_imagegrabber_is_image()

Checks that a file is an image.

This check allows the image formats identified by drupal i.e. jpeg, png, and gif. If the filename is missing an extension but is a valid image, its actual extension is added to it and the original file is renamed.

Parameters

$file: The file object.

Return value

The final file object, it may be modified or FALSE on failure.

1 call to feeds_imagegrabber_is_image()
feeds_imagegrabber_feeds_set_target in ./feeds_imagegrabber.module
Callback for mapping. Here is where the actual mapping happens.

File

./feeds_imagegrabber.module, line 588
Grabs images for items imported using the feeds module.

Code

function feeds_imagegrabber_is_image(&$file) {
  $extensions = 'jpeg jpg png gif';
  if ($file && ($filepath = $file->uri)) {
    $info = feeds_imagegrabber_get_image_info($filepath);
    if ($info && !empty($info['extension'])) {
      if (!count(file_validate_extensions($file, $extensions))) {
        return $file;
      }
      else {
        $basename = basename($filepath);
        $directory = drupal_dirname($filepath);
        if ($pos = strrpos($basename, '.')) {
          $name = substr($basename, 0, $pos);
          $ext = substr($basename, $pos);
          $regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')/i';
          if (preg_match($regex, $ext, $matches)) {
            $ext = $matches[1];
          }
          else {
            $ext = $info['extension'];
          }
          $basename = $name . '.' . $ext;
        }
        else {
          $basename .= '.' . $info['extension'];
        }
        if ($basename == basename($filepath)) {
          return $file;
        }
        $dest = file_create_filename($basename, $directory);
        if ($file = file_move($file, $dest)) {
          return $file;
        }
      }
    }
  }
  return FALSE;
}