Skip to content

Instantly share code, notes, and snippets.

@zaak
Last active November 16, 2020 00:00
Show Gist options
  • Save zaak/c093044f0931157b7ac13d8b05b1e743 to your computer and use it in GitHub Desktop.
Save zaak/c093044f0931157b7ac13d8b05b1e743 to your computer and use it in GitHub Desktop.
<?php
/**
* CKFinder 3 PHP connector plugin that pre-generates *public* thumbnails after file upload.
*
* 1. Save this file in an appropriate plugin directory (see http://docs.cksource.com/ckfinder3-php/plugins.html).
* The directory structure should look like below.
*
* plugins
* └── ThumbPregenerator
* └── ThumbPregenerator.php
*
* 2. Enable plugin in config.php.
*
* $config['plugins'] = array('ThumbPregenerator');
*
*/
namespace CKSource\CKFinder\Plugin\ThumbPregenerator;
use CKSource\CKFinder\CKFinder;
use CKSource\CKFinder\Config;
use CKSource\CKFinder\Event\AfterCommandEvent;
use CKSource\CKFinder\Event\CKFinderEvent;
use CKSource\CKFinder\Filesystem\File\File;
use CKSource\CKFinder\Image;
use CKSource\CKFinder\Plugin\PluginInterface;
use CKSource\CKFinder\ResizedImage\ResizedImageRepository;
use League\Flysystem\Cached\CachedAdapter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ThumbPregenerator implements PluginInterface, EventSubscriberInterface
{
/**
* @var CKFinder
*/
protected $app;
public function setContainer(CKFinder $app)
{
$this->app = $app;
}
public function getDefaultConfig()
{
return [];
}
public function pregenerateThumbs(AfterCommandEvent $event)
{
/* @var $workingFolder \CKSource\CKFinder\Filesystem\Folder\WorkingFolder */
$workingFolder = $this->app['working_folder'];
/** @var ResizedImageRepository $resizedImagesRepository */
$resizedImagesRepository = $this->app['resized_image_repository'];
/** @var Config $config */
$config = $this->app['config'];
$responseData = $event->getResponse()->getData();
$fileName = $responseData['fileName'];
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (!Image::isSupportedExtension($ext)) {
return;
}
if (null === $fileName || !File::isValidName($fileName, $this->app['config']->get('disallowUnsafeCharacters'))) {
return;
}
$adapter = $workingFolder->getBackend()->getAdapter();
if ($adapter instanceof CachedAdapter) {
// An ugly hack to flush the cache - there's a bug that will be fixed in CKFinder 3.4.
$flushCache = function(CachedAdapter $adapter) {
return $adapter->cache->flush();
};
$flushCache = \Closure::bind($flushCache, null, $adapter);
$flushCache($adapter);
}
if ($workingFolder->containsFile($fileName)) {
$sizes = $config->get('images.sizes');
foreach ($sizes as $size) {
$resizedImagesRepository->getResizedImage(
$workingFolder->getResourceType(),
$workingFolder->getClientCurrentFolder(),
$fileName,
$size['width'],
$size['height']
);
}
}
}
public static function getSubscribedEvents()
{
return [CKFinderEvent::AFTER_COMMAND_FILE_UPLOAD => 'pregenerateThumbs'];
}
}
@TlalocW
Copy link

TlalocW commented Jul 6, 2020

I'm getting the gist of this code, but I'm not figuring out how to change the folder where the resized image goes. Any help would be appreciated.

@michaelpalacio
Copy link

Dear zaak, just moved to CKFinder 3.5.1 from v2 with Martyna's help and we love it. We only miss the ability of pregenerate thumbnails in a public folder such as "__tumbs" This (your) CKF3 plugin "ThumbPregenerator.php" almost does it, but we are not "real programers" as we can only edit config.php , php.ini or ThumbPregenerator.php and light things like that but not fully understand PHP, hence this request for a ThumbPregenerator2.php that would work as CKFinder 2 did and we believe it could benefit also other users.
For example as of today, if you drop two pictures e.g. boat1.jpg and boat2.jpg to CKF3 then "ThumbPregenerator.php" will generate two folders with 3 thumbnails each.
We were used to (and use a LOT) only one thumbnail per photo and all thumbnails together in the same folder with the ability to edit max width, height and quality.
Please see the attached picture. Do you see any value in this request?
To a non pro like me, it appears to be simpler than your current ThumbPregenerator.php listed hereabout you know better.
Kind regards and that you, michaelpalacio

ThumbPregenerator-desired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment