3

I have a ViewHelper that processes some images. I have an image path to an original file. I need to resize this image.

Is there a PHP code I can use in TYPO3 to do this?

I tried this:

$imgPath = 'img/path/from_database.jpg
$imgConf = array();
$imgConf['file'] = $imgPath;
$imgConf['altText'] = "Sample alt text.";
$image = $this->cObj->IMAGE($imgConf);

but I'm getting this exception: "Call to a member function IMAGE() on null"

My ViewHelper inherits from: \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper

Alan
  • 1,322
  • 1
  • 21
  • 36
  • I assume that this is the field `media` of table `pages`, correct? You might consider, to use the available `` view-helper... Is the view component called from an Extbase context or by some TypoScript `FLUIDTEMPLATE`? – Oliver Hader Oct 14 '16 at 09:50
  • @OliverHader sorry, the code was pasted from the other website, I've edited it now to match my code more closely but it doesn't affect this issue. This ViewHelper is called inside Fluid template placed in the same extension(as ViewHelper's extension) so I assume it's Extbase context. I'd like to use but I don't know how to use it in PHP code? – Alan Oct 14 '16 at 10:08

1 Answers1

9

First of all you should say what TYPO3 version you use and if your extension uses Extbase or the old pibased code.

I guess it's Extbase since you inherit from a namespaced viewhelper. My advice would be to take a look at the original f:image viewhelper code (TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper) and copy what you need.

first add this to the top part of you viewhelper:

/**
 * @var \TYPO3\CMS\Extbase\Service\ImageService
 * @inject
 */
protected $imageService;

Then the code in your method

$image = $this->imageService->getImage($imgPath);

// you have to set these variables or remove if you don't need them
$processingInstructions = array(
    'width' => $width,
    'height' => $height,
    'minWidth' => $minWidth,
    'minHeight' => $minHeight,
    'maxWidth' => $maxWidth,
    'maxHeight' => $maxHeight,
    'crop' => $crop,
);
$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $this->imageService->getImageUri($processedImage);

$imageUri now holds the path to the resized image.

PS: The example you copied is from the old pibased system that no longer exists since the 7.x branch.

Dimitri L.
  • 4,499
  • 1
  • 15
  • 19
  • 1
    Thanks, that works great. The only issue was that dependency injection didn't work in my case - I cleared the cache(Cache folder and cache icon in back-end) and replaced CR LF with LF - didn't help. Because of that I had to get this object that way `\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("TYPO3\\CMS\\Extbase\\Service\\ImageService")`. – Alan Oct 17 '16 at 12:31
  • 1
    Thanks, I had to give [3 arguments](http://labor.99grad.de/fal-bild-im-backend-umrechnen-thumbnail-in-extension-erzeugen/) to the `getImage` function `$image = $this->imageService->getImage($filename, null, false);` using TYPO3 8.7. I ran into alan's injection issue too. The solution for me was to `Dump Autoload Information` in the Install Tool. – ǝlpoodooɟƃuooʞ Mar 12 '19 at 15:30
  • 1
    You always have to update the autoload files when you create a new class. You can use it either with "Dump Autoload Information" or just dele the files inside `typo3temp/autoload` – Dimitri L. Mar 13 '19 at 16:09