getSourceLocalPath(); if (empty($local_path)) { $source = $arguments->getSource(); if (!file_valid_uri($source)) { // The value of $source is likely a file path already. $arguments->setSourceLocalPath($source); } else { // If we can resolve the realpath of the file, then the file is local and // we can assign the actual file path. $file_system = \Drupal::service('file_system'); $path = $file_system->realpath($source); if ($path) { $arguments->setSourceLocalPath($path); } else { // We are working with a remote file, copy the remote source file to a // temp one and set the local path to it. $temp_path = $file_system->tempnam('temporary://', 'imagemagick_'); $file_system->unlink($temp_path); $temp_path .= '.' . pathinfo($source, PATHINFO_EXTENSION); $path = file_unmanaged_copy($arguments->getSource(), $temp_path, FILE_EXISTS_ERROR); $arguments->setSourceLocalPath($file_system->realpath($path)); } } } } /** * Implements hook_imagemagick_arguments_alter(). */ function imagemagick_imagemagick_arguments_alter(ImagemagickExecArguments $arguments, $command) { $config = \Drupal::config('imagemagick.settings'); // Add prepended arguments if needed. if ($prepend = $config->get('prepend')) { $arguments->add($prepend, $config->get('prepend_pre_source') ? ImagemagickExecArguments::PRE_SOURCE : ImagemagickExecArguments::POST_SOURCE, 0); } if ($command == 'convert') { // Convert destination image URI to filepath. $local_path = $arguments->getDestinationLocalPath(); if (empty($local_path)) { $destination = $arguments->getDestination(); if (!file_valid_uri($destination)) { // The value of $destination is likely a file path already. $arguments->setDestinationLocalPath($destination); } else { // If we can resolve the realpath of the file, then the file is local // and we can assign its real path. $file_system = \Drupal::service('file_system'); $path = $file_system->realpath($destination); if ($path) { $arguments->setDestinationLocalPath($path); } else { // We are working with a remote file, set the local destination to // a temp local file. $temp_path = $file_system->tempnam('temporary://', 'imagemagick_'); $file_system->unlink($temp_path); $temp_path .= '.' . pathinfo($destination, PATHINFO_EXTENSION); $arguments->setDestinationLocalPath($file_system->realpath($temp_path)); } } } // Change output image resolution to 72 ppi, if specified in settings. if (empty($arguments->find('/^\-density/')) && $density = (int) $config->get('advanced.density')) { $arguments->add("-density {$density} -units PixelsPerInch"); } // Apply color profile. if ($profile = $config->get('advanced.profile')) { if (file_exists($profile)) { $arguments->add('-profile ' . $arguments->escape($profile)); } } // Or alternatively apply colorspace. elseif ($colorspace = $config->get('advanced.colorspace')) { // Do not hi-jack settings made by effects. if (empty($arguments->find('/^\-colorspace/'))) { $arguments->add('-colorspace ' . $arguments->escape($colorspace)); } } // Change image quality. if (empty($arguments->find('/^\-quality/'))) { $arguments->add('-quality ' . \Drupal::config('imagemagick.settings')->get('quality')); } } } /** * Implements hook_imagemagick_post_save_alter(). */ function imagemagick_imagemagick_post_save_alter(ImagemagickExecArguments $arguments) { $file_system = \Drupal::service('file_system'); $destination = $arguments->getDestination(); $path = $file_system->realpath($destination); if (!$path) { // We are working with a remote file, so move the temp file to the final // destination, replacing any existinf file with the same name. file_unmanaged_move($arguments->getDestinationLocalPath(), $arguments->getDestination(), FILE_EXISTS_REPLACE); } }