Skip to content

Commit a156298

Browse files
committed
write PNG metadata chunks before IEND chunk
1 parent bb1ba14 commit a156298

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/WriteIptc.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ public function execute(FileInterface $file, MetadataInterface $metadata): FileI
7171
}
7272

7373
if (empty($pngIptcSegments)) {
74-
$segments[] = $this->createPngIptcSegment($metadata);
75-
7674
return $this->fileFactory->create([
7775
'path' => $file->getPath(),
78-
'segments' => $segments
76+
'segments' => $this->insertPngIptcSegment($segments, $this->createPngIptcSegment($metadata))
7977
]);
8078
}
8179

@@ -89,6 +87,22 @@ public function execute(FileInterface $file, MetadataInterface $metadata): FileI
8987
]);
9088
}
9189

90+
/**
91+
* Insert IPTC segment to image png segments before IEND chunk
92+
*
93+
* @param SegmentInterface[] $segments
94+
* @param SegmentInterface $xmpSegment
95+
* @return SegmentInterface[]
96+
*/
97+
private function insertPngIptcSegment(array $segments, SegmentInterface $xmpSegment): array
98+
{
99+
return array_merge(
100+
array_slice($segments, 0, count($segments) - 1),
101+
[$xmpSegment],
102+
array_slice($segments, count($segments) - 1)
103+
);
104+
}
105+
92106
/**
93107
* Create new zTXt segment with metadata
94108
*

app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/WriteXmp.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ public function execute(FileInterface $file, MetadataInterface $metadata): FileI
8080
}
8181

8282
if (empty($pngXmpSegments)) {
83-
$segments[] = $this->createPngXmpSegment($metadata);
84-
8583
return $this->fileFactory->create([
8684
'path' => $file->getPath(),
87-
'segments' => $segments
85+
'segments' => $this->insertPngXmpSegment($segments, $this->createPngXmpSegment($metadata))
8886
]);
8987
}
9088

@@ -98,6 +96,22 @@ public function execute(FileInterface $file, MetadataInterface $metadata): FileI
9896
]);
9997
}
10098

99+
/**
100+
* Insert XMP segment to image png segments before IEND chunk
101+
*
102+
* @param SegmentInterface[] $segments
103+
* @param SegmentInterface $xmpSegment
104+
* @return SegmentInterface[]
105+
*/
106+
private function insertPngXmpSegment(array $segments, SegmentInterface $xmpSegment): array
107+
{
108+
return array_merge(
109+
array_slice($segments, 0, count($segments) - 1),
110+
[$xmpSegment],
111+
array_slice($segments, count($segments) - 1)
112+
);
113+
}
114+
101115
/**
102116
* Write new png segment metadata
103117
*

0 commit comments

Comments
 (0)