Hello together,
fyi: (same) issue on Github
I’m trying to extend the package meteko/image-resizer with a cli command. That also handles/resizes existing images with the given configuration.
I basically copied the main logic from the ImageResizerUtility.php and put it into a foreach, to interate through the assets:
<?php
declare(strict_types=1);
namespace Meteko\ImageResizer\Command;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Service\AssetService;
use Neos\Media\Domain\Model\ImageVariant;
use Neos\Utility\MediaTypes;
use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Neos\Media\Domain\Service\ThumbnailService;
class ImageResizerCommandController extends CommandController
{
/**
* @Flow\InjectConfiguration("configuration")
* @var array
*/
protected $configuration;
/**
* @Flow\Inject
* @var ThumbnailService
*/
protected $thumbnailService;
/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;
/**
* @Flow\Inject
* @var AssetService
*/
protected $assetService;
/**
* Resizes all images
*
* Hello there
*
* Hello there
*
* @param boolean $dryrun Do a dryrun to test what images will be resized
*/
public function resizeAllCommand(bool $dryrun = false): void
{
if ($dryrun) {
$this->outputLine($dryrun ? 'true' : 'false');
// Fetch all assets
$assets = $this->assetRepository->findAll();
foreach ($assets as $asset) {
if ($asset instanceof ImageVariant) {
return;
}
$configuration = $this->configuration;
$sourceMediaType = MediaTypes::parseMediaType($asset->getMediaType());
if ($sourceMediaType['type'] !== 'image' || $configuration === []) {
return;
}
$thumbnailConfiguration = new ThumbnailConfiguration(
$configuration['width'] ?? null,
$configuration['maximumWidth'] ?? null,
$configuration['height'] ?? null,
$configuration['maximumHeight'] ?? null,
$configuration['allowCropping'] ?? false,
$configuration['allowUpScaling'] ?? false,
false,
$configuration['quality'] ?? null,
$configuration['format'] ?? null
);
$this->outputLine('Label: ' . $asset->getLabel());
$thumbnail = $this->thumbnailService->getThumbnail($asset, $thumbnailConfiguration);
$this->assetService->replaceAssetResource($asset, $thumbnail->getResource());
}
} else {
$this->outputLine($dryrun ? 'true' : 'false');
$this->outputLine('No dryrun today');
}
}
}
Running this, causes:
Label: Image1.jpg
Label: Image2.jpg
[...]
Label: IMG-1276-scaled.jpg
Crop coordinates must start at minimum 0, 0 position from top left corner, crop height and width must be positive integers and must not exceed the current image borders
Type: Imagine\Exception\OutOfBoundsException
File: Packages/Libraries/imagine/imagine/src/Gmagick/Image.php
Line: 136
[...]
I (temporarly) fix this, by adding if ($asset->getLabel() !== 'IMG-1276-scaled.jpg')
as currently i don’t want to know why that image is having troubles.
Running it again, at the very end i receive the following exception and the images are broken in the media module:
An exception occurred while executing 'UPDATE neos_media_domain_model_asset SET lastmodified = ?, resource = ?
WHERE persistence_object_identifier = ?' with params ["2024-04-25
04:58:11", "81f131e0-f652-4709-ab56-d59c02c222fd",
"8aaea708-5ec2-4cb4-a964-5157250c691d"]:
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on
table "neos_media_domain_model_asset" violates foreign key constraint
"fk_b8306b8ebc91f416"
DETAIL: Key (resource)=(81f131e0-f652-4709-ab56-d59c02c222fd) is not
present in table "neos_flow_resourcemanagement_persistentresource".
Type: Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException
File: Packages/Libraries/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQ
LDriver.php
Line: 67
Nested exception:
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "neos_media_domain_model_asset" violates foreign key constraint "fk_b8306b8ebc91f416"
DETAIL: Key (resource)=(81f131e0-f652-4709-ab56-d59c02c222fd) is not
present in table "neos_flow_resourcemanagement_persistentresource".
Type: Doctrine\DBAL\Driver\PDO\Exception
Code: 23503
File: Packages/Libraries/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php
Line: 18
Nested exception:
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "neos_media_domain_model_asset" violates foreign key constraint "fk_b8306b8ebc91f416"
DETAIL: Key (resource)=(81f131e0-f652-4709-ab56-d59c02c222fd) is not
present in table "neos_flow_resourcemanagement_persistentresource".
Type: PDOException
Code: 23503
File: Packages/Libraries/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php
Line: 117
Where could be the issue here?
Neos: 8.3.12
Flow: 8.3.8
PHP: 8.3.6
DB: postgresql 16.2