/** * Get the cropped image by File Object * * @param FileInterface $file * @param string $ratio * * @return string The new filename */ public function getCroppedImageSrcByFile(FileInterface $file, $ratio) { $absoluteImageName = GeneralUtility::getFileAbsFileName($file->getPublicUrl()); $focusPointX = MathUtility::forceIntegerInRange((int) $file->getProperty('focus_point_x'), -100, 100, 0); $focusPointY = MathUtility::forceIntegerInRange((int) $file->getProperty('focus_point_y'), -100, 100, 0); $tempImageFolder = 'typo3temp/focuscrop/'; $tempImageName = $tempImageFolder . $file->getSha1() . '-' . str_replace(':', '-', $ratio) . '-' . $focusPointX . '-' . $focusPointY . '.' . $file->getExtension(); $absoluteTempImageName = GeneralUtility::getFileAbsFileName($tempImageName); if (is_file($absoluteTempImageName)) { return $tempImageName; } $absoluteTempImageFolder = GeneralUtility::getFileAbsFileName($tempImageFolder); if (!is_dir($absoluteTempImageFolder)) { GeneralUtility::mkdir_deep($absoluteTempImageFolder); } $this->graphicalFunctions = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions'); $imageSizeInformation = getimagesize($absoluteImageName); $width = $imageSizeInformation[0]; $height = $imageSizeInformation[1]; // dimensions /** @var \HDNET\Focuspoint\Service\DimensionService $service */ $dimensionService = GeneralUtility::makeInstance('HDNET\\Focuspoint\\Service\\DimensionService'); list($focusWidth, $focusHeight) = $dimensionService->getFocusWidthAndHeight($width, $height, $ratio); $cropMode = $dimensionService->getCropMode($width, $height, $ratio); list($sourceX, $sourceY) = $dimensionService->calculateSourcePosition($cropMode, $width, $height, $focusWidth, $focusHeight, $focusPointX, $focusPointY); // generate image $sourceImage = $this->graphicalFunctions->imageCreateFromFile($absoluteImageName); $destinationImage = imagecreatetruecolor($focusWidth, $focusHeight); $this->graphicalFunctions->imagecopyresized($destinationImage, $sourceImage, 0, 0, $sourceX, $sourceY, $focusWidth, $focusHeight, $focusWidth, $focusHeight); $this->graphicalFunctions->ImageWrite($destinationImage, $absoluteTempImageName, $GLOBALS['TYPO3_CONF_VARS']['GFX']['jpg_quality']); return $tempImageName; }
/** * Internal function to retrieve the image object, * if it does not exist, an instance will be created * * @return \TYPO3\CMS\Core\Imaging\GraphicalFunctions */ protected function getImageObject() { if ($this->imageObject === NULL) { /** @var $this->imageObject \TYPO3\CMS\Core\Imaging\GraphicalFunctions */ $this->imageObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions'); $this->imageObject->init(); $this->imageObject->mayScaleUp = 0; $this->imageObject->tempPath = PATH_site . $this->imageObject->tempPath; } return $this->imageObject; }
/** * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by \TYPO3\CMS\Core\Imaging\GraphicalFunctions::readPngGif()) * * @param string $file Absolute filename of the image file from which to start the icon creation. * @return resource|int If success, image pointer, otherwise -1 * @access private * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions::readPngGif */ public static function imagecreatefrom($file) { $file = GraphicalFunctions::readPngGif($file, $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']); if (!$file) { return -1; } return $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? imagecreatefrompng($file) : imagecreatefromgif($file); }
/** * Write gif and png test * * @return \TYPO3\CMS\Install\Status\StatusInterface */ protected function writeGifAndPng() { $this->setUpDatabaseConnectionMock(); $imageProcessor = $this->initializeImageProcessor(); $parseTimeStart = GeneralUtility::milliseconds(); $testResults = array('gif' => array(), 'png' => array()); // Gif $inputFile = $this->imageBasePath . 'TestInput/Test.gif'; $imageProcessor->imageMagickConvert_forceFileNameBody = StringUtility::getUniqueId('write-gif'); $imResult = $imageProcessor->imageMagickConvert($inputFile, 'gif', '', '', '', '', array(), true); if ($imResult !== null && is_file($imResult[3])) { if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress']) { clearstatcache(); $previousSize = GeneralUtility::formatSize(filesize($imResult[3])); $methodUsed = GraphicalFunctions::gifCompress($imResult[3], ''); clearstatcache(); $compressedSize = GeneralUtility::formatSize(filesize($imResult[3])); /** @var \TYPO3\CMS\Install\Status\StatusInterface $message */ $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\InfoStatus::class); $message->setTitle('Compressed gif'); $message->setMessage('Method used by compress: ' . $methodUsed . LF . ' Previous filesize: ' . $previousSize . '. Current filesize:' . $compressedSize); } else { /** @var \TYPO3\CMS\Install\Status\StatusInterface $message */ $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\InfoStatus::class); $message->setTitle('Gif compression not enabled by [GFX][gif_compress]'); } $testResults['gif']['message'] = $message; $testResults['gif']['title'] = 'Write gif'; $testResults['gif']['outputFile'] = $imResult[3]; $testResults['gif']['referenceFile'] = $this->imageBasePath . 'TestReference/Write-gif.gif'; $testResults['gif']['command'] = $imageProcessor->IM_commands; } else { $testResults['gif']['error'] = $this->imageGenerationFailedMessage(); } // Png $inputFile = $this->imageBasePath . 'TestInput/Test.png'; $imageProcessor->IM_commands = array(); $imageProcessor->imageMagickConvert_forceFileNameBody = StringUtility::getUniqueId('write-png'); $imResult = $imageProcessor->imageMagickConvert($inputFile, 'png', '', '', '', '', array(), true); if ($imResult !== null) { $testResults['png']['title'] = 'Write png'; $testResults['png']['outputFile'] = $imResult[3]; $testResults['png']['referenceFile'] = $this->imageBasePath . 'TestReference/Write-png.png'; $testResults['png']['command'] = $imageProcessor->IM_commands; } else { $testResults['png']['error'] = $this->imageGenerationFailedMessage(); } $this->view->assign('testResults', $testResults); return $this->imageTestDoneMessage(GeneralUtility::milliseconds() - $parseTimeStart); }
/** * Returns filename of the png/gif version of the input file (which can be png or gif). * If input file type does not match the wanted output type a conversion is made and temp-filename returned. * * @param string $theFile Filepath of image file * @param bool $output_png If set, then input file is converted to PNG, otherwise to GIF * @return string If the new image file exists, its filepath is returned * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. Use \TYPO3\CMS\Core\Imaging\GraphicalFunctions::readPngGif() instead. */ public static function read_png_gif($theFile, $output_png = false) { static::logDeprecatedFunction(); $newFile = GraphicalFunctions::readPngGif($theFile, $output_png); return $newFile; }
/** * @throws Exception * @return void */ public function preprocessImage() { $src = $this->arguments['src']; $width = $this->arguments['width']; $height = $this->arguments['height']; $minW = $this->arguments['minW']; $minH = $this->arguments['minH']; $maxW = $this->arguments['maxW']; $maxH = $this->arguments['maxH']; $format = $this->arguments['format']; $quality = $this->arguments['quality']; $treatIdAsReference = (bool) $this->arguments['treatIdAsReference']; if ('BE' === TYPO3_MODE) { $this->simulateFrontendEnvironment(); } $setup = array('width' => $width, 'height' => $height, 'minW' => $minW, 'minH' => $minH, 'maxW' => $maxW, 'maxH' => $maxH, 'treatIdAsReference' => $treatIdAsReference); if (FALSE === empty($format)) { $setup['ext'] = $format; } if (0 < intval($quality)) { $quality = MathUtility::forceIntegerInRange($quality, 10, 100, 75); $setup['params'] = '-quality ' . $quality; } if ('BE' === TYPO3_MODE && '../' === substr($src, 0, 3)) { $src = substr($src, 3); } $this->imageInfo = $this->contentObject->getImgResource($src, $setup); $GLOBALS['TSFE']->lastImageInfo = $this->imageInfo; if (FALSE === is_array($this->imageInfo)) { throw new Exception('Could not get image resource for "' . htmlspecialchars($src) . '".', 1253191060); } if ((double) substr(TYPO3_version, 0, 3) < 7.1) { $this->imageInfo[3] = GeneralUtility::png_to_gif_by_imagemagick($this->imageInfo[3]); } else { $this->imageInfo[3] = GraphicalFunctions::pngToGifByImagemagick($this->imageInfo[3]); } $GLOBALS['TSFE']->imagesOnPage[] = $this->imageInfo[3]; $publicUrl = rawurldecode($this->imageInfo[3]); $this->mediaSource = $GLOBALS['TSFE']->absRefPrefix . GeneralUtility::rawUrlEncodeFP($publicUrl); if ('BE' === TYPO3_MODE) { $this->resetFrontendEnvironment(); } }
/** * @test * @dataProvider getScaleForImageDataProvider */ public function getScaleForImage($info, $width, $height, $options, $expected) { $result = $this->subject->getImageScale($info, $width, $height, $options); $this->assertEquals($result, $expected); }
/** * @param array $files * @param boolean $onlyProperties * @throws Exception * @return array|NULL */ public function preprocessImages($files, $onlyProperties = FALSE) { if (TRUE === empty($files)) { return NULL; } if ('BE' === TYPO3_MODE) { $this->simulateFrontendEnvironment(); } $setup = array('width' => $this->arguments['width'], 'height' => $this->arguments['height'], 'minW' => $this->arguments['minWidth'], 'minH' => $this->arguments['minHeight'], 'maxW' => $this->arguments['maxWidth'], 'maxH' => $this->arguments['maxHeight'], 'treatIdAsReference' => FALSE); $images = array(); foreach ($files as $file) { $imageInfo = $this->contentObject->getImgResource($file->getUid(), $setup); $GLOBALS['TSFE']->lastImageInfo = $imageInfo; if (FALSE === is_array($imageInfo)) { throw new Exception('Could not get image resource for "' . htmlspecialchars($file->getCombinedIdentifier()) . '".', 1253191060); } if ((double) substr(TYPO3_version, 0, 3) < 7.1) { $imageInfo[3] = GeneralUtility::png_to_gif_by_imagemagick($imageInfo[3]); } else { $imageInfo[3] = GraphicalFunctions::pngToGifByImagemagick($imageInfo[3]); } $imageInfo[3] = GeneralUtility::png_to_gif_by_imagemagick($imageInfo[3]); $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3]; if (TRUE === GeneralUtility::isValidUrl($imageInfo[3])) { $imageSource = $imageInfo[3]; } else { $imageSource = $GLOBALS['TSFE']->absRefPrefix . GeneralUtility::rawUrlEncodeFP($imageInfo[3]); } if (TRUE === $onlyProperties) { $file = ResourceUtility::getFileArray($file); } $images[] = array('info' => $imageInfo, 'source' => $imageSource, 'file' => $file); } if ('BE' === TYPO3_MODE) { $this->resetFrontendEnvironment(); } return $images; }
/** * Creates a color image selector * * @return void * @todo Define visibility */ public function colorImage() { // Handling color-picker image if any: if (!$this->imageError) { if ($this->pickerImage) { if (GeneralUtility::_POST('coords_x')) { $this->colorValue = '#' . $this->getIndex(\TYPO3\CMS\Core\Imaging\GraphicalFunctions::imageCreateFromFile($this->pickerImage), GeneralUtility::_POST('coords_x'), GeneralUtility::_POST('coords_y')); } $pickerFormImage = ' <p class="c-head">' . $GLOBALS['LANG']->getLL('colorpicker_fromImage', TRUE) . '</p> <input type="image" src="../' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($this->pickerImage) . '" name="coords" style="cursor:crosshair;" /><br />'; } else { $pickerFormImage = ''; } } else { $pickerFormImage = ' <p class="c-head">' . htmlspecialchars($this->imageError) . '</p>'; } return $pickerFormImage; }