2015-08-30 14:29:12 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2021-02-14 03:39:15 +01:00
|
|
|
* Copyright (C) 2015 Feld0.
|
2015-10-25 06:17:45 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
namespace App\Models;
|
2015-08-31 14:35:47 +02:00
|
|
|
|
2021-02-14 20:45:51 +01:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2015-08-31 14:35:47 +02:00
|
|
|
use External;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-12-03 05:39:31 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-02-05 23:01:45 +01:00
|
|
|
use Illuminate\Support\Str;
|
2017-11-28 22:47:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
2017-11-25 05:45:41 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2015-09-10 12:23:12 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-01-01 01:36:08 +01:00
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* App\Models\Image.
|
2016-01-01 01:36:08 +01:00
|
|
|
*
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $id
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property string $filename
|
|
|
|
* @property string $mime
|
|
|
|
* @property string $extension
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $size
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property string $hash
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $uploaded_by
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
2021-02-14 03:34:58 +01:00
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereFilename($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereMime($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereExtension($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereSize($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereHash($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereUploadedBy($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Image whereUpdatedAt($value)
|
2016-12-10 17:47:49 +01:00
|
|
|
* @mixin \Eloquent
|
2016-01-01 01:36:08 +01:00
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
class Image extends Model
|
|
|
|
{
|
|
|
|
const NORMAL = 1;
|
|
|
|
const ORIGINAL = 2;
|
|
|
|
const THUMBNAIL = 3;
|
|
|
|
const SMALL = 4;
|
|
|
|
|
|
|
|
public static $ImageTypes = [
|
2017-11-25 06:02:35 +01:00
|
|
|
self::NORMAL => ['id' => self::NORMAL, 'name' => 'normal', 'width' => 350, 'height' => 350, 'geometry' => '350'],
|
|
|
|
self::ORIGINAL => ['id' => self::ORIGINAL, 'name' => 'original', 'width' => null, 'height' => null, 'geometry' => null],
|
|
|
|
self::SMALL => ['id' => self::SMALL, 'name' => 'small', 'width' => 100, 'height' => 100, 'geometry' => '100x100^'],
|
2021-02-14 03:39:15 +01:00
|
|
|
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50, 'geometry' => '50x50^'],
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
2017-11-28 22:47:37 +01:00
|
|
|
const MIME_JPEG = 'image/jpeg';
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public static function getImageTypeFromName($name)
|
|
|
|
{
|
|
|
|
foreach (self::$ImageTypes as $cover) {
|
|
|
|
if ($cover['name'] != $name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:01:45 +01:00
|
|
|
/**
|
|
|
|
* @param UploadedFile $file
|
2016-03-06 13:27:53 +01:00
|
|
|
* @param int|User $user
|
2016-02-05 23:01:45 +01:00
|
|
|
* @param bool $forceReupload forces the image to be re-processed even if a matching hash is found
|
|
|
|
* @return Image
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function upload(UploadedFile $file, $user, bool $forceReupload = false)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$userId = $user;
|
|
|
|
if ($user instanceof User) {
|
|
|
|
$userId = $user->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash = md5_file($file->getPathname());
|
2021-02-14 03:39:15 +01:00
|
|
|
$image = self::whereHash($hash)->whereUploadedBy($userId)->first();
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-03-16 17:32:39 +01:00
|
|
|
if ($image) {
|
|
|
|
if ($forceReupload) {
|
2017-11-25 06:02:35 +01:00
|
|
|
$image->clearExisting(true);
|
2016-03-16 17:32:39 +01:00
|
|
|
} else {
|
|
|
|
return $image;
|
2016-02-05 23:01:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-14 03:39:15 +01:00
|
|
|
$image = new self();
|
2016-02-05 23:01:45 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
try {
|
|
|
|
$image->uploaded_by = $userId;
|
|
|
|
$image->size = $file->getSize();
|
|
|
|
$image->filename = $file->getClientOriginalName();
|
|
|
|
$image->extension = $file->getClientOriginalExtension();
|
|
|
|
$image->mime = $file->getMimeType();
|
|
|
|
$image->hash = $hash;
|
|
|
|
$image->save();
|
|
|
|
|
|
|
|
$image->ensureDirectoryExists();
|
|
|
|
foreach (self::$ImageTypes as $coverType) {
|
2017-11-25 05:45:41 +01:00
|
|
|
self::processFile($file, $image->getFile($coverType['id']), $coverType);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$image->delete();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 05:13:45 +01:00
|
|
|
/**
|
2017-11-25 05:45:41 +01:00
|
|
|
* Converts the image into the specified cover type to the specified path.
|
|
|
|
*
|
|
|
|
* @param File $image The image file to be processed
|
|
|
|
* @param string $path The path to save the processed image file
|
2017-12-03 04:06:25 +01:00
|
|
|
* @param array $coverType The type to process the image to
|
2017-11-25 05:13:45 +01:00
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
private static function processFile(File $image, string $path, $coverType)
|
|
|
|
{
|
|
|
|
if ($coverType['id'] === self::ORIGINAL && $image->getMimeType() === self::MIME_JPEG) {
|
|
|
|
if ($image->getPathname() === $path) {
|
2017-12-03 05:39:31 +01:00
|
|
|
Log::warning("Attempted to copy an original file $path to itself.");
|
|
|
|
} else {
|
2021-02-14 03:39:15 +01:00
|
|
|
$command = 'cp "'.$image->getPathname().'" '.$path;
|
2017-12-03 05:39:31 +01:00
|
|
|
}
|
2017-11-25 05:45:41 +01:00
|
|
|
} 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';
|
|
|
|
|
2017-11-28 22:47:37 +01:00
|
|
|
if ($image->getMimeType() === self::MIME_JPEG) {
|
2017-11-25 05:45:41 +01:00
|
|
|
$command .= ' -quality 100 -format jpeg';
|
|
|
|
} else {
|
|
|
|
$command .= ' -quality 95 -format png';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($coverType['geometry'])) {
|
|
|
|
$command .= " -gravity center -thumbnail ${coverType['geometry']} -extent ${coverType['geometry']}";
|
|
|
|
}
|
2017-11-25 05:13:45 +01:00
|
|
|
|
2017-11-25 05:45:41 +01:00
|
|
|
$command .= ' "'.$path.'"';
|
2017-11-25 05:13:45 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 05:45:41 +01:00
|
|
|
External::execute($command);
|
|
|
|
chmod($path, 0644);
|
2017-11-25 05:13:45 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
protected $table = 'images';
|
|
|
|
|
|
|
|
public function getUrl($type = self::NORMAL)
|
|
|
|
{
|
|
|
|
$type = self::$ImageTypes[$type];
|
|
|
|
|
2015-12-27 10:43:43 +01:00
|
|
|
return action('ImagesController@getImage', ['id' => $this->id, 'type' => $type['name'], 'extension' => $this->extension]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFile($type = self::NORMAL)
|
|
|
|
{
|
2016-06-06 08:29:37 +02:00
|
|
|
return $this->getDirectory().'/'.$this->getFilename($type);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilename($type = self::NORMAL)
|
|
|
|
{
|
|
|
|
$typeInfo = self::$ImageTypes[$type];
|
|
|
|
|
2016-06-06 08:29:37 +02:00
|
|
|
return $this->id.'_'.$typeInfo['name'].'.'.$this->extension;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDirectory()
|
|
|
|
{
|
2016-06-06 08:29:37 +02:00
|
|
|
$dir = (string) (floor($this->id / 100) * 100);
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2021-02-14 20:38:28 +01:00
|
|
|
return config('ponyfm.files_directory').'/images/'.$dir;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function ensureDirectoryExists()
|
|
|
|
{
|
|
|
|
$destination = $this->getDirectory();
|
|
|
|
umask(0);
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! is_dir($destination)) {
|
2015-09-10 13:50:04 +02:00
|
|
|
mkdir($destination, 0777, true);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-25 06:02:35 +01:00
|
|
|
|
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* Deletes any generated files if they exist.
|
2017-11-28 20:19:36 +01:00
|
|
|
* @param bool $includeOriginal Set to true if the original image should be deleted as well.
|
2017-11-25 06:02:35 +01:00
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public function clearExisting(bool $includeOriginal = false)
|
|
|
|
{
|
2017-11-25 06:02:35 +01:00
|
|
|
$files = scandir($this->getDirectory());
|
|
|
|
$filePrefix = $this->id.'_';
|
2021-02-14 03:39:15 +01:00
|
|
|
$originalName = $filePrefix.self::$ImageTypes[self::ORIGINAL]['name'];
|
2017-11-25 06:02:35 +01:00
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
$files = array_filter($files, function ($file) use ($originalName, $includeOriginal, $filePrefix) {
|
|
|
|
if (Str::startsWith($file, $originalName) && ! $includeOriginal) {
|
2017-11-28 20:19:36 +01:00
|
|
|
return false;
|
2021-02-14 03:39:15 +01:00
|
|
|
} else {
|
|
|
|
return Str::startsWith($file, $filePrefix);
|
2017-11-28 20:19:36 +01:00
|
|
|
}
|
2017-11-25 06:02:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
unlink($this->getDirectory().'/'.$file);
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 22:47:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the cover images for the image, overwriting if needed.
|
|
|
|
*
|
|
|
|
* @throws FileNotFoundException If the original file cannot be found.
|
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public function buildCovers()
|
|
|
|
{
|
2017-11-28 22:47:37 +01:00
|
|
|
$originalFile = new File($this->getFile(self::ORIGINAL));
|
|
|
|
|
|
|
|
foreach (self::$ImageTypes as $imageType) {
|
2017-12-03 05:39:31 +01:00
|
|
|
//Ignore original imagetype
|
2021-02-14 03:39:15 +01:00
|
|
|
if ($imageType['id'] === self::ORIGINAL) {
|
2017-12-03 05:39:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:47:37 +01:00
|
|
|
self::processFile($originalFile, $this->getFile($imageType['id']), $imageType);
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 23:56:09 +02:00
|
|
|
}
|