You are here

public function FileCommited::applySizes in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Apply sized method.

File

src/Flmngr/FileUploaderServer/lib/file/FileCommited.php, line 84

Class

FileCommited
Commited file (data structure about the file of finished upload transaction). Has method for resizing images (applying sizes when finishing transaction).

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\lib\file

Code

public function applySizes($sizes) {
  if (!$this
    ->isImage()) {
    return;
  }
  $currPreviewWidth = -1;
  $currPreviewHeight = -1;
  $filePreview = $this
    ->getFilePreview();
  if ($filePreview
    ->exists()) {
    $currPreviewWidth = $filePreview
      ->getImageWidth();
    $currPreviewHeight = $filePreview
      ->getImageHeight();
  }
  $currFullWidth = $this
    ->getImageWidth();
  $currFullHeight = $this
    ->getImageHeight();
  $fileOriginal = $this
    ->getFileOriginal();
  $fileOriginalOrFull = $this;
  if ($fileOriginal
    ->exists()) {
    $fileOriginalOrFull = $fileOriginal;
  }
  $currOriginalWidth = $fileOriginalOrFull
    ->getImageWidth();
  $currOriginalHeight = $fileOriginalOrFull
    ->getImageHeight();
  if (array_key_exists(FileCommited::SIZE_PREVIEW, $sizes)) {
    if (!$filePreview
      ->exists()) {
      $fileOriginalOrFull
        ->copyTo($filePreview);
    }
    $sizeName = FileCommited::SIZE_PREVIEW;
    $targetSize = $sizes->{$sizeName};

    // Target size differs from current.
    if ($targetSize->width != $currPreviewWidth || $targetSize->height != $currPreviewHeight) {

      // Not fully auto.
      if ($targetSize->width > 0 || $targetSize->height > 0) {

        // We reduce size of image or have enlarge allowed.
        if ($targetSize->width < $currOriginalWidth || $targetSize->height < $currOriginalHeight || $targetSize->enlarge) {
          $filePreview
            ->resizeImage($targetSize);
        }
      }
    }
  }
  if (array_key_exists(FileCommited::SIZE_FULL, $sizes)) {
    $sizeName = FileCommited::SIZE_FULL;
    $targetSize = $sizes->{$sizeName};
    if ($targetSize->width != $currFullWidth || $targetSize->height != $currFullHeight) {
      if ($targetSize->width > 0 || $targetSize->height > 0) {
        if ($targetSize->width < $currOriginalWidth || $targetSize->height < $currOriginalHeight || $targetSize->enlarge) {
          $originalExisted = $fileOriginal
            ->exists();
          if (!$originalExisted) {
            $this
              ->copyTo($fileOriginal);
          }
          if (!$this
            ->resizeImage($targetSize) && !$originalExisted) {
            $fileOriginal
              ->delete();
          }
        }
      }
    }
  }
}