1
0
mirror of https://github.com/typemill/typemill.git synced 2025-08-01 11:50:28 +02:00

Version 1.3.7: Resize and grayscale images on the fly

This commit is contained in:
trendschau
2020-06-08 15:45:26 +02:00
parent 76ab04d217
commit 021f487fe7
4 changed files with 156 additions and 34 deletions

View File

@@ -2,6 +2,8 @@
namespace Typemill;
use Typemill\Models\ProcessImage;
class Assets
{
protected $baseUrl;
@@ -16,6 +18,114 @@ class Assets
$this->editorJS = array();
$this->editorInlineJS = array();
$this->svgSymbols = array();
$this->imageUrl = false;
$this->imageFolder = 'original';
}
public function image($url)
{
$this->imageUrl = $url;
return $this;
}
public function resize($width,$height)
{
$pathinfo = pathinfo($this->imageUrl);
$extension = strtolower($pathinfo['extension']);
$imageName = $pathinfo['filename'];
$desiredSizes = ['custom' => []];
$resize = '-';
if(ctype_digit($width) && $width < 10000)
{
$resize .= $width;
$desiredSizes['custom']['width'] = $width;
}
$resize .= 'x';
if(ctype_digit($height) && $height < 10000)
{
$resize .= $height;
$desiredSizes['custom']['height'] = $height;
}
$processImage = new ProcessImage($desiredSizes);
$processImage->checkFolders('images');
$imageNameResized = $imageName . $resize;
$imagePathResized = $processImage->customFolder . $imageNameResized . '.' . $extension;
$imageUrlResized = 'media/custom/' . $imageNameResized . '.' . $extension;
if(!file_exists( $imagePathResized ))
{
# if custom version does not exist, use original version for resizing
$imageFolder = ($this->imageFolder == 'original') ? $processImage->originalFolder : $processImage->customFolder;
$imagePath = $imageFolder . $pathinfo['basename'];
$resizedImage = $processImage->generateSizesFromImageFile($imageUrlResized, $imagePath);
$savedImage = $processImage->saveImage($processImage->customFolder, $resizedImage['custom'], $imageNameResized, $extension);
if(!$savedImage)
{
# return old image url without resize
return $this;
}
}
# set folder to custom, so that the next method uses the correct (resized) version
$this->imageFolder = 'custom';
$this->imageUrl = $imageUrlResized;
return $this;
}
public function grayscale()
{
$pathinfo = pathinfo($this->imageUrl);
$extension = strtolower($pathinfo['extension']);
$imageName = $pathinfo['filename'];
$processImage = new ProcessImage([]);
$processImage->checkFolders('images');
$imageNameGrayscale = $imageName . '-grayscale';
$imagePathGrayscale = $processImage->customFolder . $imageNameGrayscale . '.' . $extension;
$imageUrlGrayscale = 'media/custom/' . $imageNameGrayscale . '.' . $extension;
if(!file_exists( $imagePathGrayscale ))
{
# if custom-version does not exist, use live-version for grayscale-manipulation.
$imageFolder = ($this->imageFolder == 'original') ? $processImage->liveFolder : $processImage->customFolder;
$imagePath = $imageFolder . $pathinfo['basename'];
$grayscaleImage = $processImage->grayscale($imagePath, $extension);
$savedImage = $processImage->saveImage($processImage->customFolder, $grayscaleImage, $imageNameGrayscale, $extension);
if(!$savedImage)
{
# return old image url without resize
return $this;
}
}
# set folder to custom, so that the next method uses the correct (resized) version
$this->imageFolder = 'custom';
$this->imageUrl = $imageUrlGrayscale;
return $this;
}
public function src()
{
return $this->imageUrl;
}
public function addCSS($CSS)