Chris@0: toolkit = $toolkit; Chris@0: if ($source) { Chris@0: $this->source = $source; Chris@0: $this->getToolkit()->setSource($this->source); Chris@0: // Defer image file validity check to the toolkit. Chris@0: if ($this->getToolkit()->parseFile()) { Chris@0: $this->fileSize = filesize($this->source); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isValid() { Chris@0: return $this->getToolkit()->isValid(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getHeight() { Chris@0: return $this->getToolkit()->getHeight(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getWidth() { Chris@0: return $this->getToolkit()->getWidth(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFileSize() { Chris@0: return $this->fileSize; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMimeType() { Chris@0: return $this->getToolkit()->getMimeType(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSource() { Chris@0: return $this->source; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getToolkitId() { Chris@0: return $this->getToolkit()->getPluginId(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getToolkit() { Chris@0: return $this->toolkit; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save($destination = NULL) { Chris@0: // Return immediately if the image is not valid. Chris@0: if (!$this->isValid()) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: $destination = $destination ?: $this->getSource(); Chris@0: if ($return = $this->getToolkit()->save($destination)) { Chris@0: // Clear the cached file size and refresh the image information. Chris@0: clearstatcache(TRUE, $destination); Chris@0: $this->fileSize = filesize($destination); Chris@0: $this->source = $destination; Chris@0: Chris@18: if (\Drupal::service('file_system')->chmod($destination)) { Chris@0: return $return; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function apply($operation, array $arguments = []) { Chris@0: return $this->getToolkit()->apply($operation, $arguments); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') { Chris@0: return $this->apply('create_new', ['width' => $width, 'height' => $height, 'extension' => $extension, 'transparent_color' => $transparent_color]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function convert($extension) { Chris@0: return $this->apply('convert', ['extension' => $extension]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function crop($x, $y, $width, $height = NULL) { Chris@0: return $this->apply('crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function desaturate() { Chris@0: return $this->apply('desaturate', []); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function resize($width, $height) { Chris@0: return $this->apply('resize', ['width' => $width, 'height' => $height]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function rotate($degrees, $background = NULL) { Chris@0: return $this->apply('rotate', ['degrees' => $degrees, 'background' => $background]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function scaleAndCrop($width, $height) { Chris@0: return $this->apply('scale_and_crop', ['width' => $width, 'height' => $height]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function scale($width, $height = NULL, $upscale = FALSE) { Chris@0: return $this->apply('scale', ['width' => $width, 'height' => $height, 'upscale' => $upscale]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides a wrapper for drupal_chmod() to allow unit testing. Chris@0: * Chris@0: * @param string $uri Chris@0: * A string containing a URI file, or directory path. Chris@0: * @param int $mode Chris@0: * Integer value for the permissions. Consult PHP chmod() documentation for Chris@0: * more information. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE for success, FALSE in the event of an error. Chris@18: * Chris@18: * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Chris@18: * Use \Drupal\Core\File\FileSystem::chmod(). Chris@18: * Chris@18: * @see \Drupal\Core\File\FileSystemInterface::chmod() Chris@18: * @see https://www.drupal.org/node/2418133 Chris@0: */ Chris@0: protected function chmod($uri, $mode = NULL) { Chris@18: @trigger_error('chmod() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::chmod(). See https://www.drupal.org/node/2418133.', E_USER_DEPRECATED); Chris@18: return \Drupal::service('file_system')->chmod($uri, $mode); Chris@0: } Chris@0: Chris@0: }