Created
May 24, 2019 11:49
-
-
Save SpyWSamara/0b4c662694c20ac16523e4dd6f2d3842 to your computer and use it in GitHub Desktop.
Convert bitrix resize_cache to webp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Bitrix\Main\Diag\Debug; | |
Debug::startTimeLabel('convert'); | |
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(\Bitrix\main\Application::getDocumentRoot().'/upload/resize_cache')); | |
$files = []; | |
foreach ($iterator as $entity) { | |
if ($entity->isDir()) { | |
continue; | |
} | |
$origin = $entity->getRealPath(); | |
$webp = $entity->getRealPath().'.webp'; | |
if (\file_exists($webp)) { | |
continue; | |
} | |
$ext = \mb_strtolower($entity->getExtension()); | |
if ('png' === $ext || 'jpg' === $ext || 'jpeg' === $ext) { | |
if ('png' === $ext) { | |
$image = \imagecreatefrompng($origin); | |
} else { | |
$image = \imagecreatefromjpeg($origin); | |
} | |
if ($image) { | |
\imagewebp($image, $webp); | |
} else { | |
\dump('error: '.$origin); | |
} | |
} | |
} | |
Debug::endTimeLabel('convert'); | |
\dump(Debug::getTimeLabels()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment