getSourceLocalPath(); if (empty($local_path)) { $source = $toolkit->getSource(); if (!file_valid_uri($source)) { // The value of $source is likely a file path already. $toolkit->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) { $toolkit->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($toolkit->getSource(), $temp_path, FILE_EXISTS_ERROR); $toolkit->setSourceLocalPath($file_system->realpath($path)); } } } } /** * Implements hook_imagemagick_arguments_alter(). */ function imagemagick_imagemagick_arguments_alter(ImagemagickToolkit $toolkit, $command) { $config = \Drupal::config('imagemagick.settings'); // Add prepended arguments if needed. if ($prepend = $config->get('prepend')) { $toolkit->prependArgument($prepend); } if ($command == 'convert') { // Convert destination image URI to filepath. $local_path = $toolkit->getDestinationLocalPath(); if (empty($local_path)) { $destination = $toolkit->getDestination(); if (!file_valid_uri($destination)) { // The value of $destination is likely a file path already. $toolkit->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) { $toolkit->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); $toolkit->setDestinationLocalPath($file_system->realpath($temp_path)); } } } // Change image density. if ($toolkit->findArgument('-density') === FALSE && $density = (int) $config->get('advanced.density')) { $toolkit->addArgument("-density {$density} -units PixelsPerInch"); } // Apply color profile. if ($profile = $config->get('advanced.profile')) { if (file_exists($profile)) { $toolkit->addArgument('-profile ' . $toolkit->escapeShellArg($profile)); } } // Or alternatively apply colorspace. elseif ($colorspace = $config->get('advanced.colorspace')) { // Do not hi-jack settings made by effects. if ($toolkit->findArgument('-colorspace') === FALSE) { $toolkit->addArgument('-colorspace ' . $toolkit->escapeShellArg($colorspace)); } } // Change image quality. if ($toolkit->findArgument('-quality') === FALSE) { $toolkit->addArgument('-quality ' . \Drupal::config('imagemagick.settings')->get('quality')); } } } /** * Implements hook_imagemagick_post_save_alter(). */ function imagemagick_imagemagick_post_save_alter(ImagemagickToolkit $toolkit) { $file_system = \Drupal::service('file_system'); $destination = $toolkit->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($toolkit->getDestinationLocalPath(), $toolkit->getDestination(), FILE_EXISTS_REPLACE); } }