1
0
mirror of https://github.com/typemill/typemill.git synced 2025-04-23 05:25:31 +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)

View File

@ -213,7 +213,7 @@ class MediaApiController extends ContentController
}
$imageProcessor = new ProcessImage($this->settings['images']);
if(!$imageProcessor->checkFolders())
if(!$imageProcessor->checkFolders('images'))
{
return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
}

View File

@ -6,22 +6,22 @@ use \URLify;
class ProcessAssets
{
# holds the path to the baseFolder
protected $baseFolder;
public $baseFolder;
# holds the path to the mediaFolder
protected $mediaFolder;
public $mediaFolder;
# holds the path to the temporary image folder
protected $tmpFolder;
public $tmpFolder;
# holds the path where original images are stored
protected $originalFolder;
public $originalFolder;
# holds the path where images for frontend use are stored
protected $liveFolder;
public $liveFolder;
# holds the folder where the thumbs for the media library are stored
protected $thumbFolder;
public $thumbFolder;
# holds the folder where the thumbs for the media library are stored
public $fileFolder;
@ -43,6 +43,8 @@ class ProcessAssets
$this->thumbFolder = $this->mediaFolder . 'thumbs' . DIRECTORY_SEPARATOR;
$this->customFolder = $this->mediaFolder . 'custom' . DIRECTORY_SEPARATOR;
$this->fileFolder = $this->mediaFolder . 'files' . DIRECTORY_SEPARATOR;
$this->desiredSizes = $desiredSizes;
@ -55,7 +57,7 @@ class ProcessAssets
if($forassets == 'images')
{
$folders = [$this->mediaFolder, $this->tmpFolder, $this->originalFolder, $this->liveFolder, $this->thumbFolder];
$folders = [$this->mediaFolder, $this->tmpFolder, $this->originalFolder, $this->liveFolder, $this->thumbFolder, $this->customFolder];
}
foreach($folders as $folder)

View File

@ -257,19 +257,19 @@ class ProcessImage extends ProcessAssets
$result = false;
}
# you should not use glob but exact name with ending
/*
foreach(glob($this->originalFolder . $name) as $image)
# delete custom images (resized and grayscaled)
# array_map('unlink', glob("some/dir/*.txt"));
$pathinfo = pathinfo($name);
foreach(glob($this->customFolder . $pathinfo['filename'] . '\-*.' . $pathinfo['extension']) as $image)
{
# you could check if extension is the same here
if(!unlink($image))
{
$success = false;
}
}
*/
# array_map('unlink', glob("some/dir/*.txt"));
return $result;
}
@ -350,15 +350,8 @@ class ProcessImage extends ProcessAssets
$this->setFileName($filename, 'image', $overwrite = true);
# if($this->extension == 'jpg') $this->extension = 'jpeg';
switch($this->extension)
{
case 'gif': $image = imagecreatefromgif($this->liveFolder . $filename); break;
case 'jpg' :
case 'jpeg': $image = imagecreatefromjpeg($this->liveFolder . $filename); break;
case 'png': $image = imagecreatefrompng($this->liveFolder . $filename); break;
default: return 'image type not supported';
}
$image = $this->createImageFromPath($this->liveFolder . $filename, $this->extension);
$originalSize = $this->getImageSize($image);
@ -374,20 +367,14 @@ class ProcessImage extends ProcessAssets
return false;
}
public function generateSizesFromImageFile($filename, $image)
# filename and imagepath can be a tmp-version after upload.
public function generateSizesFromImageFile($filename, $imagePath)
{
$this->setFileName($filename, 'image');
if($this->extension == 'jpg') $this->extension = 'jpeg';
switch($this->extension)
{
case 'gif': $image = imagecreatefromgif($image); break;
case 'jpg' :
case 'jpeg': $image = imagecreatefromjpeg($image); break;
case 'png': $image = imagecreatefrompng($image); break;
default: return 'image type not supported';
}
# if($this->extension == 'jpg') $this->extension = 'jpeg';
$image = $this->createImageFromPath($imagePath, $this->extension);
$originalSize = $this->getImageSize($image);
@ -395,4 +382,27 @@ class ProcessImage extends ProcessAssets
return $resizedImages;
}
public function grayscale($imagePath, $extension)
{
$image = $this->createImageFromPath($imagePath, $extension);
imagefilter($image, IMG_FILTER_GRAYSCALE);
return $image;
}
public function createImageFromPath($imagePath, $extension)
{
switch($extension)
{
case 'gif': $image = imagecreatefromgif($imagePath); break;
case 'jpg' :
case 'jpeg': $image = imagecreatefromjpeg($imagePath); break;
case 'png': $image = imagecreatefrompng($imagePath); break;
default: return 'image type not supported';
}
return $image;
}
}