You are here

function FMDiskFileSystem::resizeFile in N1ED - Visual editor as CKEditor plugin with Bootstrap support 7

Overrides IFMDiskFileSystem::resizeFile

File

vendor/edsdk/flmngr-server-php/src/fs/FMDiskFileSystem.php, line 288

Class

FMDiskFileSystem

Namespace

EdSDK\FlmngrServer\fs

Code

function resizeFile($filePath, $newFileNameWithoutExt, $width, $height, $mode) {

  // $filePath here starts with "/", not with "/root_dir"
  $rootDir = $this
    ->getRootDirName();
  $filePath = '/' . $rootDir . $filePath;
  $srcPath = $this
    ->getAbsolutePath($filePath);
  $index = strrpos($srcPath, "/");
  $oldFileNameWithExt = substr($srcPath, $index + 1);
  $newExt = "png";
  $oldExt = strtolower(Utils::getExt($srcPath));
  if ($oldExt === "jpg" || $oldExt === "jpeg") {
    $newExt = "jpg";
  }
  if ($oldExt === "webp") {
    $newExt = "webp";
  }
  $dstPath = substr($srcPath, 0, $index) . "/" . $newFileNameWithoutExt . "." . $newExt;
  if (Utils::getNameWithoutExt($dstPath) === Utils::getNameWithoutExt($srcPath)) {

    // This is `default` format request - we need to process the image itself without changing its extension
    $dstPath = $srcPath;
  }
  if ($mode === "IF_EXISTS" && !file_exists($dstPath)) {
    throw new MessageException(Message::createMessage(FMMessage::FM_NOT_ERROR_NOT_NEEDED_TO_UPDATE));
  }
  if ($mode === "DO_NOT_UPDATE" && file_exists($dstPath)) {
    $url = substr($dstPath, strlen($this->dirFiles) + 1);
    if (strpos($url, '/') !== 0) {
      $url = '/' . $url;
    }
    return $url;
  }
  $image = NULL;
  switch (FMDiskFileSystem::getMimeType($srcPath)) {
    case 'image/gif':
      $image = @imagecreatefromgif($srcPath);
      break;
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($srcPath);
      break;
    case 'image/png':
      $image = @imagecreatefrompng($srcPath);
      break;
    case 'image/bmp':
      $image = @imagecreatefromwbmp($srcPath);
      break;
    case 'image/webp':

      // If you get problems with WEBP preview creation, please consider updating GD > 2.2.4
      // https://stackoverflow.com/questions/59621626/converting-webp-to-jpeg-in-with-php-gd-library
      $image = @imagecreatefromwebp($srcPath);
      break;
    case 'image/svg+xml':

      // Return SVG as is
      $url = substr($srcPath, strlen($this->dirFiles) + 1);
      if (strpos($url, '/') !== 0) {
        $url = '/' . $url;
      }
      return $url;
  }

  // Somewhy it can not read ONLY SOME JPEG files, we've caught it on Windows + IIS + PHP
  // Solution from here: https://github.com/libgd/libgd/issues/206
  if (!$image) {
    $image = imagecreatefromstring(file_get_contents($srcPath));
  }

  // end of fix
  if (!$image) {
    throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
  }
  imagesavealpha($image, TRUE);
  $imageInfo = FMDiskFileSystem::getImageInfo($srcPath);
  $originalWidth = $imageInfo->width;
  $originalHeight = $imageInfo->height;
  $needToFitWidth = $originalWidth > $width && $width > 0;
  $needToFitHeight = $originalHeight > $height && $height > 0;
  if ($needToFitWidth && $needToFitHeight) {
    if ($width / $originalWidth < $height / $originalHeight) {
      $needToFitHeight = false;
    }
    else {
      $needToFitWidth = false;
    }
  }
  if (!$needToFitWidth && !$needToFitHeight) {

    // if we generated the preview in past, we need to update it in any case
    if (!file_exists($dstPath) || $newFileNameWithoutExt . "." . $oldExt === $oldFileNameWithExt) {

      // return old file due to it has correct width/height to be used as a preview
      $url = substr($srcPath, strlen($this->dirFiles) + 1);
      if (strpos($url, '/') !== 0) {
        $url = '/' . $url;
      }
      return $url;
    }
    else {
      $width = $originalWidth;
      $height = $originalHeight;
    }
  }
  if ($needToFitWidth) {
    $ratio = $width / $originalWidth;
    $height = $originalHeight * $ratio;
  }
  else {
    if ($needToFitHeight) {
      $ratio = $height / $originalHeight;
      $width = $originalWidth * $ratio;
    }
  }
  $resizedImage = imagecreatetruecolor($width, $height);
  imagealphablending($resizedImage, false);
  imagesavealpha($resizedImage, true);
  imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
  $result = FALSE;
  $ext = strtolower(Utils::getExt($dstPath));
  if ($ext === "png") {
    $result = imagepng($resizedImage, $dstPath);
  }
  else {
    if ($ext === "jpg" || $ext === "jpeg") {
      $result = imagejpeg($resizedImage, $dstPath);
    }
    else {
      if ($ext === "bmp") {
        $result = imagebmp($resizedImage, $dstPath);
      }
      else {
        if ($ext === "webp") {
          $result = imagewebp($resizedImage, $dstPath);
        }
        else {
          $result = TRUE;
        }
      }
    }
  }

  // do not resize other formats (i. e. GIF)
  if ($result === FALSE) {
    throw new MessageException(FMMessage::createMessage(FMMessage::FM_UNABLE_TO_WRITE_PREVIEW_IN_CACHE_DIR, $dstPath));
  }
  $url = substr($dstPath, strlen($this->dirFiles) + 1);
  if (strpos($url, '/') !== 0) {
    $url = '/' . $url;
  }
  return $url;
}