Migrate to Focal Point from EPSA Crop (epsacrop)

Created on 23 September 2016, over 8 years ago
Updated 11 January 2024, about 1 year ago

Not sure if this is the right place to put it, but I wanted this code to be available for others:

I decided to move from Epsa Crop ( https://www.drupal.org/project/epsacrop β†’ ) to Focal Point and wrote a function to migrate existing croppings.

Not sure if this works for every situation, I quickly wrote this up for a custom site, where it worked great. Not all EPSAcrop features are supported (esp. multiple croppings, only the first cropping is ever used).

If wanted, this can be included within the focal_point module.

Copy this to a new file "focal_point.drush.inc" and then run "drush focal-point-migrate-epsacrop", wait, enjoy.

/**
 * Implements hook_drush_command().
 */
function focal_point_drush_command() {
  $items = array();
  $items['focal-point-migrate-epsacrop'] = array(
    'callback' => 'focal_point_drush_migrateepsacrop',
    'description' => 'Migrate cropping data from EPSA Crop to Focal Point.'
  );
  return $items;
}

function focal_point_drush_migrateepsacrop() {
  $res = db_query("SELECT * from {epsacrop_files} ORDER BY fid ASC");
  while ($row = $res->fetchObject()) {
    $fid = $row->fid;
    $coords = unserialize($row->coords);
    if (is_string($coords)) {
      $coords = drupal_json_decode($coords);
    }
    else {
      continue;
    }
    if (empty($coords[$fid])) {
      continue;
    }

    $data = reset($coords[$fid]);
    $file = file_load($fid);
    $focal_point_x = $data['x1'] + round($data['w'] / 2);
    $focal_point_y = $data['y1'] + round($data['h'] / 2);
    $file_info = $file->metadata;

    if (!empty($file_info['width']) && !empty($file_info['height']) && 
      $focal_point_x <= $file_info['width'] && $focal_point_y <= $file_info['height']) {
      $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);
    }
  }
}
πŸ“Œ Task
Status

Needs review

Version

1.0

Component

Other Code

Created by

πŸ‡©πŸ‡ͺGermany Frando

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States firewaller

    I refactored the above for D10:

    /**
     * Implements hook_entity_presave().
     */
    function MY_MODULE_entity_presave(EntityInterface $entity): void {
      // Skip if not file.
      if (!$entity instanceof FileInterface) {
        return;
      }
    
      // Migrate epsa crop to focal point.
      $fid = $entity->id();
      $migrate_database = Database::getConnection('default', 'migrate');
      $query = $migrate_database->select('epsacrop_files', 'e');
      $query->innerJoin('file_metadata', 'fmw', "fmw.fid = e.fid AND fmw.name = 'width'");
      $query->innerJoin('file_metadata', 'fmh', "fmh.fid = e.fid AND fmh.name = 'height'");
      $query->addField('e', 'coords');
      $query->addField('fmw', 'value', 'width');
      $query->addField('fmh', 'value', 'height');
      $query->condition('e.fid', $fid);
      $query->range(0, 1);
      $results = $query->execute();
      if ($results) {
        /** @var \Drupal\focal_point\FocalPointManagerInterface $focal_point_manager */
        $focal_point_manager = \Drupal::service('focal_point.manager');
        $crop_type = \Drupal::config('focal_point.settings')->get('crop_type');
        $crop = $focal_point_manager->getCropEntity($entity, $crop_type);
        while ($record = $results->fetchAssoc()) {
          $coords = unserialize($record['coords']);
          $coords = Json::decode($coords);
          if (isset($coords[$fid])) {
            // Get first image style.
            $coords = reset($coords[$fid]);
            $focal_point_x = $coords['x'] + round($coords['w'] / 2);
            $focal_point_y = $coords['y'] + round($coords['h'] / 2);
            $width = unserialize($record['width']);
            $height = unserialize($record['height']);
            // Skip if invalid.
            if (
              $focal_point_x > $width ||
              $focal_point_y > $height
            ) {
              continue;
            }
            $relative = $focal_point_manager->absoluteToRelative($focal_point_x, $focal_point_y, $width, $height);
            $x = $relative['x'];
            $y = $relative['y'];
            $focal_point = implode(',', $relative);
            if (
              $focal_point_manager->validateFocalPoint($focal_point) &&
              $width &&
              $height
            ) {
              $focal_point_manager->saveCropEntity($x, $y, $width, $height, $crop);
            }
          }
        }
      }
    }
    
Production build 0.71.5 2024