function FMDiskFileSystem::getImagePreview in N1ED - Visual editor as CKEditor plugin with Bootstrap support 7
Overrides IFMDiskFileSystem::getImagePreview
File
- vendor/
edsdk/ flmngr-server-php/ src/ fs/ FMDiskFileSystem.php, line 517
Class
Namespace
EdSDK\FlmngrServer\fsCode
function getImagePreview($filePath, $width, $height) {
$fullPath = $this
->getAbsolutePath($filePath);
$hash = md5($filePath . $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 . '/' . $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':
// 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($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));
}
// end of fix
if (!$image) {
throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
}
imagesavealpha($image, TRUE);
// TODO:
// throw new MessageException(FMMessage.createMessage(FMMessage.FM_UNABLE_TO_CREATE_PREVIEW));
$imageInfo = FMDiskFileSystem::getImageInfo($fullPath);
$ratio_thumb = $width / $height;
// ratio thumb
$xx = $imageInfo->width;
$yy = $imageInfo->height;
$ratio_original = $xx / $yy;
// ratio original
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));
}