Skip to content

Commit

Permalink
Merge pull request #48 from craftcms/feature/pt-1978-image-dimensions…
Browse files Browse the repository at this point in the history
…-not-updated-when-replacing-an-image-in-the

Image dimensions not updated when replacing an image
  • Loading branch information
timkelty authored Jul 25, 2024
2 parents c736774 + a7e0263 commit 0f04bf1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ public function actionReplaceFile(): Response
$sourceAssetId = $this->request->getBodyParam('sourceAssetId');
$filename = $this->request->getBodyParam('filename');
$targetFilename = $this->request->getBodyParam('targetFilename');
$size = $this->request->getBodyParam('size');
$width = $this->request->getBodyParam('width');
$height = $this->request->getBodyParam('height');
$lastModifiedMs = (int) $this->request->getBodyParam('lastModified');
$dateModified = $lastModifiedMs
? DateTime::createFromFormat('U', (string) floor($lastModifiedMs / 1000))
: new DateTime();

$assets = Craft::$app->getAssets();

// Must have at least one existing asset (source or target).
Expand All @@ -261,6 +269,10 @@ public function actionReplaceFile(): Response

// Handle the Element Action
if ($assetToReplace !== null && $filename) {
$assetToReplace->width = $width;
$assetToReplace->height = $height;
$assetToReplace->size = $size;
$assetToReplace->dateModified = $dateModified;
if (!$this->replaceAssetFile($assetToReplace, $filename, $targetFilename)) {
throw new Exception('Unable to replace asset.');
}
Expand Down Expand Up @@ -349,9 +361,9 @@ public function replaceAssetFile(Asset $asset, string $filename, string $targetF
$asset->getVolume()->deleteFile($oldPath);

// Try again, in case the resulting filename has a tmp suffix from `avoidFilenameConflicts`
if ($saved && $oldPath !== $asset->getPath()) {
if ($saved && $targetFilename !== $asset->getFilename()) {
$asset->newFilename = $targetFilename;
$saved = Craft::$app->getElements()->saveElement($asset);
$saved = $this->saveAsset($asset);
}

if ($assets->hasEventHandlers($assets::EVENT_AFTER_REPLACE_ASSET)) {
Expand Down

0 comments on commit 0f04bf1

Please sign in to comment.