You are here

function _image_update_7002_populate_dimensions in Drupal 7

Populate image dimensions in a specific table.

Parameters

$table: The name of the database table to be updated.

$columns: Keyed array of columns this table is supposed to have.

$last_fid: The fid of the last image to have been processed.

Return value

The number of images that were processed.

Related topics

1 call to _image_update_7002_populate_dimensions()
image_update_7002 in modules/image/image.install
Add width and height columns to image field schema and populate.

File

modules/image/image.install, line 314
Install, update and uninstall functions for the image module.

Code

function _image_update_7002_populate_dimensions($table, $field_name, &$last_fid) {

  // Define how many images to process per pass.
  $images_per_pass = 100;

  // Query the database for fid / URI pairs.
  $query = db_select($table, NULL, array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $query
    ->join('file_managed', NULL, $table . '.' . $field_name . '_fid = file_managed.fid');
  if ($last_fid) {
    $query
      ->condition('file_managed.fid', $last_fid, '>');
  }
  $result = $query
    ->fields('file_managed', array(
    'fid',
    'uri',
  ))
    ->orderBy('file_managed.fid')
    ->range(0, $images_per_pass)
    ->execute();
  $count = 0;
  foreach ($result as $file) {
    $count++;
    $info = image_get_info($file['uri']);
    if (is_array($info)) {
      db_update($table)
        ->fields(array(
        $field_name . '_width' => $info['width'],
        $field_name . '_height' => $info['height'],
      ))
        ->condition($field_name . '_fid', $file['fid'])
        ->execute();
    }
  }

  // If less than the requested number of rows were returned then this table
  // has been fully processed.
  $last_fid = $count < $images_per_pass ? NULL : $file['fid'];
  return $count;
}