Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Model;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Events\ImageEvent;
use Maslosoft\Mangan\Exceptions\FileNotFoundException;
use Maslosoft\Mangan\Helpers\ImageThumb;
use MongoGridFSFile;
/**
* Class for storing embedded images
* @since 2.0.1
* @author Piotr
*/
class Image extends File
{
const EventBeforeResize = 'beforeResize';
const EventAfterResize = 'afterResize';
/**
* Image width
* @var int
*/
public $width = 0;
/**
* Image height
* @var int
*/
public $height = 0;
/**
* Get resized image
* @param ImageParams $params
* @return MongoGridFSFile
*/
public function get(ImageParams $params = null)
{
// Get original image or file if it is not image
if (!$params || !$this->isImage($this->filename))
{
return $this->_get();
}
// Get resized image
$params->isTemp = true;
$result = $this->_get($params->toArray());
// Resize and store if not found
if (!$result)
{
$result = $this->_get();
if (!$result)
{
throw new FileNotFoundException('File not found');
}
$originalFilename = $result->file['filename'];
$fileName = tempnam('/tmp/', str_replace('\\', '.', __CLASS__));
$result->write($fileName);
$ie = new ImageEvent;
$ie->sender = $this;
$ie->path = $fileName;
Event::trigger($this, self::EventBeforeResize, $ie);
$image = new ImageThumb($fileName);
if ($params->adaptive)
{
$image->adaptiveResize($params->width, $params->height)->save($fileName);
}
else
{
$image->resize($params->width, $params->height)->save($fileName);
}
$ie = new ImageEvent;
$ie->sender = $this;
$ie->path = $fileName;
Event::trigger($this, self::EventAfterResize, $ie);
$this->_set($fileName, $originalFilename, $params->toArray());
unlink($fileName);
return $this->_get($params->toArray());
}
return $result;
}
/**
* Return true if it is really image
* @return bool
*/
public function isImage($name)
{
return in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'gif', 'png']);
}
protected function _set($tempName, $fileName, $params = [])
{
if ($this->isImage($fileName))
{
$thumb = new ImageThumb($tempName);
$dimensions = (object) $thumb->getCurrentDimensions();
$this->width = $dimensions->width;
$this->height = $dimensions->height;
}
parent::_set($tempName, $fileName, $params);
}
/**
* Send file to browser
*/
public function send(ImageParams $params = null)
{
$this->_send($this->get($params));
}
/**
* Stream file to browser
*/
public function stream(ImageParams $params = null)
{
$this->_stream($this->get($params));
}
}
API documentation generated by ApiGen