Removed getOriginalFiles and refactored image processing

This commit is contained in:
Isaac 2017-11-24 20:45:41 -08:00
parent 249b98311e
commit 2f498f44d3

View file

@ -24,6 +24,7 @@ use External;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Config; use Config;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
/** /**
@ -124,27 +125,7 @@ class Image extends Model
$image->ensureDirectoryExists(); $image->ensureDirectoryExists();
foreach (self::$ImageTypes as $coverType) { foreach (self::$ImageTypes as $coverType) {
if ($coverType['id'] === self::ORIGINAL && $image->mime === 'image/jpeg') { self::processFile($file, $image->getFile($coverType['id']), $coverType);
$command = 'cp "'.$file->getPathname().'" '.$image->getFile($coverType['id']);
} else {
// ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php
$command = 'convert 2>&1 "'.$file->getPathname().'" -background white -alpha remove -alpha off -strip';
if ($image->mime === 'image/jpeg') {
$command .= ' -quality 100 -format jpeg';
} else {
$command .= ' -quality 95 -format png';
}
if (isset($coverType['geometry'])) {
$command .= " -gravity center -thumbnail ${coverType['geometry']} -extent ${coverType['geometry']}";
}
$command .= ' "'.$image->getFile($coverType['id']).'"';
}
External::execute($command);
chmod($image->getFile($coverType['id']), 0644);
} }
return $image; return $image;
@ -155,18 +136,34 @@ class Image extends Model
} }
/** /**
* Finds the names of all the original-sized images uploaded to the server * Converts the image into the specified cover type to the specified path.
* @return string[] *
* @param File $image The image file to be processed
* @param string $path The path to save the processed image file
* @param int $coverType The type to process the image to
*/ */
public static function getOriginalFiles() { public static function processFile(File $image, string $path, $coverType) {
$rv = array(); if ($coverType['id'] === self::ORIGINAL && $image->getMimeType() === 'image/jpeg') {
$command = 'cp "'.$image->getPathname().'" '.$path;
} else {
// ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php
$command = 'convert 2>&1 "'.$image->getPathname().'" -background white -alpha remove -alpha off -strip';
//We use a cursor to cut down on memory if ($image->getMimeType() === 'image/jpeg') {
foreach (Image::cursor() as $image) { $command .= ' -quality 100 -format jpeg';
$rv[] = $image->getFile(Image::ORIGINAL); } else {
$command .= ' -quality 95 -format png';
} }
return $rv; if (isset($coverType['geometry'])) {
$command .= " -gravity center -thumbnail ${coverType['geometry']} -extent ${coverType['geometry']}";
}
$command .= ' "'.$path.'"';
}
External::execute($command);
chmod($path, 0644);
} }
protected $table = 'images'; protected $table = 'images';