You are here

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

File

vendor/edsdk/file-uploader-server-php/src/lib/file/FileCommited.php, line 57

Class

FileCommited

Namespace

EdSDK\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 (isset($sizes[FileCommited::SIZE_PREVIEW])) {
    if (!$filePreview
      ->exists()) {
      $fileOriginalOrFull
        ->copyTo($filePreview);
    }
    $sizeName = FileCommited::SIZE_PREVIEW;
    $targetSize = $sizes->{$sizeName};
    if ($targetSize->width != $currPreviewWidth || $targetSize->height != $currPreviewHeight) {

      // Target size differs from current
      if ($targetSize->width > 0 || $targetSize->height > 0) {

        // not fully auto
        if ($targetSize->width < $currOriginalWidth || $targetSize->height < $currOriginalHeight || $targetSize->enlarge) {

          // We reduce size of image or have enlarge allowed
          $filePreview
            ->resizeImage($targetSize);
        }
      }
    }
  }
  if (isset($sizes[FileCommited::SIZE_FULL])) {
    $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();
          }
        }
      }
    }
  }
}