本文整理汇总了PHP中FileCache::needTinify方法的典型用法代码示例。如果您正苦于以下问题:PHP FileCache::needTinify方法的具体用法?PHP FileCache::needTinify怎么用?PHP FileCache::needTinify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileCache
的用法示例。
在下文中一共展示了FileCache::needTinify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: tinifyDirectory
private function tinifyDirectory($dir, $recursive, &$report)
{
if (!file_exists($dir)) {
$this->logWarning($dir . ' does not exist', $report);
return;
}
if ($recursive) {
$Files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
} else {
$Files = new FilesystemIterator($dir);
}
$extensions = array("png", "jpg", "jpeg");
$pictures = array();
foreach ($Files as $fileinfo) {
if ($fileinfo->isFile() && in_array(strtolower($fileinfo->getExtension()), $extensions)) {
$pictures[] = $fileinfo;
}
}
$dirFileCounter = 0;
$dirGainSize = 0;
// Sort by size
usort($pictures, function ($a, $b) {
if ($a->getSize() == $b->getSize()) {
return 0;
}
return $a->getSize() > $b->getSize() ? -1 : 1;
});
foreach ($pictures as $fileinfo) {
$object = new FileCache($fileinfo);
if ($object->needTinify()) {
set_time_limit(10);
$filename = $object->getPathname();
$fileSize = $object->getSize();
$object->tinify();
$newFileSize = $object->getSize();
$gainSize = $fileSize - $newFileSize;
$dirFileCounter++;
$dirGainSize += $gainSize;
$report->update($gainSize);
if (isset($this->logger)) {
$this->logger->logInfo('[' . $filename . '] ' . $fileSize . '=>' . $newFileSize . ' (' . (100 - (int) ($newFileSize * 100 / $fileSize)) . '%)');
}
}
}
if (isset($this->logger)) {
$this->logger->logInfo('[' . $dir . ']: ' . $dirFileCounter . ' files(s), gain: ' . (int) ($dirGainSize / 1024) . 'Ko');
}
}