Source of file FlysystemAssetStore.php
Size: 2,934 Bytes - Last Modified: 2021-12-24T07:10:34+00:00
/var/www/docs.ssmods.com/process/src/src/Flysystem/FlysystemAssetStore.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | <?php namespace Nomidi\WebPCreator\Flysystem; use SilverStripe\Assets\Flysystem\FlysystemAssetStore as SS_FlysystemAssetStore; class FlysystemAssetStore extends SS_FlysystemAssetStore { /** * @var mixed */ public $webp_quality; private static $webp_default_quality = 80; public function __construct() { $this->webp_quality = $this->config()->webp_default_quality; } public function setFromString($data, $filename, $hash = null, $variant = null, $config = []) { $fileID = $this->getFileID($filename, $hash); if ($this->getPublicFilesystem()->has($fileID)) { if ($filename) { $extension = substr(strrchr($filename, '.'), 1); $tmp_file = TEMP_PATH . DIRECTORY_SEPARATOR . 'raw_' . uniqid() . '.' . $extension; file_put_contents($tmp_file, $data); $this->createWebPImage($tmp_file, $filename, $hash, $variant); } } return parent::setFromString($data, $filename, $hash, $variant, $config); } public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = []) { if ($filename) { if (isset($config['visibility']) && self::VISIBILITY_PROTECTED === $config['visibility']) { //todo: generate protected webp image } else { $this->createWebPImage($path, $filename, $hash, $variant); } } // Submit to conflict check return parent::setFromLocalFile($path, $filename, $hash, $variant, $config); } public function createWebPImage($path, $filename, $hash, $variant = false) { if (function_exists('imagewebp') && function_exists('imagecreatefromjpeg') && function_exists('imagecreatefrompng')) { $orgpath = './' . $this->getAsURL($filename, $hash, $variant); list($width, $height, $type, $attr) = getimagesize($path); if (2 === $type) { $img = imagecreatefromjpeg($path); if ($img) { imagewebp($img, $this->createWebPName($orgpath), $this->webp_quality); } } elseif (3 === $type) { $img = imagecreatefrompng($path); if ($img) { imagesavealpha($img, true); // save alphablending setting (important) imagewebp($img, $this->createWebPName($orgpath), $this->webp_quality); } } if (isset($img)) { imagedestroy($img); } } } public function createWebPName($filename) { $picname = pathinfo($filename, PATHINFO_FILENAME); $directory = pathinfo($filename, PATHINFO_DIRNAME); $extension = pathinfo($filename, PATHINFO_EXTENSION); return $directory . '/' . $picname . '_' . $extension . '.webp'; } } |