Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 138 additions & 21 deletions Engine/Modules/Media/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

namespace Oforge\Engine\Modules\Media;

use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Exception;
use Oforge\Engine\Modules\AdminBackend\Core\Services\BackendNavigationService;
use Oforge\Engine\Modules\Core\Abstracts\AbstractBootstrap;
use Oforge\Engine\Modules\Core\Exceptions\ServiceNotFoundException;
use Oforge\Engine\Modules\Core\Exceptions\Template\TemplateNotFoundException;
use Oforge\Engine\Modules\Core\Models\Config\ConfigType;
use Oforge\Engine\Modules\Core\Services\ConfigService;
use Oforge\Engine\Modules\I18n\Helper\I18N;
use Oforge\Engine\Modules\Media\Models\Media;
use Oforge\Engine\Modules\Media\Services\ImageCompressService;
use Oforge\Engine\Modules\Media\Services\MediaService;
use Oforge\Engine\Modules\Media\Twig\MediaExtension;
use Oforge\Engine\Modules\TemplateEngine\Core\Services\TemplateRenderService;
use Twig_Error_Loader;

/**
* Class Bootstrap
*
* @package Oforge\Engine\Modules\Media
*/
class Bootstrap extends AbstractBootstrap {
class Bootstrap extends AbstractBootstrap
{

public function __construct() {
public function __construct()
{
$this->endpoints = [
Controller\Backend\Media\AjaxController::class,
Controller\Backend\Media\MediaController::class,
Expand All @@ -37,27 +38,143 @@ public function __construct() {
'media' => MediaService::class,
'image.compress' => ImageCompressService::class,
];

$this->setConfiguration(
'settingGroups',
[
[
'name' => 'media',
'label' => [
'en' => 'Media',
'de' => 'Medien',
],
'items' => [
[
'name' => 'media_upload_image_adjustment_enabled',
'type' => ConfigType::BOOLEAN,
'default' => false,
'label' => [
'en' => 'Image upload: Adjustments when uploading?',
'de' => 'Bildupload: Anpassungen beim hochladen?',
],
'required' => false,
],# media_upload_image_adjustment_enabled
[
'name' => 'media_upload_image_adjustment_downscaling_max_width',
'type' => ConfigType::INTEGER,
'default' => 0,
'label' => [
'en' => 'Image upload: Down scaling to max width (deactivated if 0)',
'de' => 'Bildupload: Verkleinern auf maximale Breite (deaktiviert wenn 0)',
],
'required' => false,
],# media_upload_image_adjustment_downscaling_max_width
[
'name' => 'media_upload_image_adjustment_compress',
'type' => ConfigType::BOOLEAN,
'default' => false,
'label' => [
'en' => 'Image upload: Compress?',
'de' => 'Bildupload: Komprimieren?',
],
'required' => false,
],# media_upload_image_adjustment_compress
[
'name' => 'media_image_upscaling_enabled',
'type' => ConfigType::BOOLEAN,
'default' => true,
'label' => [
'en' => 'Image upscaling (of small images) ?',
'de' => 'Bild-Upscaling (von kleinen Bildern)?',
],
'required' => false,
],# media_upscaling_enabled
],
],# media
]
);
}

/** @inheritDoc */
public function uninstall(bool $keepData)
{
if ( !$keepData) {
$this->uninstallSettings();
}
}

/** @inheritDoc */
public function install()
{
$this->installSettings();
}

/** @inheritDoc */
public function activate() {
public function activate()
{
/** @var TemplateRenderService $templateRenderer */
$templateRenderer = Oforge()->Services()->get('template.render');
$templateRenderer->View()->addExtension(new MediaExtension());
/** @var BackendNavigationService $backendNavigationService */
$backendNavigationService = Oforge()->Services()->get('backend.navigation');
$backendNavigationService->add([
'name' => 'module_media',
'order' => 3,
'position' => 'sidebar',
]);
$backendNavigationService->add([
'name' => 'module_media_media',
'parent' => 'module_media',
'icon' => 'fa fa-picture-o',
'path' => 'backend_media',
'position' => 'sidebar',
'order' => 1,
]);
$backendNavigationService->add(
[
'name' => 'module_media',
'order' => 3,
'position' => 'sidebar',
]
);
$backendNavigationService->add(
[
'name' => 'module_media_media',
'parent' => 'module_media',
'icon' => 'fa fa-picture-o',
'path' => 'backend_media',
'position' => 'sidebar',
'order' => 1,
]
);
}

/**
*
*/
protected function installSettings()
{
try {
/** @var ConfigService $configService */
$configService = Oforge()->Services()->get('config');
foreach ($this->getConfiguration('settingGroups') as $settingGroup) {
I18N::translate('config_group_' . $settingGroup['name'], $settingGroup['label']);
foreach ($settingGroup['items'] as $setting) {
$labelKey = 'config_' . $setting['name'];
I18N::translate($labelKey, $setting['label']);
$setting['label'] = $labelKey;
$setting['group'] = $settingGroup['name'];
$configService->add($setting);
}
}
} catch (Exception $exception) {
Oforge()->Logger()->logException($exception);
}
}

/**
*
*/
protected function uninstallSettings()
{
try {
/** @var ConfigService $configService */
$configService = Oforge()->Services()->get('config');
foreach ($this->getConfiguration('settingGroups') as $settingGroup) {
foreach ($settingGroup['items'] as $setting) {
$configService->remove($setting['name']);
}
}
} catch (Exception $exception) {
Oforge()->Logger()->logException($exception);
}
}

}
11 changes: 9 additions & 2 deletions Engine/Modules/Media/Controller/Backend/Media/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ public function demoAction(Request $request, Response $response) {
* @throws OptimisticLockException
* @EndpointAction(path="/upload")
*/
public function uploadAction(Request $request, Response $response) {
public function uploadAction(Request $request, Response $response)
{
if (isset($_FILES['upload-media'])) {
/** @var MediaService $service */
$service = Oforge()->Services()->get('media');
$created = $service->add($_FILES['upload-media']);
Oforge()->View()->assign(['created' => $created != null ? $created->toArray() : false]);
Oforge()->View()->assign(
[
'json' => [
'created' => $created !== null ? $created->toArray() : false,
]
]
);
}
}

Expand Down
68 changes: 41 additions & 27 deletions Engine/Modules/Media/Services/ImageCompressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
use Doctrine\ORM\ORMException;
use Imagick;
use ImagickException;
use Insertion\Models\InsertionMedia;
use Oforge\Engine\Modules\Core\Abstracts\AbstractDatabaseAccess;
use Oforge\Engine\Modules\Core\Exceptions\ServiceNotFoundException;
use Oforge\Engine\Modules\Core\Services\ConfigService;
use Oforge\Engine\Modules\Media\Models\Media;

/**
* Class ImageCompressService
*
* @package Oforge\Engine\Modules\Media\Services
*/
class ImageCompressService extends AbstractDatabaseAccess {

public function __construct() {
parent::__construct(['default' => Media::class, 'insertionMedia' => InsertionMedia::class]);
}
class ImageCompressService
{
/** @var ConfigService $configService */
private $configService;

/**
* @param string|null $path
Expand All @@ -29,25 +27,26 @@ public function __construct() {
* @throws ORMException
* @throws ServiceNotFoundException
*/
public function getPath(?string $path, int $width = 0) : ?string {
if (!isset($path)) {
public function getPath(?string $path, int $width = 0) : ?string
{
if ( !isset($path)) {
return null;
}
/** @var MediaService $mediaService */
$mediaService = Oforge()->Services()->get('media');
$media = $mediaService->getByPath($path);

if (!isset($media)) {
if ( !isset($media)) {
$media = $mediaService->getById($path);
if (!isset($media)) {
if ( !isset($media)) {
return $path;
}
}

if ($width > 0) {
$fileExtension = $this->getFileExtension($media);

if (!empty($fileExtension)) {
if ( !empty($fileExtension)) {
$cacheUrl = substr($media->getPath(), 0, -strlen($fileExtension) - 1) . '_' . $width . '.' . $fileExtension;
//File is already compressed and stored
if (file_exists(ROOT_PATH . $cacheUrl)) {
Expand All @@ -68,24 +67,24 @@ public function getPath(?string $path, int $width = 0) : ?string {
return $media->getPath();
}

public function getFileExtension(Media $media) {
$fileExtension = '';
public function getFileExtension(Media $media) : string
{
$tmpFileExtension = pathinfo($media->getPath(), PATHINFO_EXTENSION);
switch ($media->getType()) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
$fileExtension = $tmpFileExtension;
break;
return $tmpFileExtension;
default:
return '';
}

return $fileExtension;
}

/**
* @param string $imagePath
*/
public function compress(string $imagePath) {
public function compress(string $imagePath)
{
try {
if (extension_loaded('imagick')) {
$imagick = new Imagick(ROOT_PATH . $imagePath);
Expand All @@ -97,15 +96,14 @@ public function compress(string $imagePath) {
$imagick->setImageFormat('jpeg');
$imagick->setImageCompressionQuality(40);
$imagick->setSamplingFactors(['2x2', '1x1', '1x1']);
//$profiles = $imagick->getImageProfiles("icc", true);
//$profiles = $imagick->getImageProfiles('icc', true);
// $imagick->stripImage();
// if (!empty($profiles)) {
// $imagick->profileImage('icc', $profiles['icc']);
// }

$imagick->setInterlaceScheme(Imagick::INTERLACE_JPEG);
$imagick->setColorspace(Imagick::COLORSPACE_SRGB);

} elseif ($image_types[2] === IMAGETYPE_GIF) {
$imagick->setImageFormat('gif');
} elseif ($image_types[2] === IMAGETYPE_PNG) {
Expand All @@ -118,21 +116,37 @@ public function compress(string $imagePath) {
$imagick->writeImage(ROOT_PATH . $imagePath);
}
} catch (ImagickException $e) {
Oforge()->Logger()->get()->error('ImagickException', ["imagePath" => $imagePath ]);
Oforge()->Logger()->get()->error('ImagickException', ['imagePath' => $imagePath]);
}
}

public function scale(Media $media, int $width, string $cacheUrl) {
public function scale(Media $media, int $targetWidth, string $cacheUrl)
{
if ( !isset($this->configService)) {
$this->configService = Oforge()->Services()->get('config');
}
try {
if (extension_loaded('imagick')) {
$imagick = new Imagick(ROOT_PATH . $media->getPath());
$widthCurrent = $imagick->getImageWidth();
$heightCurrent = $imagick->getImageHeight();
$imagick->scaleImage($width, (int) (1.0 * $width / $widthCurrent * $heightCurrent));
$currentWidth = $imagick->getImageWidth();
$currentHeight = $imagick->getImageHeight();

if ($currentWidth > $targetWidth || $this->configService->get('media_image_upscaling_enabled')) {
$imagick->scaleImage($targetWidth, (int)(1.0 * $targetWidth / $currentWidth * $currentHeight));
}

$imagick->writeImage(ROOT_PATH . $cacheUrl);
}
} catch (ImagickException $e) {
Oforge()->Logger()->get()->error('ImagickException', ["media" => $media->toArray(1),"width" => $width, "cacheUrl" => $cacheUrl ]);
Oforge()->Logger()->get()->error(
'ImagickException',
[
'media' => $media->toArray(1),
'targetWidth' => $targetWidth,
'cacheUrl' => $cacheUrl,
]
);
}
}

}
Loading