Last active
December 14, 2015 23:58
-
-
Save hagbarddenstore/5169176 to your computer and use it in GitHub Desktop.
An image resizer tool written in PHP.
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 | |
class Console | |
{ | |
public static function WriteLine($string) | |
{ | |
echo $string . PHP_EOL; | |
} | |
public static function ReadLine() | |
{ | |
return trim(fgets(STDIN)); | |
} | |
} | |
function FindImages($directory) | |
{ | |
$images = array(); | |
$files = scandir($directory); | |
foreach ($files as $file) | |
{ | |
if ($file == '.' || $file == '..') | |
{ | |
continue; | |
} | |
$file = $directory . DIRECTORY_SEPARATOR . $file; | |
if (!is_dir($file)) | |
{ | |
if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), array('jpg', 'png', 'jpeg'))) | |
{ | |
$images[] = $file; | |
} | |
} | |
else | |
{ | |
$images = array_merge($images, FindImages($file)); | |
} | |
} | |
return $images; | |
} | |
if (count($argv) < 3) | |
{ | |
Console::WriteLine('Usage: resize.php $searchDirectory $destinationDirectory [-size=300x300]'); | |
die(1); | |
} | |
$searchDirectory = realpath($argv[1]); | |
$destinationDirectory = realpath(dirname($argv[2])) . DIRECTORY_SEPARATOR . basename($argv[2]); | |
if (!file_exists($searchDirectory)) | |
{ | |
Console::WriteLine('Error: Search directory doesn\'t exist.'); | |
die(0); | |
} | |
if (file_exists($destinationDirectory)) | |
{ | |
Console::WriteLine('Warning: Destination directory exists and files might be overwritten.'); | |
Console::WriteLine('Continue? (yes/no)'); | |
if (Console::ReadLine() == 'no') | |
{ | |
die(0); | |
} | |
} | |
else | |
{ | |
Console::WriteLine('Creating destination directory: ' . $destinationDirectory); | |
mkdir($destinationDirectory); | |
} | |
$height = 300; | |
$width = 300; | |
foreach ($argv as $argument) | |
{ | |
if (substr_compare($argument, '-size=', 0, min(strlen($argument), 6), true) == 0) | |
{ | |
$size = explode('x', substr($argument, strpos($argument, '=') + 1)); | |
if (count($size) != 2) | |
{ | |
Console::WriteLine('Invalid format for argument: ' . $argument); | |
die(1); | |
} | |
if (is_numeric($size[0])) | |
{ | |
$width = intval($size[0]); | |
} | |
if (is_numeric($size[1])) | |
{ | |
$height = intval($size[1]); | |
} | |
break; | |
} | |
} | |
$images = FindImages($searchDirectory); | |
if (count($images) == 0) | |
{ | |
Console::WriteLine('No images found to resize. Exiting...'); | |
die(0); | |
} | |
foreach ($images as $image) | |
{ | |
Console::WriteLine('Processing file "' . $image . '"'); | |
$folders = dirname(str_replace($searchDirectory, '', $image)); | |
if (!file_exists($destinationDirectory . $folders)) | |
{ | |
mkdir($destinationDirectory . $folders, 0777, true); | |
} | |
if ($folders == DIRECTORY_SEPARATOR) | |
{ | |
$folders = ''; | |
} | |
$filename = basename($image); | |
$destinationFile = $destinationDirectory . $folders . DIRECTORY_SEPARATOR . $filename; | |
$destinationImage = imagecreatetruecolor($width, $height); | |
$white = imagecolorallocate($destinationImage, 0xFF, 0xFF, 0xFF); | |
imagefill($destinationImage, 0, 0, $white); | |
switch(strtolower(pathinfo($image, PATHINFO_EXTENSION))) | |
{ | |
case 'jpg': | |
case 'jpeg': | |
$sourceImage = imagecreatefromjpeg($image); | |
break; | |
case 'png': | |
$sourceImage = imagecreatefrompng($image); | |
break; | |
default: | |
Console::WriteLine('Unrecognized file-extension.'); | |
continue 2; | |
break; | |
} | |
$imageWidth = imagesx($sourceImage); | |
$imageHeight = imagesy($sourceImage); | |
if ($imageWidth > $imageHeight) | |
{ | |
$ratio = $imageHeight / $imageWidth; | |
$sourceWidth = $width; | |
$sourceHeight = $width * $ratio; | |
$y = floor(($height - $sourceHeight) / 2); | |
$x = 0; | |
} | |
else if ($imageWidth < $imageHeight) | |
{ | |
$ratio = $imageWidth / $imageHeight; | |
$sourceHeight = $height; | |
$sourceWidth = $height * $ratio; | |
$x = floor(($width - $sourceWidth) / 2); | |
$y = 0; | |
} | |
else | |
{ | |
$ratio = 1; | |
$sourceHeight = $height; | |
$sourceWidth = $width; | |
$x = 0; | |
$y = 0; | |
} | |
imagecopyresampled($destinationImage, $sourceImage, $x, $y, 0, 0, $sourceWidth, $sourceHeight, $imageWidth, $imageHeight); | |
switch(strtolower(pathinfo($image, PATHINFO_EXTENSION))) | |
{ | |
case 'jpg': | |
case 'jpeg': | |
imagejpeg($destinationImage, $destinationFile, 90); | |
break; | |
case 'png': | |
imagepng($destinationImage, $destinationFile, 8); | |
break; | |
default: | |
Console::WriteLine('Unrecognized file-extension.'); | |
break; | |
} | |
imagedestroy($sourceImage); | |
imagedestroy($destinationImage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment