/** * {@inheritdoc} */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { // Don't do entity saving when we have validation erors. if (count($form_state->getErrors()) || !$form_state->isValidationComplete()) { return parent::massageFormValues($values, $form, $form_state); } foreach ($values as $value) { if (isset($value['fids'][0]) && isset($value['cropinfo']) && $value['cropinfo']['changed']) { $fid = $value['fids'][0]; $new_crop_info = array('fid' => $fid, 'x' => $value['cropinfo']['x'], 'y' => $value['cropinfo']['y'], 'width' => $value['cropinfo']['width'], 'height' => $value['cropinfo']['height']); imagefield_crop_update_file_info($new_crop_info); $source = File::load($fid); if ($source) { image_path_flush($source->getFileUri()); } } } return parent::massageFormValues($values, $form, $form_state); }
/** * {@inheritdoc} * * @throws \Symfony\Component\Validator\Exception\InvalidArgumentException */ public function content($fid, $focal_point_value) { $output = []; $file = File::load($fid); $image = \Drupal::service('image.factory')->get($file->getFileUri()); if (!$image->isValid()) { throw new InvalidArgumentException('The file with id = $fid is not an image.'); } $styles = $this->getFocalPointImageStyles(); // Since we are about to create a new preview of this image, we first must // flush the old one. This should not be a performance hit since there is // no good reason for anyone to preview an image unless they are changing // the focal point value. image_path_flush($image->getSource()); $derivative_images = []; $derivative_image_note = ''; $original_image = [ '#theme' => 'image', '#uri' => $image->getSource(), '#alt' => t('Focal Point Preview Image'), '#attributes' => [ 'id' => 'focal-point-preview-image', ], ]; if (!empty($styles)) { foreach ($styles as $style) { $style_label = $style->get('label'); $url = $this->buildUrl($style, $file, $focal_point_value); $derivative_images[$style->getName()] = [ 'style' => $style_label, 'url' => $url, 'image' => [ '#theme' => 'image', '#uri' => $url, '#alt' => $this->t('Focal Point Preview: %label', array('%label' => $style_label)), '#attributes' => [ 'class' => ['focal-point-derivative-preview-image'], ], ], ]; } $derivative_image_note = $this->t('Click an image to see a larger preview. You may need to scroll horizontally for more image styles.'); } else { // There are no styles that use a focal point effect to preview. drupal_set_message($this->t('You must have at least one <a href=":url">image styles</a> defined that uses a focal point effect in order to preview.', array(':url' => '/admin/config/media/image-styles')), 'warning'); } $output['focal_point_preview_page'] = array( '#theme' => "focal_point_preview_page", "#original_image" => $original_image, '#derivative_images' => $derivative_images, '#focal_point' => $focal_point_value, '#preview_image_note' => $this->t('This preview image above may have been scaled to fit on the page.'), '#derivative_image_note' => $derivative_image_note, ); return $output; }
/** * {@inheritdoc} */ public function save() { parent::save(); image_path_flush($this->uri->value); }
/** * Implements \Drupal\focal_point\FocalPoint::flush(). * * Flush all image derivatives for the given file. * * @param int $fid */ public function flush($fid) { $file = File::load($fid); image_path_flush($file->getFileUri()); }