[SOLVED] Process task: modify an image

Hello,

I’m a bit confused at the moment. I’d like to implement a processor that hooks into the image generation. From the original images, for example, images in different sizes and various crops are created.

Once that is done, I’d like to hook into the process. The hooking works quite well with:

What I don’t understand, however, is the task workflow. I receive a TaskInterface with a SourceFile and a TargetFile.

I suppose it would be unwise to manipulate the source file, so I take the path from the TargetFile and save my result under

$processingPath = $targetFile->getForLocalProcessing(true);

Then I thought I needed to call

$targetFile->updateWithLocalFile($processingPath);

BUT that doesn’t work because of the identifier, which I can’t set—why is that?

public function updateWithLocalFile(string $filePath): void
{
    if (empty($this->identifier)) {
        throw new \RuntimeException('Cannot update original file!', 1350582054);
    }
}

Have I possibly completely misunderstood the process? Or is it just a small detail…

Thanks in advance for any helpful answers…
Clemens

Here is my Coding:


    /**
     * Processes the given task and sets the processing result in the task object.
     */
    public function processTask(TaskInterface $task): void
    {
          $watermarkUids = $this->get_category_watermark_file_uids($task->getSourceFile());
          $wm = $this->fileRepository->findByUid($watermarkUids[0]['uid_local']);
          $this->addWatermark($task->getSourceFile(), $task->getTargetFile(), $wm, 'test.jpg');
    }

    private function addWatermark(
        ProcessedFile|File $sourceFile,
        ProcessedFile|File $targetFile,
        File $watermarkFile,
        string $targetImagePath
    ): void {

        $background = $this->create_image($targetFile);
        $watermark = $this->create_image($watermarkFile);


        $bgWidth = imagesx($background);
        $bgHeight = imagesy($background);
        $wmWidth = imagesx($watermark);
        $wmHeight = imagesy($watermark);

        $padding = 10;
        $posX = $bgWidth - $wmWidth - $padding;
        $posY = $bgHeight - $wmHeight - $padding;

        imagecopy($background, $watermark, $posX, $posY, 0, 0, $wmWidth, $wmHeight);
        $processingPath = $targetFile->getForLocalProcessing(true);
        imagejpeg($background, $processingPath, 90); // 90 = JPEG-Qualität
        imagedestroy($background);
        imagedestroy($watermark);
       
        $targetFile->updateWithLocalFile($processingPath);
    }

I think maybe GitHub - plan2net/webp: Create a WebP copy for images (TYPO3 CMS) uses the services quite understandable, that could help?

Specifically this: webp/Classes/Service/Webp.php at master · plan2net/webp · GitHub

Hi @docmcfly!

I wonder if you have managed to solve your issue. Was @ghi’s suggestion of help to you?

Hi @mabolek

Thanks for your follow-up. Unfortunately, I’ve been on the road a lot these past weeks and haven’t really had the chance to revisit the problem. However, I did have it on my agenda to get back to you in the end…

Cool! Looking forward to hearing more once you’re back.

Enjoy your travels!

Thank you. I’m back now and have taken up the topic again.

Basically, the AfterFileProcessingEvent from the WebP extension really helped me. The first tests also look very promising. When my extension reaches the beta stage, I’ll post the GitHub link here.

However, I’ve run into a problem that I’ve temporarily solved with a workaround, but I’m not entirely happy with it:

In the AfterFileProcessingEvent, I can’t tell whether the images are for the front end or back end. In the back end, only very small images (150px and below) are needed. Therefore, my mechanism only processes larger images—in the hope that those are for the front end.

Since my extension is supposed to add watermarks to front-end images without modifying the originals, this is the only viable approach I’ve found so far. If there are any ideas or information that could help me here, I’d love to hear them. :wink:

Regards,
Clemens

One hour later - Addendum: Really dumb oversight. My extension only works when the image size is changed—i.e., when a crop is selected in the editor. If I understand correctly, if an image is smaller than the required size, no ProcessedFile is created and it isn’t used later by the renderer. That saves resources, but I have no way to apply my watermark.

New day, new insight… of course it doesn’t work so easily with the AfterFileProcessingEvent.
I’m going to create my own task after all, one that overrides the normal Image-Resize-Crop-Task. Typo3 simply doesn’t support a cascade of tasks. :frowning:
I’ll let you know when I’ve made progress…

Now it’s running exactly as I envisioned. Perfect.

One big caveat: I have two options:
LocalImageProcessor has an annoying optimization mechanism in my case: if the images are small, no processed files are created—so there’s nowhere to apply my watermark. :frowning: In GraphicalFunctions#resize(), there’s a forceCreation flag, but LocalImageProcessor has it hard-coded to never use it.

I see two options:

Enhance LocalImageProcessor and its helpers via XClass and submit the changes so that this issue is resolved in a future release.

OR

Duplicate LocalImageProcessor and its helpers, but then maintain all changes to LocalImageProcessor myself.

What’s the best way to submit my request? As a pull request or as a feature issue?

Addendum: Oh yes, the WebP extension didn’t help me :frowning:

Here the link to my extension:

When my extension work in a production process, i public the extension and and please request a core change.

Best regards Clemens

It’s great that you’d like to contribute your enhancement to TYPO3’s core! :medal_sports:

The best would be to submit a feature issue first. Then you can contribute a patch that resolves your own issue. Here’s a step-by-step walkthrough of the contribution process:

https://docs.typo3.org/permalink/t3contribute:quickstart-create-a-patch

Feel free to post here on talk.typo3.org if you have any further questions.

Thanks for your post.
My patch is on the road.. :wink:

https://review.typo3.org/c/Packages/TYPO3.CMS/+/90428

1 Like