1
0
Fork 0
pixelfed/app/Util/Media/Image.php

165 lines
4.6 KiB
PHP
Raw Normal View History

2018-05-23 00:06:21 +00:00
<?php
namespace App\Util\Media;
use App\Media;
use Image as Intervention;
2019-02-25 19:33:47 +00:00
use Cache, Storage;
2018-05-23 00:06:21 +00:00
2018-08-28 03:07:36 +00:00
class Image
{
public $square;
public $landscape;
public $portrait;
public $thumbnail;
public $orientation;
2018-10-10 01:20:09 +00:00
public $acceptedMimes = [
'image/png',
2018-11-19 04:56:22 +00:00
'image/jpeg'
2018-10-10 01:20:09 +00:00
];
2018-08-28 03:07:36 +00:00
public function __construct()
{
ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
$this->square = $this->orientations()['square'];
$this->landscape = $this->orientations()['landscape'];
$this->portrait = $this->orientations()['portrait'];
$this->thumbnail = [
2018-10-10 01:20:09 +00:00
'width' => 640,
'height' => 640,
];
2018-08-28 03:07:36 +00:00
$this->orientation = null;
}
2018-05-23 00:06:21 +00:00
2018-08-28 03:07:36 +00:00
public function orientations()
{
return [
2018-10-10 01:20:09 +00:00
'square' => [
'width' => 1080,
'height' => 1080,
],
'landscape' => [
'width' => 1920,
'height' => 1080,
],
'portrait' => [
'width' => 1080,
'height' => 1350,
],
2018-05-23 00:06:21 +00:00
];
}
2018-08-28 03:07:36 +00:00
public function getAspectRatio($mediaPath, $thumbnail = false)
{
if (!is_file($mediaPath)) {
throw new \Exception('Invalid Media Path');
}
if ($thumbnail) {
return [
2019-06-18 04:15:36 +00:00
'dimensions' => $this->thumbnail,
'orientation' => 'thumbnail',
];
2018-08-28 03:07:36 +00:00
}
2018-05-23 00:06:21 +00:00
2018-08-28 03:07:36 +00:00
list($width, $height) = getimagesize($mediaPath);
$aspect = $width / $height;
$orientation = $aspect === 1 ? 'square' :
2019-06-18 04:15:36 +00:00
($aspect > 1 ? 'landscape' : 'portrait');
2018-08-28 03:07:36 +00:00
$this->orientation = $orientation;
return [
2019-06-18 04:15:36 +00:00
'dimensions' => $this->orientations()[$orientation],
'orientation' => $orientation,
];
2018-08-28 03:07:36 +00:00
}
public function resizeImage(Media $media)
{
2018-08-28 03:07:36 +00:00
$basePath = storage_path('app/'.$media->media_path);
$this->handleResizeImage($media);
}
2018-05-23 00:06:21 +00:00
2018-08-28 03:07:36 +00:00
public function resizeThumbnail(Media $media)
{
$basePath = storage_path('app/'.$media->media_path);
$this->handleThumbnailImage($media);
2018-05-23 00:06:21 +00:00
}
2018-08-28 03:07:36 +00:00
public function handleResizeImage(Media $media)
{
$this->handleImageTransform($media, false);
}
2018-08-28 03:07:36 +00:00
public function handleThumbnailImage(Media $media)
{
$this->handleImageTransform($media, true);
}
2018-05-23 00:06:21 +00:00
2018-08-28 03:07:36 +00:00
public function handleImageTransform(Media $media, $thumbnail = false)
{
$path = $media->media_path;
$file = storage_path('app/'.$path);
2018-10-10 01:20:09 +00:00
if (!in_array($media->mime, $this->acceptedMimes)) {
return;
}
2018-08-28 03:07:36 +00:00
$ratio = $this->getAspectRatio($file, $thumbnail);
$aspect = $ratio['dimensions'];
$orientation = $ratio['orientation'];
try {
2019-08-02 04:38:26 +00:00
$img = Intervention::make($file);
$metadata = $img->exif();
$img->orientate();
2018-10-10 01:20:09 +00:00
if($thumbnail) {
2018-11-22 20:25:31 +00:00
$img->resize($aspect['width'], $aspect['height'], function ($constraint) {
$constraint->aspectRatio();
});
2018-10-10 01:20:09 +00:00
} else {
2019-06-18 04:15:36 +00:00
if(config('media.exif.database', false) == true) {
$media->metadata = json_encode($metadata);
}
2018-10-10 01:20:09 +00:00
$img->resize($aspect['width'], $aspect['height'], function ($constraint) {
$constraint->aspectRatio();
});
}
2018-08-28 03:07:36 +00:00
$converted = $this->setBaseName($path, $thumbnail, $img->extension);
$newPath = storage_path('app/'.$converted['path']);
$quality = config('pixelfed.image_quality');
$img->save($newPath, $quality);
if (!$thumbnail) {
$media->orientation = $orientation;
}
if ($thumbnail == true) {
$media->thumbnail_path = $converted['path'];
$media->thumbnail_url = url(Storage::url($converted['path']));
} else {
$media->media_path = $converted['path'];
$media->mime = $img->mime;
}
$media->save();
2019-06-18 04:15:36 +00:00
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
2019-02-25 19:33:47 +00:00
Cache::forget('status:thumb:'.$media->status_id);
2018-08-28 03:07:36 +00:00
} catch (Exception $e) {
}
}
public function setBaseName($basePath, $thumbnail, $extension)
{
$png = false;
$path = explode('.', $basePath);
$name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0];
$ext = last($path);
$basePath = "{$name}.{$ext}";
return ['path' => $basePath, 'png' => $png];
}
}