You are here

trait FontOperationTrait in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/FontOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\FontOperationTrait
  2. 8 src/Plugin/ImageToolkit/Operation/FontOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\FontOperationTrait

Base trait for image toolkit operations that require font handling.

Hierarchy

2 files declare their use of FontOperationTrait
TextOverlay.php in src/Plugin/ImageToolkit/Operation/gd/TextOverlay.php
TextToWrapper.php in src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php

File

src/Plugin/ImageToolkit/Operation/FontOperationTrait.php, line 8

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation
View source
trait FontOperationTrait {

  /**
   * Return the path of the font file.
   *
   * The imagettf* GD functions, and ImageMagick toolkit, do not allow use of
   * URIs to specify files. Always resolve the font file to a local path.
   *
   * @param string $font_uri
   *   The font URI.
   *
   * @return string
   *   The local path of the font file.
   */
  protected function getFontPath($font_uri) {
    if (!$font_uri) {
      throw new \InvalidArgumentException('Font file not specified');
    }

    // Determine if the $font_uri is a real URI or a local path.
    $uri_wrapper = \Drupal::service('stream_wrapper_manager')
      ->getViaUri($font_uri);

    // If local path, return it.
    if ($uri_wrapper === FALSE) {
      return $font_uri;
    }

    // Determine if a local path can be resolved for the URI. If so, return it.
    $local_path = $uri_wrapper
      ->realpath();
    if ($local_path !== FALSE) {
      return $local_path;
    }

    // If no local path available, the file may be stored in a remote file
    // system. Use the file metadata manager service to copy the file to local
    // temp and keep it there for further access within same request. It is not
    // necessary to load its metadata.
    $file = \Drupal::service('file_metadata_manager')
      ->uri($font_uri);
    $local_path = $file
      ->getLocalTempPath();
    if ($local_path !== NULL) {
      return $local_path;
    }
    elseif ($file
      ->copyUriToTemp() === TRUE) {
      return $file
        ->getLocalTempPath();
    }

    // None of the above worked, file can not be accessed.
    throw new \InvalidArgumentException("Cannot access font file '{$font_uri}'");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FontOperationTrait::getFontPath protected function Return the path of the font file.