...
 
Commits (2)
......@@ -78,11 +78,15 @@ class pages extends Controller implements Interfaces\Api, Interfaces\ApiIgnorePa
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$fs = Di::_()->get('Storage');
$dir = Di::_()->get('Config')->get('staticStorageFolder') ?: 'pages';
/** @var Core\Media\Imagick\Manager $manager */
$manager = Core\Di\Di::_()->get('Media\Imagick\Manager');
$resized = get_resized_image_from_uploaded_file('file', 2000, 10000);
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize(2000, 10000);
$fs->open("$dir/page_banners/{$page->getPath()}.jpg", '');
$fs->write($resized);
$fs->write($manager->getJpeg());
}
$page->setHeader(true)
......
......@@ -320,8 +320,14 @@ class blog implements Interfaces\Api
}
if ($saved && is_uploaded_file($_FILES['file']['tmp_name'])) {
$image = get_resized_image_from_uploaded_file('file', 2000, 10000);
$header->write($blog, $image, isset($_POST['header_top']) ? (int) $_POST['header_top'] : 0);
/** @var Core\Media\Imagick\Manager $manager */
$manager = Core\Di\Di::_()->get('Media\Imagick\Manager');
$manager->setImage($_FILES['file']['tmp_name'])
->resize(2000, 1000);
$header->write($blog, $manager->getJpeg(), isset($_POST['header_top']) ? (int) $_POST['header_top'] : 0);
}
if ($saved) {
......@@ -364,8 +370,10 @@ class blog implements Interfaces\Api
}
if (is_uploaded_file($_FILES['header']['tmp_name'])) {
$image = get_resized_image_from_uploaded_file('header', 2000, 10000);
$header->write($blog, $image, isset($_POST['header_top']) ? (int) $_POST['header_top'] : 0);
$manager->setImage($_FILES['header']['tmp_name'])
->resize(2000, 1000);
$header->write($blog, $manager->getJpeg(), isset($_POST['header_top']) ? (int) $_POST['header_top'] : 0);
}
return Factory::response([]);
......
......@@ -98,6 +98,9 @@ class channel implements Interfaces\Api
$guid = Core\Session::getLoggedinUser()->legacy_guid;
}
/** @var Core\Media\Imagick\Manager $manager */
$manager = Core\Di\Di::_()->get('Media\Imagick\Manager');
$response = [];
switch ($pages[0]) {
......@@ -107,15 +110,17 @@ class channel implements Interfaces\Api
// so we can do clean up if one fails.
$files = array();
foreach ($icon_sizes as $name => $size_info) {
$resized = get_resized_image_from_uploaded_file('file', $size_info['w'], $size_info['h'], $size_info['square'], $size_info['upscale']);
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize($size_info['w'], $size_info['h'], $size_info['upscale'], $size_info['square']);
if ($resized) {
if ($blob = $manager->getJpeg()) {
//@todo Make these actual entities. See exts #348.
$file = new ElggFile();
$file->owner_guid = Core\Session::getLoggedinUser()->guid;
$file->setFilename("profile/{$guid}{$name}.jpg");
$file->open('write');
$file->write($resized);
$file->write($blob);
$file->close();
$files[] = $file;
} else {
......@@ -157,12 +162,15 @@ class channel implements Interfaces\Api
$item->save();
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$resized = get_resized_image_from_uploaded_file('file', 2000, 10000);
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize(2000, 10000);
$file = new Entities\File();
$file->owner_guid = $item->owner_guid;
$file->setFilename("banners/{$item->guid}.jpg");
$file->open('write');
$file->write($resized);
$file->write($manager->getJpeg());
$file->close();
$response['uploaded'] = true;
......@@ -183,12 +191,15 @@ class channel implements Interfaces\Api
);
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$resized = get_resized_image_from_uploaded_file('file', 2000, 10000);
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize(2000, 10000);
$file = new Entities\File();
$file->owner_guid = $item->owner_guid;
$file->setFilename("banners/{$item->guid}.jpg");
$file->open('write');
$file->write($resized);
$file->write($manager->getJpeg());
$file->close();
$response['uploaded'] = true;
......
......@@ -298,10 +298,11 @@ class group implements Interfaces\Api
/**
* Uploads a Group avatar
* @param GroupEntity $group
* @param GroupEntity $group
* @return GroupEntity
* @throws \IOException
* @throws \InvalidParameterException
* @throws \ImagickException
*/
protected function uploadAvatar(GroupEntity $group)
{
......@@ -309,13 +310,18 @@ class group implements Interfaces\Api
$group_owner = EntitiesFactory::build($group->getOwnerObj());
foreach (['tiny', 'small', 'medium', 'large'] as $size) {
$resized = get_resized_image_from_uploaded_file('file', $icon_sizes[$size]['w'], $icon_sizes[$size]['h'], $icon_sizes[$size]['square']);
/** @var Core\Media\Imagick\Manager $manager */
$manager = Core\Di\Di::_()->get('Media\Imagick\Manager');
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize($icon_sizes[$size]['w'], $icon_sizes[$size]['h'], true, $icon_sizes[$size]['square']);
$file = new FileEntity();
$file->owner_guid = $group->owner_guid ?: $group_owner->getGuid();
$file->setFilename("groups/{$group->getGuid()}{$size}.jpg");
$file->open('write');
$file->write($resized);
$file->write($manager->getJpeg());
$file->close();
}
......@@ -327,27 +333,34 @@ class group implements Interfaces\Api
/**
* Uploads a Group banner
* @param GroupEntity $group
* @param GroupEntity $group
* @param $banner_position
* @return GroupEntity
* @throws \IOException
* @throws \InvalidParameterException
* @throws \ImagickException
*/
protected function uploadBanner($group, $banner_position)
{
$group_owner = EntitiesFactory::build($group->getOwnerObj());
$resized = get_resized_image_from_uploaded_file('file', 3840, 1404);
/** @var Core\Media\Imagick\Manager $manager */
$manager = Core\Di\Di::_()->get('Media\Imagick\Manager');
$manager->setImage($_FILES['file']['tmp_name'])
->autorotate()
->resize(3840, 1404);
$file = new FileEntity();
$file->owner_guid = $group->owner_guid ?: $group_owner->getGuid();
$file->setFilename("group/{$group->getGuid()}.jpg");
$file->open('write');
$file->write($resized);
$file->write($manager->getJpeg());
$file->close();
$group
->setBanner(time())
->setBannerPosition($banner_position);
->setBanner(time())
->setBannerPosition($banner_position);
$group->save();
......
<?php
namespace Minds\Core\Media\Imagick;
use Minds\Core\Di\Di;
use Minds\Core\Media\Imagick\Autorotate;
use Minds\Core\Media\Imagick\Resize;
class Manager
{
/** @var Autorotate */
private $autorotate;
/** @var Resize */
private $resize;
/** @var \Imagick */
private $image;
public function __construct($autorotate = null, $resize = null)
{
$this->autorotate = $autorotate ?: Di::_()->get('Media\Imagick\Autorotate');
$this->resize = $resize ?: Di::_()->get('Media\Imagick\Resize');
}
public function getImage()
{
return $this->image;
}
/**
* @param int $quality
* @return string
* @throws \Exception
*/
public function getJpeg($quality = 80)
{
if (!$this->image) {
throw new \Exception('Output was not generated');
}
$this->image->setImageBackgroundColor('white');
$this->image = $this->image->flattenImages();
$this->image->setImageCompression($quality);
$this->image->setImageFormat('jpg');
return $this->image->getImageBlob();
}
/**
* @param $value
* @return $this
* @throws \ImagickException
*/
public function setImage($value)
{
$this->image = new \Imagick($value);
return $this;
}
/**
* @return $this
*/
public function autorotate()
{
$this->autorotate
->setImage($this->image);
$this->image = $this->autorotate->autorotate();
return $this;
}
/**
* @param bool $upscale
* @param bool $square
* @param int $width
* @param int $height
* @return $this
* @throws \Exception
*/
public function resize(int $width, int $height, bool $upscale = false, bool $square = false)
{
$this->resize->setImage($this->image)
->setUpscale($upscale)
->setSquare($square)
->setWidth($width)
->setHeight($height)
->resize();
return $this;
}
}
......@@ -55,5 +55,9 @@ class MediaProvider extends Provider
$this->di->bind('Media\Imagick\Resize', function ($di) {
return new Imagick\Resize();
}, ['useFactory' => true]);
$this->di->bind('Media\Imagick\Manager', function ($di) {
return new Imagick\Manager();
}, ['useFactory' => false]);
}
}
......@@ -154,10 +154,10 @@ class Image extends File
continue;
}
/** @var Core\Media\Proxy\Autorotate $autorotate */
/** @var Core\Media\Imagick\Autorotate $autorotate */
$autorotate = Core\Di\Di::_()->get('Media\Imagick\Autorotate');
/** @var Core\Media\Proxy\Resize $resize */
/** @var Core\Media\Imagick\Resize $resize */
$resize = Core\Di\Di::_()->get('Media\Imagick\Resize');
$image = new \Imagick($master);
......
<?php
namespace Spec\Minds\Core\Media\Imagick;
use Minds\Core\Media\Imagick\Autorotate;
use Minds\Core\Media\Imagick\Resize;
use Minds\Core\Media\Imagick\Manager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ManagerSpec extends ObjectBehavior
{
/** @var Autorotate */
private $autorotate;
/** @var Resize */
private $resize;
function let(Autorotate $autorotate, Resize $resize)
{
$this->autorotate = $autorotate;
$this->resize = $resize;
$this->beConstructedWith($autorotate, $resize);
}
function it_is_initializable()
{
$this->shouldHaveType(Manager::class);
}
function it_should_autorotate_the_image()
{
$this->autorotate->setImage(Argument::any())
->shouldBeCalled();
$this->autorotate->autorotate()
->shouldBeCalled()
->willReturn([]);
$this->autorotate()->shouldReturn($this);
}
function it_should_resize_the_image()
{
$this->resize->setImage(Argument::any())
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setUpscale(true)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setSquare(true)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setWidth(10)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setHeight(10)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->resize()
->shouldBeCalled();
$this->resize(10, 10, true, true);
}
function it_should_resize_the_image_with_no_upscaling_and_no_squaring_if_unspecified()
{
$this->resize->setImage(Argument::any())
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setUpscale(false)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setSquare(false)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setWidth(10)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->setHeight(10)
->shouldBeCalled()
->willReturn($this->resize);
$this->resize->resize()
->shouldBeCalled();
$this->resize(10, 10, false, false);
}
}