2014-03-20 01:55:48 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - TemporaryPhoto
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2014 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Utils;
|
|
|
|
|
2014-04-07 22:29:15 +02:00
|
|
|
use OCP\ICache,
|
|
|
|
OCP\Image;
|
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
/**
|
2014-03-20 21:53:13 +01:00
|
|
|
* This class is used for getting a temporary contact photo for cropping.
|
2014-03-24 21:24:45 +01:00
|
|
|
*
|
|
|
|
* Sub-classes must override __construct() and processImage()
|
2014-03-20 01:55:48 +01:00
|
|
|
*/
|
2014-03-20 21:53:13 +01:00
|
|
|
class TemporaryPhoto {
|
2014-03-20 01:55:48 +01:00
|
|
|
|
|
|
|
const MAX_SIZE = 400;
|
|
|
|
|
|
|
|
const PHOTO_CURRENT = 0;
|
|
|
|
const PHOTO_FILESYSTEM = 1;
|
|
|
|
const PHOTO_UPLOADED = 2;
|
2014-06-15 10:28:30 +02:00
|
|
|
const PHOTO_USER = 3;
|
2014-03-20 01:55:48 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-08 01:19:30 +02:00
|
|
|
* @var \OCP\ICache
|
2014-03-20 01:55:48 +01:00
|
|
|
*/
|
2014-04-08 01:32:53 +02:00
|
|
|
protected $cache;
|
2014-03-20 01:55:48 +01:00
|
|
|
|
|
|
|
/**
|
2014-03-23 19:02:16 +01:00
|
|
|
* @var \OCP\Image
|
2014-03-20 16:00:22 +01:00
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
protected $image;
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Cache key for temporary storage.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
protected $key;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Whether normalizePhoto() has already been called.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
protected $normalized;
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Whether the photo is cached.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
protected $cached;
|
|
|
|
|
2014-03-20 16:00:22 +01:00
|
|
|
/**
|
|
|
|
* Photo loader classes.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
static public $classMap = array(
|
|
|
|
'OCA\\Contacts\\Utils\\TemporaryPhoto\\Contact',
|
|
|
|
'OCA\\Contacts\\Utils\\TemporaryPhoto\\FileSystem',
|
|
|
|
'OCA\\Contacts\\Utils\\TemporaryPhoto\\Uploaded',
|
2014-06-15 10:28:30 +02:00
|
|
|
'OCA\\Contacts\\Utils\\TemporaryPhoto\\User',
|
2014-03-20 01:55:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2014-04-08 01:19:30 +02:00
|
|
|
* Always call parents ctor in subclasses:
|
|
|
|
* parent::__construct($cache);
|
2014-03-20 16:00:22 +01:00
|
|
|
*/
|
2014-04-07 22:29:15 +02:00
|
|
|
public function __construct(ICache $cache, $key = null) {
|
|
|
|
$this->cache = $cache;
|
2014-03-20 21:53:13 +01:00
|
|
|
$this->key = $key;
|
|
|
|
if (!is_null($key)) {
|
|
|
|
$this->processImage();
|
|
|
|
}
|
2014-03-20 01:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Returns an instance of a subclass of this class
|
|
|
|
*
|
|
|
|
* @param \OCP\IServerContainer $server
|
2014-03-20 21:53:13 +01:00
|
|
|
* @param int|null $type One of the pre-defined types.
|
|
|
|
* @param mixed|null $data Whatever data is needed to load the photo.
|
2014-03-20 16:00:22 +01:00
|
|
|
*/
|
2014-04-07 22:29:15 +02:00
|
|
|
public static function create(ICache $cache, $type = null, $data = null) {
|
2014-03-20 01:55:48 +01:00
|
|
|
if (isset(self::$classMap[$type])) {
|
2014-04-07 22:29:15 +02:00
|
|
|
return new self::$classMap[$type]($cache, $data);
|
2014-03-20 01:55:48 +01:00
|
|
|
} else {
|
2014-04-08 01:19:30 +02:00
|
|
|
return new self($cache, $data);
|
2014-03-20 01:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 21:53:13 +01:00
|
|
|
/**
|
|
|
|
* Remove a cached image by key.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*/
|
|
|
|
public function remove($key) {
|
2014-04-07 22:29:15 +02:00
|
|
|
return $this->cache->remove($key);
|
2014-03-20 21:53:13 +01:00
|
|
|
}
|
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Do what's needed to get the image from storage
|
|
|
|
* depending on the type.
|
|
|
|
* After this method is called $this->image must hold an
|
|
|
|
* instance of \OCP\Image.
|
|
|
|
*/
|
2014-03-20 21:53:13 +01:00
|
|
|
protected function processImage() {
|
2014-04-07 22:29:15 +02:00
|
|
|
$this->image = new Image();
|
|
|
|
$this->image->loadFromData($this->cache->get($this->key));
|
2014-03-20 21:53:13 +01:00
|
|
|
}
|
2014-03-20 01:55:48 +01:00
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Whether this image is valied
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
public function isValid() {
|
2014-04-07 22:29:15 +02:00
|
|
|
return (($this->image instanceof Image) && $this->image->valid());
|
2014-03-20 01:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Get the key to the cache image.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
public function getKey() {
|
|
|
|
$this->cachePhoto();
|
|
|
|
return $this->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Get normalized image.
|
|
|
|
*
|
|
|
|
* @return \OCP\Image
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
public function getPhoto() {
|
|
|
|
$this->normalizePhoto();
|
|
|
|
return $this->image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Save image data to cache and return the key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
private function cachePhoto() {
|
|
|
|
if ($this->cached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:29:15 +02:00
|
|
|
if (!$this->image instanceof Image) {
|
2014-03-20 01:55:48 +01:00
|
|
|
$this->processImage();
|
|
|
|
}
|
|
|
|
$this->normalizePhoto();
|
|
|
|
|
|
|
|
$data = $this->image->data();
|
|
|
|
$this->key = uniqid('photo-');
|
2014-04-07 22:29:15 +02:00
|
|
|
$this->cache->set($this->key, $data, 600);
|
2014-03-20 01:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-20 16:00:22 +01:00
|
|
|
* Resize and rotate the image if needed.
|
|
|
|
*/
|
2014-03-20 01:55:48 +01:00
|
|
|
private function normalizePhoto() {
|
|
|
|
|
|
|
|
if($this->normalized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->image->fixOrientation();
|
|
|
|
|
|
|
|
if ($this->image->height() > self::MAX_SIZE
|
|
|
|
|| $this->image->width() > self::MAX_SIZE
|
|
|
|
) {
|
|
|
|
$this->image->resize(self::MAX_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->normalized = true;
|
|
|
|
}
|
|
|
|
|
2014-06-15 10:28:30 +02:00
|
|
|
public function getMimeType(){
|
|
|
|
return $this->image->mimeType();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImage(){
|
|
|
|
return $this->image;
|
|
|
|
}
|
2014-03-20 01:55:48 +01:00
|
|
|
}
|