<?php namespace SGalinski\SgNews\Service; /*************************************************************** * Copyright notice * * (c) sgalinski Internet Services (https://www.sgalinski.de) * * All rights reserved * * This script is part of the TYPO3 project. The TYPO3 project is * free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * The GNU General Public License can be found at * http://www.gnu.org/copyleft/gpl.html. * * This script 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 General Public License for more details. * * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ use TYPO3\CMS\Core\Charset\CharsetConverter; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Domain\Model\FileReference; /** * Service for the media stuff from the domain model News. */ class ImageService implements SingletonInterface { /** * @var CharsetConverter */ protected $characterSetConverter; /** * Crops the first media file from the given News and returns the cropped image. * * Expected coordinates array structure: * array ( * 'x1' => string, * 'y1' => string, * 'w' => string, * 'h' => string, * ) * * @param FileReference $image * @param array $coordinates * @param string $path * @param string $prefix * @return string * @throws \RuntimeException * @throws \InvalidArgumentException */ public function cropFirstMediaImage( FileReference $image, array $coordinates, $path = 'typo3temp/sg_news/', $prefix = '' ): string { $originalResource = $image->getOriginalResource(); $extension = strtolower($originalResource->getExtension()); if (!in_array($extension, ['gif', 'png', 'jpg', 'jpeg', 'svg', 'webp'], TRUE)) { return ''; } if ($prefix !== '') { $prefix = str_replace(['?', '&', ' '], '_', $prefix); $prefix = $this->characterSetConverter->specCharsToASCII('utf-8', $prefix) . '_'; } $fileName = $coordinates['w'] . $coordinates['h'] . $coordinates['x1'] . $coordinates['y1']; $fileName .= $originalResource->getNameWithoutExtension(); $croppedImage = $path . $prefix . sha1($fileName) . '.' . $extension; $originalImage = Environment::getPublicPath() . '/' . rawurldecode($originalResource->getPublicUrl()); if ($originalImage !== '' && $croppedImage !== '' && !is_file(Environment::getPublicPath() . '/' . $croppedImage)) { GeneralUtility::mkdir_deep(Environment::getPublicPath() . '/' . $path); $imagick = new \Imagick($originalImage); $imagick->cropImage($coordinates['w'], $coordinates['h'], $coordinates['x1'], $coordinates['y1']); $imagick->writeImage(Environment::getPublicPath() . '/' . $croppedImage); if (!is_file(Environment::getPublicPath() . '/' . $croppedImage)) { $croppedImage = ''; } else { GeneralUtility::fixPermissions(Environment::getPublicPath() . '/' . $croppedImage); } } return $croppedImage; } /** * @param CharsetConverter $characterSetConverter */ public function injectCharacterSetConverter(CharsetConverter $characterSetConverter) { $this->characterSetConverter = $characterSetConverter; } }