Created
August 24, 2016 11:06
-
-
Save alexmccabe/73051e608d94968b13adc3d7fcc4e3ea to your computer and use it in GitHub Desktop.
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 | |
ini_set('max_execution_time', 600); // 10 minutes | |
class OptimiseImage | |
{ | |
private $baseDir = '/'; | |
private $inputDir = 'input'; | |
private $outputDir = 'output/large'; | |
private $tmpDir = 'tmp'; | |
private $files = []; | |
private $fullDir = ''; | |
private $currentFileNameName = ''; | |
public function __construct() | |
{ | |
$baseDir = realpath(dirname(__FILE__) . '/..'); | |
$fullDir = $baseDir . '/' . $this->inputDir . '/'; | |
$this->baseDir = $baseDir; | |
$this->fullDir = $fullDir; | |
} | |
private function getFiles() | |
{ | |
$dir = $this->fullDir; | |
$files = []; | |
if (!is_dir($dir)) { | |
throw new Exception("Dir '$dir' not found", 1); | |
} | |
$files = scandir($dir); | |
// Remove the periods from the start of the array | |
$files = array_filter($files, function($item) { | |
return !is_dir('../' . $item); | |
}); | |
$this->files = $files; | |
return $files; | |
} | |
private function save($img, $quality = 80, $tmp = false) | |
{ | |
$tmpDir = $this->baseDir . '/' . $this->tmpDir; | |
$outputDir = $this->baseDir . '/' . $this->outputDir; | |
$output = ''; | |
$fileExists = false; | |
if ($tmp) { | |
if (!is_dir($tmpDir)) { | |
mkdir($tmpDir, true); | |
} | |
$output = $tmpDir . '/' . $this->currentFileName; | |
} else { | |
$output = $outputDir . '/' . $this->currentFileName; | |
} | |
$fileExists = file_exists($output); | |
if (!$fileExists) { | |
imagejpeg($img, $output, $quality); | |
} | |
} | |
private function cleanup() | |
{ | |
$this->currentFileName = ''; | |
} | |
private function compress($quality) | |
{ | |
$file = $this->currentFileName; | |
$img = imagecreatefromjpeg($this->inputDir . '/' . $file); | |
$this->save($img, $quality); | |
return $this; | |
} | |
public function optimise($quality = 80) | |
{ | |
$files = $this->getFiles(); | |
if (empty($files)) { | |
throw new Exception("Cannot process 0 files", 1); | |
} | |
foreach ($files as $file) { | |
if ($this->isValidImage($file)) { | |
$this->currentFileName = $file; | |
$this->compress($quality); | |
} | |
} | |
} | |
/** | |
* Crude check to see if a given filename extension matches a known | |
* image extension | |
* @param string $file | |
* @return boolean | |
*/ | |
public function isValidImage($file) | |
{ | |
$extension = strtolower(substr($file, strrpos($file, '.') + 1)); | |
$fileUrl = $this->inputDir . '/' . $file; | |
switch ($extension) { | |
case 'jpeg': | |
case 'jpg': | |
$validImage = @imagecreatefromjpeg($fileUrl); | |
break; | |
case 'png': | |
$validImage = @imagecreatefrompng($fileUrl); | |
break; | |
case 'gif': | |
$validImage = @imagecreatefromgif($fileUrl); | |
break; | |
default: | |
$validImage = false; | |
} | |
if (!$validImage) { | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment