-
-
Save zaak/7bb61025bfd9d3a6633029b0b4b087f9 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* CKFinder 3 PHP connector plugin that pre-generates 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\Event\AfterCommandEvent; | |
use CKSource\CKFinder\Event\CKFinderEvent; | |
use CKSource\CKFinder\Filesystem\File\File; | |
use CKSource\CKFinder\Image; | |
use CKSource\CKFinder\Plugin\PluginInterface; | |
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']; | |
$thumbRepository = $this->app['thumbnail_repository']; | |
$responseData = $event->getResponse()->getData(); | |
$fileName = $responseData['fileName']; | |
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); | |
if (!Image::isSupportedExtension($ext, $thumbRepository->isBitmapSupportEnabled())) { | |
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 = [150, 300, 500]; | |
foreach ($sizes as $size) { | |
$thumbRepository->getThumbnail( | |
$workingFolder->getResourceType(), | |
$workingFolder->getClientCurrentFolder(), | |
$fileName, | |
$size, | |
$size); | |
} | |
} | |
} | |
public static function getSubscribedEvents() | |
{ | |
return [CKFinderEvent::AFTER_COMMAND_FILE_UPLOAD => 'pregenerateThumbs']; | |
} | |
} |
Hi @Yahav. Just checked in the latest version and it seems to work wine 😉 Is there any error thrown?
No error thrown
It does get to the getThumbnail function and it does seem to return an array with the thumb data but thumbnail never gets written to disk nor the thumbnails dir is created..
@zaak Well i misunderstood the whole point of this plugin, it seems that the plugin will pregenerate the internal (cached) thumbnails while i though it will generate a resized version of the image which will be stored at __thumbs under the actual resource folder..
guess i'll have to continue my googling about this.
@Yahav: I have added another gist of a plugin that works as you described - please have a look here. On upload, the plugin will create rescaled versions of images according to sizes defined in images.sizes
config option.
Please let me know if it helped.
Great! Thank you, i'll have a look into it :)
This doesn't seem to work anymore, thumbnail doesn't get saved to disk. :(