2015-08-03 11:35:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Picture {
|
|
|
|
private $_path = CACHE_PATH;
|
|
|
|
private $_uri = CACHE_URI;
|
|
|
|
private $_key;
|
|
|
|
private $_bin = false;
|
2016-02-25 11:28:01 +01:00
|
|
|
private $_formats = ['jpeg' => '.jpg', 'png' => '.png'];
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Load a bin picture from a path
|
|
|
|
*/
|
|
|
|
public function fromPath($path) {
|
|
|
|
$handle = fopen($path, "r");
|
|
|
|
$this->_bin = fread($handle, filesize($path));
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Load a bin picture from a base64
|
|
|
|
*/
|
|
|
|
public function fromBase($base = false) {
|
|
|
|
if($base) {
|
|
|
|
$this->_bin = (string)base64_decode((string)$base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Convert to a base64
|
|
|
|
*/
|
|
|
|
public function toBase() {
|
|
|
|
if($this->_bin)
|
|
|
|
return base64_encode($this->_bin);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Get a picture of the current size
|
|
|
|
* @param $key The key of the picture
|
|
|
|
* @param $width The width requested
|
|
|
|
* @param $height The height requested
|
|
|
|
* @return The url of the picture
|
|
|
|
*/
|
2016-02-25 11:28:01 +01:00
|
|
|
public function get($key, $width = false, $height = false, $format = 'jpeg') {
|
|
|
|
if(!in_array($format, array_keys($this->_formats))) $format = 'jpeg';
|
2015-08-03 11:35:01 +02:00
|
|
|
$this->_key = $key;
|
|
|
|
|
2016-02-25 11:28:01 +01:00
|
|
|
$original = $this->_path.md5($this->_key).$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
// We request the original picture
|
|
|
|
if($width == false) {
|
|
|
|
if(file_exists($original)) {
|
|
|
|
$this->fromPath($original);
|
2016-02-25 11:28:01 +01:00
|
|
|
return $this->_uri.md5($this->_key).$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We request a specific size
|
|
|
|
} else {
|
2016-02-25 11:28:01 +01:00
|
|
|
if(file_exists($this->_path.md5($this->_key).'_'.$width.$this->_formats[$format])) {
|
|
|
|
$this->fromPath($this->_path.md5($this->_key).'_'.$width.$this->_formats[$format]);
|
|
|
|
return $this->_uri.md5($this->_key).'_'.$width.$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
} else {
|
|
|
|
if(file_exists($original)) {
|
|
|
|
$this->fromPath($original);
|
|
|
|
$this->createThumbnail($width, $height);
|
|
|
|
|
2016-02-25 11:28:01 +01:00
|
|
|
return $this->_uri.md5($this->_key).'_'.$width.$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Save a picture (original size)
|
|
|
|
* @param $key The key of the picture
|
|
|
|
*/
|
2016-02-25 11:28:01 +01:00
|
|
|
public function set($key, $format = 'jpeg') {
|
|
|
|
if(!in_array($format, array_keys($this->_formats))) $format = 'jpeg';
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
$this->_key = $key;
|
2016-02-25 11:28:01 +01:00
|
|
|
$path = $this->_path.md5($this->_key).$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
// If the file exist we replace it
|
|
|
|
if(file_exists($path) && $this->_bin) {
|
|
|
|
unlink($path);
|
|
|
|
|
|
|
|
// And destroy all the thumbnails
|
|
|
|
foreach(
|
|
|
|
glob(
|
|
|
|
$this->_path.
|
|
|
|
md5($key).
|
2016-02-25 11:28:01 +01:00
|
|
|
'*'.$this->_formats[$format],
|
2015-08-03 11:35:01 +02:00
|
|
|
GLOB_NOSORT
|
|
|
|
) as $path_thumb) {
|
|
|
|
unlink($path_thumb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->_bin) {
|
|
|
|
$im = new Imagick();
|
|
|
|
try {
|
|
|
|
$im->readImageBlob($this->_bin);
|
|
|
|
if($im != false) {
|
2016-02-25 11:28:01 +01:00
|
|
|
$im->setImageFormat($format);
|
2015-08-03 11:35:01 +02:00
|
|
|
$im->setImageCompressionQuality(95);
|
|
|
|
$im->setInterlaceScheme(Imagick::INTERLACE_PLANE);
|
|
|
|
$im->writeImage($path);
|
2016-02-12 10:02:32 +01:00
|
|
|
$im->clear();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
} catch (ImagickException $e) {
|
|
|
|
error_log($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Create a thumbnail of the picture and save it
|
|
|
|
* @param $size The size requested
|
|
|
|
*/
|
2016-02-25 11:28:01 +01:00
|
|
|
private function createThumbnail($width, $height = false, $format = 'jpeg') {
|
|
|
|
if(!in_array($format, array_keys($this->_formats))) $format = 'jpeg';
|
2015-08-03 11:35:01 +02:00
|
|
|
if(!$height) $height = $width;
|
|
|
|
|
2016-02-25 11:28:01 +01:00
|
|
|
$path = $this->_path.md5($this->_key).'_'.$width.$this->_formats[$format];
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
$im = new Imagick;
|
|
|
|
$im->readImageBlob($this->_bin);
|
2016-02-25 11:28:01 +01:00
|
|
|
$im->setImageFormat($format);
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
$geo = $im->getImageGeometry();
|
|
|
|
|
|
|
|
$im->cropThumbnailImage($width, $height);
|
|
|
|
if($width > $geo['width']) {
|
|
|
|
$factor = floor($width/$geo['width']);
|
|
|
|
$im->blurImage($factor, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
$im->setImageCompressionQuality(85);
|
|
|
|
$im->setInterlaceScheme(Imagick::INTERLACE_PLANE);
|
|
|
|
|
|
|
|
$im->writeImage($path);
|
2016-02-12 10:02:32 +01:00
|
|
|
$im->clear();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|