You are here

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

1 call to FileCommited::resizeImage()
FileCommited::applySizes in vendor/edsdk/file-uploader-server-php/src/lib/file/FileCommited.php

File

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

Class

FileCommited

Namespace

EdSDK\FileUploaderServer\lib\file

Code

public function resizeImage($targetSize) {
  if ($this->m_config
    ->getMaxImageResizeWidth() > 0 && $targetSize->width > $this->m_config
    ->getMaxImageResizeWidth()) {
    throw new MessageException(Message::createMessage(Message::MAX_RESIZE_WIDTH_EXCEEDED, "" . $targetSize->width, $this
      ->getName(), "" . $this->m_config
      ->getMaxImageResizeWidth()));
  }
  if ($this->m_config
    ->getMaxImageResizeHeight() > 0 && $targetSize->height > $this->m_config
    ->getMaxImageResizeHeight()) {
    throw new MessageException(Message::createMessage(Message::MAX_RESIZE_HEIGHT_EXCEEDED, "" . $targetSize->height, $this
      ->getName(), "" . $this->m_config
      ->getMaxImageResizeHeight()));
  }
  $fileSrc = $this;
  if ($this->m_mainFile != null) {

    // if this is just a size of main file
    $fileSrc = $this->m_mainFile;
  }
  $fileOriginal = $fileSrc
    ->getFileOriginal();
  if ($fileOriginal
    ->exists()) {
    $fileSrc = $fileOriginal;
  }
  $imageWidth = $this
    ->getImageWidth();
  $imageHeight = $this
    ->getImageHeight();
  if ($targetSize->width == 0 && $targetSize->height == 0) {
    return false;
  }
  if ($targetSize->width == 0 && $targetSize->height == $imageHeight) {
    return false;
  }
  if ($targetSize->height == 0 && $targetSize->width == $imageWidth) {
    return false;
  }
  if ($targetSize->width > 0 && $targetSize->height > 0 && $targetSize->width == $imageWidth && $targetSize->height == $imageHeight) {
    return false;
  }

  // Calc full target size of image (with paddings)
  $scaleWWithPadding = -1;
  $scaleHWithPadding = -1;
  if ($targetSize->width > 0 && $targetSize->height > 0) {
    $scaleWWithPadding = $targetSize->width;
    $scaleHWithPadding = $targetSize->height;
  }
  else {
    if ($targetSize->width > 0) {
      $scaleWWithPadding = $targetSize->width;
      $scaleHWithPadding = floor($scaleWWithPadding / $imageWidth * $imageHeight);
    }
    else {
      if ($targetSize->height > 0) {
        $scaleHWithPadding = $targetSize->height;
        $scaleWWithPadding = floor($scaleHWithPadding / $imageHeight * $imageWidth);
      }
    }
  }
  if (($scaleWWithPadding > $imageWidth || $scaleHWithPadding > $imageHeight) && !$targetSize->enlarge) {
    $scaleWWithPadding = $imageWidth;
    $scaleHWithPadding = $imageHeight;
  }

  // Check we have not exceeded max width/height
  if ($this->m_config
    ->getMaxImageResizeWidth() > 0 && $scaleWWithPadding > $this->m_config
    ->getMaxImageResizeWidth() || $this->m_config
    ->getMaxImageResizeHeight() > 0 && $scaleHWithPadding > $this->m_config
    ->getMaxImageResizeHeight()) {
    $coef = max($scaleWWithPadding / $this->m_config
      ->getMaxImageResizeWidth(), $scaleHWithPadding / $this->m_config
      ->getMaxImageResizeHeight());
    $scaleWWithPadding = floor($scaleWWithPadding / $coef);
    $scaleHWithPadding = floor($scaleHWithPadding / $coef);
  }

  // Calc actual size of image (without paddings)
  $scaleW = -1;
  $scaleH = -1;
  if ($scaleWWithPadding / $imageWidth < $scaleHWithPadding / $imageHeight) {
    $scaleW = $scaleWWithPadding;
    $scaleH = floor($scaleW / $imageWidth * $imageHeight);
  }
  else {
    $scaleH = $scaleHWithPadding;
    $scaleW = floor($scaleH / $imageHeight * $imageWidth);
  }
  if ($scaleWWithPadding == $imageWidth && $scaleW == $imageWidth && $scaleHWithPadding == $imageHeight && $scaleH == $imageHeight) {
    return false;
  }

  // no resize is needed
  $fitMode = FileCommited::FIT_EXACT;
  if ($targetSize->width == 0) {
    $fitMode = FileCommited::FIT_TO_HEIGHT;
  }
  else {
    if ($targetSize->height == 0) {
      $fitMode = FileCommited::FIT_TO_WIDTH;
    }
  }
  $image = FileCommited::resizeImageNative($this
    ->getImage(), $scaleW, $scaleH, $fitMode);
  if ($scaleWWithPadding > $scaleW || $scaleHWithPadding > $scaleH) {
    $image = $this
      ->addPaddingsToImageNative($image, $scaleW, $scaleH, $scaleWWithPadding, $scaleHWithPadding);
  }
  $this
    ->writeImage($image);
  return true;
}