You are here

class GD in ImageAPI Optimize GD 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/ImageAPIOptimizeProcessor/GD.php \Drupal\imageapi_optimize_gd\Plugin\ImageAPIOptimizeProcessor\GD

Provides a ImageAPI Optimize processor for GD.

Plugin annotation


@ImageAPIOptimizeProcessor(
  id = "imageapi_optimize_gd",
  label = @Translation("GD"),
  description = @Translation("Adjust quality of JPEG and WebP Images with GD.")
)

Hierarchy

Expanded class hierarchy of GD

File

src/Plugin/ImageAPIOptimizeProcessor/GD.php, line 17

Namespace

Drupal\imageapi_optimize_gd\Plugin\ImageAPIOptimizeProcessor
View source
class GD extends ConfigurableImageAPIOptimizeProcessorBase {

  /**
   * {@inheritdoc}
   */
  public function applyToImage($image_uri) {
    $success = FALSE;

    // Confirm GD library exists.
    if (function_exists('imagegd2')) {
      if (in_array($this
        ->getMimeType($image_uri), $this->configuration['file_types'])) {
        $image = $this
          ->getImageFactory()
          ->get($image_uri, 'gd');
        if (!$image
          ->isValid()) {
          return FALSE;
        }

        // Get the correct function based on file type.
        $function = 'image' . image_type_to_extension($image
          ->getToolkit()
          ->getType(), FALSE);
        if (function_exists($function)) {

          // Convert stream wrapper URI to normal path.
          $destination = \Drupal::service('file_system')
            ->realpath($image_uri);
          $success = $function($image
            ->getToolkit()
            ->getResource(), $destination, $this->configuration['quality']);
        }
      }
    }
    else {
      $this->logger
        ->notice('The PHP GD library must be installed for the ImageAPI Optimize GD module to process images.');
    }
    return $success;
  }

  /**
   * Returns the image factory.
   *
   * @return \Drupal\Core\Image\ImageFactory
   *   The image factory.
   */
  protected function getImageFactory() {
    return \Drupal::service('image.factory');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'quality' => 75,
      'file_types' => [
        'image/jpeg',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['quality'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Image quality'),
      '#description' => $this
        ->t('Specify the image quality.'),
      '#default_value' => $this->configuration['quality'],
      '#required' => TRUE,
      '#min' => 1,
      '#max' => 100,
    ];
    $form['file_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('File Types'),
      '#options' => [
        'image/jpeg' => 'JPEG',
        'image/webp' => 'WebP',
      ],
      '#default_value' => $this->configuration['file_types'],
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['quality'] = $form_state
      ->getValue('quality');
    $this->configuration['file_types'] = array_filter($form_state
      ->getValue('file_types'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableImageAPIOptimizeProcessorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
GD::applyToImage public function Apply this image optimize processor to the given image. Overrides ImageAPIOptimizeProcessorInterface::applyToImage
GD::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
GD::defaultConfiguration public function Gets default configuration for this plugin. Overrides ImageAPIOptimizeProcessorBase::defaultConfiguration
GD::getImageFactory protected function Returns the image factory.
GD::submitConfigurationForm public function Form submission handler. Overrides ConfigurableImageAPIOptimizeProcessorBase::submitConfigurationForm
ImageAPIOptimizeProcessorBase::$imageFactory protected property The image factory.
ImageAPIOptimizeProcessorBase::$logger protected property A logger instance.
ImageAPIOptimizeProcessorBase::$uuid protected property The image optimize processor ID.
ImageAPIOptimizeProcessorBase::$weight protected property The weight of the image optimize processor.
ImageAPIOptimizeProcessorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ImageAPIOptimizeProcessorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ImageAPIOptimizeProcessorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ImageAPIOptimizeProcessorBase::getMimeType protected function
ImageAPIOptimizeProcessorBase::getSummary public function Returns a render array summarizing the configuration of the image optimize processor. Overrides ImageAPIOptimizeProcessorInterface::getSummary
ImageAPIOptimizeProcessorBase::getUuid public function Returns the unique ID representing the image optimize processor. Overrides ImageAPIOptimizeProcessorInterface::getUuid
ImageAPIOptimizeProcessorBase::getWeight public function Returns the weight of the image optimize processor. Overrides ImageAPIOptimizeProcessorInterface::getWeight
ImageAPIOptimizeProcessorBase::label public function Returns the image optimize processor label. Overrides ImageAPIOptimizeProcessorInterface::label
ImageAPIOptimizeProcessorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ImageAPIOptimizeProcessorBase::setWeight public function Sets the weight for this image optimize processor. Overrides ImageAPIOptimizeProcessorInterface::setWeight
ImageAPIOptimizeProcessorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.