You are here

public function BrightcoveVideo::saveImage in Brightcove Video Connect 8.2

Same name and namespace in other branches
  1. 8 src/Entity/BrightcoveVideo.php \Drupal\brightcove\Entity\BrightcoveVideo::saveImage()
  2. 3.x src/Entity/BrightcoveVideo.php \Drupal\brightcove\Entity\BrightcoveVideo::saveImage()

Helper function to save the image for the entity.

Parameters

string $type: The type of the image. Possible values are:

  • IMAGE_TYPE_THUMBNAIL: Recommended aspect ratio of 16:9 and a minimum width of 640px.
  • IMAGE_TYPE_POSTER: Recommended aspect ratio of 16:9 and a minimum width of 160px.

\Brightcove\Item\Video\Image|array $image: The image from Brightcove.

Return value

\Drupal\brightcove\BrightcoveVideoInterface The called Brightcove Video.

Throws

\Exception If the type is not matched with the possible types.

Overrides BrightcoveVideoInterface::saveImage

File

src/Entity/BrightcoveVideo.php, line 124

Class

BrightcoveVideo
Defines the Brightcove Video entity.

Namespace

Drupal\brightcove\Entity

Code

public function saveImage($type, $image) {

  // Prepare function name and throw an exception if it doesn't match for an
  // existing function.
  $image_dir = 'public://';
  switch ($type) {
    case self::IMAGE_TYPE_THUMBNAIL:
      $image_dir .= self::VIDEOS_IMAGES_THUMBNAILS_DIR;
      break;
    case self::IMAGE_TYPE_POSTER:
      $image_dir .= self::VIDEOS_IMAGES_POSTERS_DIR;
      break;
    default:
      throw new \Exception(t("Invalid type given: @type, the type argument must be either '@thumbnail' or '@poster'.", [
        '@type' => $type,
        '@thumbnail' => self::IMAGE_TYPE_THUMBNAIL,
        '@poster' => self::IMAGE_TYPE_POSTER,
      ]));
  }

  // Make type's first character uppercase for correct function name.
  $function = ucfirst($type);
  $needs_save = TRUE;

  // Try to get the image's filename from Brightcove.
  // @TODO: Find a drupal friendly solution for file handling.
  preg_match('/\\/(?!.*\\/)([\\w\\.-]+)/i', $img_src = is_array($image) ? $image['src'] : $image
    ->getSrc(), $matches);
  if (isset($matches[1])) {

    // Get entity's image file.

    /** @var \Drupal\file\Entity\File $file */
    $entity_image = $this
      ->{"get{$function}"}();

    // If the entity already has an image then load and delete it.
    if (!empty($entity_image['target_id'])) {
      $file = File::load($entity_image['target_id']);
      if (!is_null($file) && $file
        ->getFileName() != $matches[1]) {
        $this
          ->{"set{$function}"}(NULL);
      }
      elseif (!is_null($file)) {
        $file_path = \Drupal::service('file_system')
          ->realpath($file
          ->getFileUri());
        $file_md5 = md5_file($file_path);
        $image_md5 = md5_file($image
          ->getSrc());
        if ($file_md5 !== FALSE && $file_md5 === $image_md5) {
          $needs_save = FALSE;
        }
      }
    }

    // Save the new image from Brightcove to the entity.
    if ($needs_save) {
      $image_content = file_get_contents($img_src);

      // Prepare directory and if it was a success try to save the image.
      if (file_prepare_directory($image_dir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY)) {
        $image_name = $matches[1];
        $file = file_save_data($image_content, "{$image_dir}/{$image_name}");

        // Set image if there was no error.
        if ($file !== FALSE) {
          $this
            ->{"set{$function}"}($file
            ->id());
        }
      }
    }
  }
  return $this;
}