Newer
Older
<?php
/**
* @file
* Contains \Drupal\image_test\Plugin\ImageToolkit\TestToolkit.
*/
namespace Drupal\image_test\Plugin\ImageToolkit;
Alex Pott
committed
use Drupal\Component\Utility\Unicode;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Alex Pott
committed
use Drupal\Core\ImageToolkit\ImageToolkitBase;
/**
* Defines a Test toolkit for image manipulation within Drupal.
*
Alex Pott
committed
* @ImageToolkit(
* id = "test",
* title = @Translation("A dummy toolkit that works")
* )
*/
Alex Pott
committed
class TestToolkit extends ImageToolkitBase {
Alex Pott
committed
/**
* Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
*
* @var int
*/
protected $type;
catch
committed
/**
* The width of the image.
*
* @var int
*/
protected $width;
/**
* The height of the image.
*
* @var int
*/
protected $height;
/**
* {@inheritdoc}
*/
public function settingsForm() {
Alex Pott
committed
$this->logCall('settings', func_get_args());
Alex Pott
committed
$form['test_parameter'] = array(
'#type' => 'number',
'#title' => $this->t('Test toolkit parameter'),
'#description' => $this->t('A toolkit parameter for testing purposes.'),
'#min' => 0,
'#max' => 100,
'#default_value' => \Drupal::config('system.image.test_toolkit')->get('test_parameter'),
);
return $form;
}
/**
* {@inheritdoc}
Dries Buytaert
committed
public function settingsFormSubmit($form, FormStateInterface $form_state) {
Alex Pott
committed
\Drupal::config('system.image.test_toolkit')
->set('test_parameter', $form_state['values']['test']['test_parameter'])
->save();
}
/**
* {@inheritdoc}
catch
committed
public function parseFile() {
Alex Pott
committed
$this->logCall('parseFile', func_get_args());
catch
committed
$data = @getimagesize($this->getImage()->getSource());
Alex Pott
committed
if ($data && in_array($data[2], static::supportedTypes())) {
$this->setType($data[2]);
catch
committed
$this->width = $data[0];
$this->height = $data[1];
Alex Pott
committed
return TRUE;
Alex Pott
committed
}
Alex Pott
committed
return FALSE;
}
/**
* {@inheritdoc}
catch
committed
public function save($destination) {
Alex Pott
committed
$this->logCall('save', func_get_args());
// Return false so that image_save() doesn't try to chmod the destination
// file that we didn't bother to create.
return FALSE;
}
/**
* Stores the values passed to a toolkit call.
*
* @param string $op
Alex Pott
committed
* One of the toolkit methods 'parseFile', 'save', 'settings', or 'apply'.
* @param array $args
* Values passed to hook.
*
* @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset()
* @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls()
*/
protected function logCall($op, $args) {
$results = \Drupal::state()->get('image_test.results') ?: array();
$results[$op][] = $args;
Alex Pott
committed
// A call to apply is also logged under its operation name whereby the
// array of arguments are logged as separate arguments, this because at the
// ImageInterface level we still have methods named after the operations.
if ($op === 'apply') {
$operation = array_shift($args);
$results[$operation][] = array_values(reset($args));
}
\Drupal::state()->set('image_test.results', $results);
}
catch
committed
/**
* {@inheritdoc}
*/
catch
committed
public function getWidth() {
catch
committed
return $this->width;
}
/**
* {@inheritdoc}
*/
catch
committed
public function getHeight() {
catch
committed
return $this->height;
}
Alex Pott
committed
/**
* Returns the type of the image.
*
* @return int
* The image type represented by a PHP IMAGETYPE_* constant (e.g.
* IMAGETYPE_JPEG).
*/
public function getType() {
return $this->type;
}
/**
* Sets the PHP type of the image.
*
* @param int $type
* The image type represented by a PHP IMAGETYPE_* constant (e.g.
* IMAGETYPE_JPEG).
*
Alex Pott
committed
* @return $this
Alex Pott
committed
*/
public function setType($type) {
if (in_array($type, static::supportedTypes())) {
$this->type = $type;
}
return $this;
}
/**
* {@inheritdoc}
*/
catch
committed
public function getMimeType() {
Alex Pott
committed
return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
}
/**
* {@inheritdoc}
*/
public static function isAvailable() {
return TRUE;
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function getSupportedExtensions() {
$extensions = array();
foreach (static::supportedTypes() as $image_type) {
$extensions[] = Unicode::strtolower(image_type_to_extension($image_type, FALSE));
}
return $extensions;
}
/**
* Returns a list of image types supported by the toolkit.
*
* @return array
* An array of available image types. An image type is represented by a PHP
* IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
*/
protected static function supportedTypes() {
Alex Pott
committed
return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function apply($operation, array $arguments = array()) {
$this->logCall('apply', func_get_args());
return TRUE;
}