Pony.fm/app/models/Entities/Image.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2013-07-27 02:15:07 +02:00
<?php
namespace Entities;
use External;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
use Symfony\Component\HttpFoundation\File\UploadedFile;
2013-07-27 02:15:07 +02:00
class Image extends \Eloquent {
const NORMAL = 1;
const ORIGINAL = 2;
const THUMBNAIL = 3;
const SMALL = 4;
public static $ImageTypes = [
self::NORMAL => ['id' => self::NORMAL, 'name' => 'normal', 'width' => 350, 'height' => 350],
self::ORIGINAL => ['id' => self::ORIGINAL, 'name' => 'original', 'width' => null, 'height' => null],
2013-07-28 06:37:32 +02:00
self::SMALL => ['id' => self::SMALL, 'name' => 'small', 'width' => 100, 'height' => 100],
2013-07-27 02:15:07 +02:00
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50]
];
2013-07-28 06:37:32 +02:00
public static function getImageTypeFromName($name) {
2013-07-27 02:15:07 +02:00
foreach (self::$ImageTypes as $cover) {
if ($cover['name'] != $name)
continue;
return $cover;
}
return null;
}
public static function upload(UploadedFile $file, $user) {
2013-08-01 18:58:47 +02:00
$userId = $user;
if ($user instanceof User)
$userId = $user->id;
2013-07-27 02:15:07 +02:00
$hash = md5_file($file->getPathname());
2013-08-01 18:58:47 +02:00
$image = Image::whereHash($hash)->whereUploadedBy($userId)->first();
2013-07-27 02:15:07 +02:00
if ($image)
return $image;
$image = new Image();
try {
2013-08-01 18:58:47 +02:00
$image->uploaded_by = $userId;
2013-07-27 02:15:07 +02:00
$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) {
$command = 'convert 2>&1 "' . $file->getPathname() . '" -background transparent -flatten +matte -strip -quality 95 -format png ';
if (isset($coverType['width']) && isset($coverType['height']))
$command .= '-thumbnail ' . $coverType['width'] . 'x' . $coverType['height'] . '^ -gravity center -extent ' . $coverType['width'] . 'x' . $coverType['height'] . ' ';
$command .= '"' . $image->getFile($coverType['id']) . '"';
External::execute($command);
}
return $image;
}
catch (\Exception $e) {
$image->delete();
throw $e;
}
}
protected $table = 'images';
2013-07-28 20:50:02 +02:00
public function getUrl($type = self::NORMAL) {
2013-07-27 02:15:07 +02:00
$type = self::$ImageTypes[$type];
return URL::to('i' . $this->id . '/' . $type['name'] . '.png');
}
2013-07-28 20:50:02 +02:00
public function getFile($type = self::NORMAL) {
2013-07-27 02:15:07 +02:00
return $this->getDirectory() . '/' . $this->getFilename($type);
}
2013-07-28 20:50:02 +02:00
public function getFilename($type = self::NORMAL) {
2013-07-27 02:15:07 +02:00
$typeInfo = self::$ImageTypes[$type];
return $this->id . '_' . $typeInfo['name'] . '.png';
}
public function getDirectory() {
$dir = (string) ( floor( $this->id / 100 ) * 100 );
return Config::get('app.files_directory') . '/images/' . $dir;
}
public function ensureDirectoryExists() {
$destination = $this->getDirectory();
2013-09-27 22:49:22 +02:00
umask(0);
2013-07-27 02:15:07 +02:00
if (!is_dir($destination))
2013-09-01 23:54:33 +02:00
mkdir($destination, 777, true);
2013-07-27 02:15:07 +02:00
}
}