Skip to content

Instantly share code, notes, and snippets.

@SpyWSamara
Created May 24, 2019 11:49
Show Gist options
  • Save SpyWSamara/0b4c662694c20ac16523e4dd6f2d3842 to your computer and use it in GitHub Desktop.
Save SpyWSamara/0b4c662694c20ac16523e4dd6f2d3842 to your computer and use it in GitHub Desktop.
Convert bitrix resize_cache to webp
<?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