You are here

class Image in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/validator/Constraints/Image.php \Symfony\Component\Validator\Constraints\Image
  2. 8 core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image
  3. 8 core/lib/Drupal/Component/Utility/Image.php \Drupal\Component\Utility\Image
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image

Defines an image object to represent an image file.

Hierarchy

Expanded class hierarchy of Image

See also

\Drupal\Core\ImageToolkit\ImageToolkitInterface

\Drupal\image\ImageEffectInterface

Related topics

1 file declares its use of Image
ImageTest.php in core/tests/Drupal/Tests/Core/Image/ImageTest.php
Contains \Drupal\Tests\Core\Image\ImageTest.
11 string references to 'Image'
aggregator_entity_extra_field_info in core/modules/aggregator/aggregator.module
Implements hook_entity_extra_field_info().
DrupalImage::getButtons in core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImage.php
Returns the buttons that this plugin provides, along with metadata.
EditorImageDialog::buildForm in core/modules/editor/src/Form/EditorImageDialog.php
field.field.node.article.field_image.yml in core/profiles/standard/config/install/field.field.node.article.field_image.yml
core/profiles/standard/config/install/field.field.node.article.field_image.yml
FormTestFormStateValuesCleanAdvancedForm::buildForm in core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanAdvancedForm.php
Form constructor.

... See full list

File

core/lib/Drupal/Core/Image/Image.php, line 20
Contains \Drupal\Core\Image\Image.

Namespace

Drupal\Core\Image
View source
class Image implements ImageInterface {

  /**
   * Path of the image file.
   *
   * @var string
   */
  protected $source = '';

  /**
   * An image toolkit object.
   *
   * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
   */
  protected $toolkit;

  /**
   * File size in bytes.
   *
   * @var int
   */
  protected $fileSize;

  /**
   * Constructs a new Image object.
   *
   * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
   *   The image toolkit.
   * @param string|null $source
   *   (optional) The path to an image file, or NULL to construct the object
   *   with no image source.
   */
  public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
    $this->toolkit = $toolkit;
    if ($source) {
      $this->source = $source;
      $this
        ->getToolkit()
        ->setSource($this->source);

      // Defer image file validity check to the toolkit.
      if ($this
        ->getToolkit()
        ->parseFile()) {
        $this->fileSize = filesize($this->source);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isValid() {
    return $this
      ->getToolkit()
      ->isValid();
  }

  /**
   * {@inheritdoc}
   */
  public function getHeight() {
    return $this
      ->getToolkit()
      ->getHeight();
  }

  /**
   * {@inheritdoc}
   */
  public function getWidth() {
    return $this
      ->getToolkit()
      ->getWidth();
  }

  /**
   * {@inheritdoc}
   */
  public function getFileSize() {
    return $this->fileSize;
  }

  /**
   * {@inheritdoc}
   */
  public function getMimeType() {
    return $this
      ->getToolkit()
      ->getMimeType();
  }

  /**
   * {@inheritdoc}
   */
  public function getSource() {
    return $this->source;
  }

  /**
   * {@inheritdoc}
   */
  public function getToolkitId() {
    return $this
      ->getToolkit()
      ->getPluginId();
  }

  /**
   * {@inheritdoc}
   */
  public function getToolkit() {
    return $this->toolkit;
  }

  /**
   * {@inheritdoc}
   */
  public function save($destination = NULL) {

    // Return immediately if the image is not valid.
    if (!$this
      ->isValid()) {
      return FALSE;
    }
    $destination = $destination ?: $this
      ->getSource();
    if ($return = $this
      ->getToolkit()
      ->save($destination)) {

      // Clear the cached file size and refresh the image information.
      clearstatcache(TRUE, $destination);
      $this->fileSize = filesize($destination);
      $this->source = $destination;

      // @todo Use File utility when https://www.drupal.org/node/2050759 is in.
      if ($this
        ->chmod($destination)) {
        return $return;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function apply($operation, array $arguments = array()) {
    return $this
      ->getToolkit()
      ->apply($operation, $arguments);
  }

  /**
   * {@inheritdoc}
   */
  public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
    return $this
      ->apply('create_new', array(
      'width' => $width,
      'height' => $height,
      'extension' => $extension,
      'transparent_color' => $transparent_color,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function convert($extension) {
    return $this
      ->apply('convert', array(
      'extension' => $extension,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function crop($x, $y, $width, $height = NULL) {
    return $this
      ->apply('crop', array(
      'x' => $x,
      'y' => $y,
      'width' => $width,
      'height' => $height,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function desaturate() {
    return $this
      ->apply('desaturate', array());
  }

  /**
   * {@inheritdoc}
   */
  public function resize($width, $height) {
    return $this
      ->apply('resize', array(
      'width' => $width,
      'height' => $height,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function rotate($degrees, $background = NULL) {
    return $this
      ->apply('rotate', array(
      'degrees' => $degrees,
      'background' => $background,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function scaleAndCrop($width, $height) {
    return $this
      ->apply('scale_and_crop', array(
      'width' => $width,
      'height' => $height,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function scale($width, $height = NULL, $upscale = FALSE) {
    return $this
      ->apply('scale', array(
      'width' => $width,
      'height' => $height,
      'upscale' => $upscale,
    ));
  }

  /**
   * Provides a wrapper for drupal_chmod() to allow unit testing.
   *
   * @param string $uri
   *   A string containing a URI file, or directory path.
   * @param int $mode
   *   Integer value for the permissions. Consult PHP chmod() documentation for
   *   more information.
   *
   * @see drupal_chmod()
   *
   * @todo Remove when https://www.drupal.org/node/2050759 is in.
   *
   * @return bool
   *   TRUE for success, FALSE in the event of an error.
   */
  protected function chmod($uri, $mode = NULL) {
    return drupal_chmod($uri, $mode);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Image::$fileSize protected property File size in bytes.
Image::$source protected property Path of the image file.
Image::$toolkit protected property An image toolkit object.
Image::apply public function Applies a toolkit operation to the image. Overrides ImageInterface::apply
Image::chmod protected function Provides a wrapper for drupal_chmod() to allow unit testing.
Image::convert public function Instructs the toolkit to save the image in the format specified by the extension. Overrides ImageInterface::convert
Image::createNew public function Prepares a new image, without loading it from a file. Overrides ImageInterface::createNew
Image::crop public function Crops an image to a rectangle specified by the given dimensions. Overrides ImageInterface::crop
Image::desaturate public function Converts an image to grayscale. Overrides ImageInterface::desaturate
Image::getFileSize public function Returns the size of the image file. Overrides ImageInterface::getFileSize
Image::getHeight public function Returns the height of the image. Overrides ImageInterface::getHeight
Image::getMimeType public function Returns the MIME type of the image file. Overrides ImageInterface::getMimeType
Image::getSource public function Retrieves the source path of the image file. Overrides ImageInterface::getSource
Image::getToolkit public function Returns the image toolkit used for this image file. Overrides ImageInterface::getToolkit
Image::getToolkitId public function Returns the ID of the image toolkit used for this image file. Overrides ImageInterface::getToolkitId
Image::getWidth public function Returns the width of the image. Overrides ImageInterface::getWidth
Image::isValid public function Checks if the image is valid. Overrides ImageInterface::isValid
Image::resize public function Resizes an image to the given dimensions (ignoring aspect ratio). Overrides ImageInterface::resize
Image::rotate public function Rotates an image by the given number of degrees. Overrides ImageInterface::rotate
Image::save public function Closes the image and saves the changes to a file. Overrides ImageInterface::save
Image::scale public function Scales an image while maintaining aspect ratio. Overrides ImageInterface::scale
Image::scaleAndCrop public function Scales an image to the exact width and height given. Overrides ImageInterface::scaleAndCrop
Image::__construct public function Constructs a new Image object.