Skip to content
TestToolkit.php 4.8 KiB
Newer Older
 * Contains \Drupal\image_test\Plugin\ImageToolkit\TestToolkit.
namespace Drupal\image_test\Plugin\ImageToolkit;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\ImageToolkit\ImageToolkitBase;

/**
 * Defines a Test toolkit for image manipulation within Drupal.
 *
 *   id = "test",
 *   title = @Translation("A dummy toolkit that works")
 * )
 */
class TestToolkit extends ImageToolkitBase {
  /**
   * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
   *
   * @var int
   */
  protected $type;

  /**
   * The width of the image.
   *
   * @var int
   */
  protected $width;

  /**
   * The height of the image.
   *
   * @var int
   */
  protected $height;

   */
  public function settingsForm() {
    $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;
  public function settingsFormSubmit($form, FormStateInterface $form_state) {
    \Drupal::config('system.image.test_toolkit')
      ->set('test_parameter', $form_state['values']['test']['test_parameter'])
      ->save();
  }
    $data = @getimagesize($this->getImage()->getSource());
    if ($data && in_array($data[2], static::supportedTypes())) {
      $this->setType($data[2]);
      $this->width = $data[0];
      $this->height = $data[1];
    // 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
   *   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();
    // 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);
  /**
   * 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).
   *
   */
  public function setType($type) {
    if (in_array($type, static::supportedTypes())) {
      $this->type = $type;
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
    return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
  }

   */
  public static function isAvailable() {
    return TRUE;
  }
  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() {
    return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
  }

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