You are here

function focal_point_migrate_imagefield_focus_data in Focal Point 7

Convert existing imagefield_focus data to focal point.

Parameters

bool $overwrite: If TRUE, existing focal point data will be overwritten.

Return value

int|null Returns the number of images that were converted or NULL.

1 call to focal_point_migrate_imagefield_focus_data()
focal_point_install in ./focal_point.install
Implements hook_install().

File

./focal_point.install, line 74
Install hooks for focal_point.

Code

function focal_point_migrate_imagefield_focus_data($overwrite = FALSE) {
  if (module_exists('imagefield_focus')) {

    // Get all imagefield_focus data, calculate the center of the focus rect,
    // and use that as the new focal_point.
    $select = db_select('imagefield_focus_file', 'iff');
    if ($overwrite) {
      $select
        ->join('focal_point', 'fp', 'iff.fid = fp.fid');
    }
    $select
      ->addField('iff', 'fid');
    $select
      ->addField('iff', 'focus_rect');
    $rows = $select
      ->execute()
      ->fetchAllAssoc('fid');
    foreach ($rows as $row) {
      $file = file_load($row->fid);
      list($x1, $y1, $x2, $y2) = explode(',', $row->focus_rect);
      $focal_point_x = $x1 + round($x2 / 2);
      $focal_point_y = $y1 + round($y2 / 2);
      $file_info = image_get_info($file->uri);
      $focal_point = round(100 * $focal_point_x / $file_info['width']) . ',' . round(100 * $focal_point_y / $file_info['height']);
      focal_point_save($file->fid, $focal_point);
    }
    return count($rows);
  }
  return NULL;
}