You are here

public function FMDiskFileSystem::getImagePreview in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Gets image preview.

Overrides FMDiskFileSystemInterface::getImagePreview

1 call to FMDiskFileSystem::getImagePreview()
FMDiskFileSystem::getFiles in src/Flmngr/FlmngrServer/fs/FMDiskFileSystem.php
Gets a files list.

File

src/Flmngr/FlmngrServer/fs/FMDiskFileSystem.php, line 523

Class

FMDiskFileSystem
Implements file system interface. Provides an interface to access file system (local disc FS). This is the correct module to replace if you want to implement some custom file system support (i. e. network file system like Amazon S3).

Namespace

Drupal\n1ed\Flmngr\FlmngrServer\fs

Code

public function getImagePreview($fullPath, $width, $height) {
  $hash = md5($width . $height . filesize($fullPath) . filemtime($fullPath));
  if (!file_exists($this->dirCache)) {
    if (!mkdir($this->dirCache)) {
      throw new MessageException(FMMessage::createMessage(FMMessage::FM_UNABLE_TO_CREATE_DIRECTORY));
    }
  }
  $fileCachedPath = $this->dirCache . DIRECTORY_SEPARATOR . $hash . '.png';
  if (!file_exists($fileCachedPath)) {
    $image = NULL;
    switch (FMDiskFileSystem::getMimeType($fullPath)) {
      case 'image/gif':
        $image = @imagecreatefromgif($fullPath);
        break;
      case 'image/jpeg':
        $image = @imagecreatefromjpeg($fullPath);
        break;
      case 'image/png':
        $image = @imagecreatefrompng($fullPath);
        break;
      case 'image/bmp':
        $image = @imagecreatefromwbmp($fullPath);
        break;
      case 'image/webp':
        $image = @imagecreatefromwebp($fullPath);
        break;
      case 'image/svg+xml':
        return [
          "image/svg+xml",
          fopen($fullPath, 'rb'),
        ];
    }

    // 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($fullPath));
    }
    if (!$image) {
      throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
    }
    imagesavealpha($image, TRUE);
    $imageInfo = FMDiskFileSystem::getImageInfo($fullPath);

    // Ratio thumb.
    $ratio_thumb = $width / $height;
    $xx = $imageInfo->width;
    $yy = $imageInfo->height;

    // Ratio original.
    $ratio_original = $xx / $yy;
    if ($ratio_original >= $ratio_thumb) {
      $yo = $yy;
      $xo = ceil($yo * $width / $height);
      $xo_ini = ceil(($xx - $xo) / 2);
      $xy_ini = 0;
    }
    else {
      $xo = $xx;
      $yo = ceil($xo * $height / $width);
      $xy_ini = ceil(($yy - $yo) / 2);
      $xo_ini = 0;
    }
    $resizedImage = imagecreatetruecolor($width, $height);
    imagecopyresampled($resizedImage, $image, 0, 0, $xo_ini, $xy_ini, $width, $height, $xo, $yo);
    if (imagepng($resizedImage, $fileCachedPath) === FALSE) {
      throw new MessageException(FMMessage::createMessage(FMMessage::FM_UNABLE_TO_WRITE_PREVIEW_IN_CACHE_DIR, $fileCachedPath));
    }
  }
  $f = fopen($fileCachedPath, 'rb');
  if ($f) {
    return [
      "image/png",
      $f,
    ];
  }
  throw new MessageException(FMMessage::createMessage(FMMessage::FM_FILE_DOES_NOT_EXIST));
}