Update Image Util

This commit is contained in:
Daniel Supernault 2020-01-28 21:31:05 -07:00
parent 22e9e3df08
commit 7355e94723
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -8,157 +8,191 @@ use Cache, Storage;
class Image class Image
{ {
public $square; public $square;
public $landscape; public $landscape;
public $portrait; public $portrait;
public $thumbnail; public $thumbnail;
public $orientation; public $orientation;
public $acceptedMimes = [ public $acceptedMimes = [
'image/png', 'image/png',
'image/jpeg' 'image/jpeg'
]; ];
public function __construct() public function __construct()
{ {
ini_set('memory_limit', config('pixelfed.memory_limit', '1024M')); ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
$this->square = $this->orientations()['square']; $this->square = $this->orientations()['square'];
$this->landscape = $this->orientations()['landscape']; $this->landscape = $this->orientations()['landscape'];
$this->portrait = $this->orientations()['portrait']; $this->portrait = $this->orientations()['portrait'];
$this->thumbnail = [ $this->thumbnail = [
'width' => 640, 'width' => 640,
'height' => 640, 'height' => 640,
]; ];
$this->orientation = null; $this->orientation = null;
} }
public function orientations() public function orientations()
{ {
return [ return [
'square' => [ 'square' => [
'width' => 1080, 'width' => 1080,
'height' => 1080, 'height' => 1080,
], ],
'landscape' => [ 'landscape' => [
'width' => 1920, 'width' => 1920,
'height' => 1080, 'height' => 1080,
], ],
'portrait' => [ 'portrait' => [
'width' => 1080, 'width' => 1080,
'height' => 1350, 'height' => 1350,
], ],
]; ];
} }
public function getAspectRatio($mediaPath, $thumbnail = false) public function getAspectRatio($mediaPath, $thumbnail = false)
{ {
if (!is_file($mediaPath)) { if (!is_file($mediaPath)) {
throw new \Exception('Invalid Media Path'); throw new \Exception('Invalid Media Path');
} }
if ($thumbnail) { if ($thumbnail) {
return [ return [
'dimensions' => $this->thumbnail, 'dimensions' => $this->thumbnail,
'orientation' => 'thumbnail', 'orientation' => 'thumbnail',
]; ];
} }
list($width, $height) = getimagesize($mediaPath); list($width, $height) = getimagesize($mediaPath);
$aspect = $width / $height; $aspect = $width / $height;
$orientation = $aspect === 1 ? 'square' : $orientation = $aspect === 1 ? 'square' :
($aspect > 1 ? 'landscape' : 'portrait'); ($aspect > 1 ? 'landscape' : 'portrait');
$this->orientation = $orientation; $this->orientation = $orientation;
return [ return [
'dimensions' => $this->orientations()[$orientation], 'dimensions' => $this->orientations()[$orientation],
'orientation' => $orientation, 'orientation' => $orientation,
]; ];
} }
public function resizeImage(Media $media) public function resizeImage(Media $media)
{ {
$basePath = storage_path('app/'.$media->media_path); $basePath = storage_path('app/'.$media->media_path);
$this->handleResizeImage($media); $this->handleResizeImage($media);
} }
public function resizeThumbnail(Media $media) public function resizeThumbnail(Media $media)
{ {
$basePath = storage_path('app/'.$media->media_path); $basePath = storage_path('app/'.$media->media_path);
$this->handleThumbnailImage($media); $this->handleThumbnailImage($media);
} }
public function handleResizeImage(Media $media) public function handleResizeImage(Media $media)
{ {
$this->handleImageTransform($media, false); $this->handleImageTransform($media, false);
} }
public function handleThumbnailImage(Media $media) public function handleThumbnailImage(Media $media)
{ {
$this->handleImageTransform($media, true); $this->handleImageTransform($media, true);
} }
public function handleImageTransform(Media $media, $thumbnail = false) public function handleImageTransform(Media $media, $thumbnail = false)
{ {
$path = $media->media_path; $path = $media->media_path;
$file = storage_path('app/'.$path); $file = storage_path('app/'.$path);
if (!in_array($media->mime, $this->acceptedMimes)) { if (!in_array($media->mime, $this->acceptedMimes)) {
return; return;
} }
$ratio = $this->getAspectRatio($file, $thumbnail); $ratio = $this->getAspectRatio($file, $thumbnail);
$aspect = $ratio['dimensions']; $aspect = $ratio['dimensions'];
$orientation = $ratio['orientation']; $orientation = $ratio['orientation'];
try { try {
$img = Intervention::make($file); $img = Intervention::make($file);
$metadata = $img->exif(); $metadata = $img->exif();
$img->orientate(); $img->orientate();
if($thumbnail) { if($thumbnail) {
$img->resize($aspect['width'], $aspect['height'], function ($constraint) { $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} else { } else {
if(config('media.exif.database', false) == true) { if(config('media.exif.database', false) == true) {
$media->metadata = json_encode($metadata); $meta = [];
} $keys = [
"COMPUTED",
"FileName",
"FileSize",
"FileType",
"Make",
"Model",
"MimeType",
"ColorSpace",
"ExifVersion",
"Orientation",
"UserComment",
"XResolution",
"YResolution",
"FileDateTime",
"SectionsFound",
"ExifImageWidth",
"ResolutionUnit",
"ExifImageLength",
"FlashPixVersion",
"Exif_IFD_Pointer",
"YCbCrPositioning",
"ComponentsConfiguration",
"ExposureTime",
"FNumber",
"ISOSpeedRatings",
"ShutterSpeedValue"
];
foreach ($metadata as $k => $v) {
if(in_array($k, $keys)) {
$meta[$k] = $v;
}
}
$media->metadata = json_encode($meta);
}
$img->resize($aspect['width'], $aspect['height'], function ($constraint) { $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} }
$converted = $this->setBaseName($path, $thumbnail, $img->extension); $converted = $this->setBaseName($path, $thumbnail, $img->extension);
$newPath = storage_path('app/'.$converted['path']); $newPath = storage_path('app/'.$converted['path']);
$quality = config('pixelfed.image_quality'); $quality = config('pixelfed.image_quality');
$img->save($newPath, $quality); $img->save($newPath, $quality);
$img->destroy(); $img->destroy();
if (!$thumbnail) { if (!$thumbnail) {
$media->orientation = $orientation; $media->orientation = $orientation;
} }
if ($thumbnail == true) { if ($thumbnail == true) {
$media->thumbnail_path = $converted['path']; $media->thumbnail_path = $converted['path'];
$media->thumbnail_url = url(Storage::url($converted['path'])); $media->thumbnail_url = url(Storage::url($converted['path']));
} else { } else {
$media->media_path = $converted['path']; $media->media_path = $converted['path'];
$media->mime = $img->mime; $media->mime = $img->mime;
} }
$media->save(); $media->save();
Cache::forget('status:transformer:media:attachments:'.$media->status_id); Cache::forget('status:transformer:media:attachments:'.$media->status_id);
Cache::forget('status:thumb:'.$media->status_id); Cache::forget('status:thumb:'.$media->status_id);
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
public function setBaseName($basePath, $thumbnail, $extension) public function setBaseName($basePath, $thumbnail, $extension)
{ {
$png = false; $png = false;
$path = explode('.', $basePath); $path = explode('.', $basePath);
$name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0]; $name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0];
$ext = last($path); $ext = last($path);
$basePath = "{$name}.{$ext}"; $basePath = "{$name}.{$ext}";
return ['path' => $basePath, 'png' => $png]; return ['path' => $basePath, 'png' => $png];
} }
} }