Skip to content

Commit 6cb8862

Browse files
committed
update clearMetadata()
1 parent 4558547 commit 6cb8862

File tree

8 files changed

+18
-129
lines changed

8 files changed

+18
-129
lines changed

system/Images/Handlers/BaseHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,8 @@ public function getHeight()
777777
* since all the data are cleared automatically.
778778
*
779779
* GDHandler can't preserve the image metadata.
780-
*
781-
* @param array<int|string, array<int, string>|string> $data
782780
*/
783-
public function clearMetadata(array $data = []): static
781+
public function clearMetadata(): static
784782
{
785783
return $this;
786784
}

system/Images/Handlers/ImageMagickHandler.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -539,48 +539,17 @@ public function reorient(bool $silent = false)
539539
}
540540

541541
/**
542-
* Clears metadata from the image based on specified parameters.
543-
*
544-
* Configuration for metadata clearing:
545-
* - If empty, all metadata is stripped
546-
* - If contains 'except' key, keeps only those properties
547-
* - Otherwise, deletes only the specified keys
548-
*
549-
* @param array<int|string, array<int, string>|string> $data
542+
* Clears metadata from the image.
550543
*
551544
* @return $this
552545
*
553546
* @throws ImagickException
554547
*/
555-
public function clearMetadata(array $data = []): static
548+
public function clearMetadata(): static
556549
{
557550
$this->ensureResource();
558551

559-
// Strip all metadata when no parameters are provided
560-
if ($data === []) {
561-
$this->resource->stripImage();
562-
563-
return $this;
564-
}
565-
566-
// Keep only properties specified in 'except' array
567-
if (isset($data['except'])) {
568-
$propertiesToKeep = (array) $data['except'];
569-
$allPropertyNames = $this->resource->getImageProperties('*', false);
570-
571-
foreach ($allPropertyNames as $property) {
572-
if (! in_array($property, $propertiesToKeep, true)) {
573-
$this->resource->deleteImageProperty($property);
574-
}
575-
}
576-
577-
return $this;
578-
}
579-
580-
// Delete only specific properties
581-
foreach ($data as $property) {
582-
$this->resource->deleteImageProperty($property);
583-
}
552+
$this->resource->stripImage();
584553

585554
return $this;
586555
}

tests/system/Images/ImageMagickHandlerTest.php

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -459,72 +459,24 @@ public function testClearMetadataReturnsSelf(): void
459459
$this->assertSame($this->handler, $result);
460460
}
461461

462-
public function testClearMetadataAll(): void
462+
public function testClearMetadata(): void
463463
{
464-
$this->handler->withFile($this->path);
464+
$this->handler->withFile($this->origin . 'Steveston_dusk.JPG');
465465
/** @var Imagick $imagick */
466466
$imagick = $this->handler->getResource();
467467
$before = $imagick->getImageProperties();
468-
$this->assertGreaterThan(10, $before);
469-
470-
$this->handler
471-
->clearMetadata()
472-
->save($this->root . 'ci-logo-no-metadata.png');
473-
474-
$this->handler->withFile($this->root . 'ci-logo-no-metadata.png');
475-
/** @var Imagick $imagick */
476-
$imagick = $this->handler->getResource();
477-
$after = $imagick->getImageProperties();
478468

479-
$this->assertLessThan(10, count($after));
480-
}
481-
482-
public function testClearMetadataExcept(): void
483-
{
484-
$this->handler->withFile($this->path);
485-
/** @var Imagick $imagick */
486-
$imagick = $this->handler->getResource();
487-
$before = $imagick->getImageProperties();
488-
$this->assertArrayHasKey('png:gAMA', $before);
469+
$this->assertCount(44, $before);
489470

490-
// Keep 2 properties
491471
$this->handler
492-
->clearMetadata(['except' => ['png:bKGD', 'png:cHRM']])
493-
->save($this->root . 'ci-logo-no-metadata.png');
494-
495-
$this->handler->withFile($this->root . 'ci-logo-no-metadata.png');
496-
/** @var Imagick $imagick */
497-
$imagick = $this->handler->getResource();
498-
$after = $imagick->getImageProperties();
499-
500-
$this->assertArrayHasKey('png:bKGD', $after);
501-
$this->assertArrayHasKey('png:cHRM', $after);
502-
$this->assertArrayNotHasKey('png:gAMA', $after);
503-
}
504-
505-
public function testClearMetadataSpecific(): void
506-
{
507-
$this->handler->withFile($this->path);
508-
/** @var Imagick $imagick */
509-
$imagick = $this->handler->getResource();
510-
$before = $imagick->getImageProperties();
511-
512-
$this->assertArrayHasKey('png:bKGD', $before);
513-
$this->assertArrayHasKey('png:cHRM', $before);
514-
$this->assertArrayHasKey('png:gAMA', $before);
515-
516-
// Delete only 1
517-
$this->handler
518-
->clearMetadata(['png:gAMA'])
519-
->save($this->root . 'ci-logo-no-metadata.png');
472+
->clearMetadata()
473+
->save($this->root . 'exif-info-no-metadata.jpg');
520474

521-
$this->handler->withFile($this->root . 'ci-logo-no-metadata.png');
475+
$this->handler->withFile($this->root . 'exif-info-no-metadata.jpg');
522476
/** @var Imagick $imagick */
523477
$imagick = $this->handler->getResource();
524478
$after = $imagick->getImageProperties();
525479

526-
$this->assertArrayHasKey('png:bKGD', $after);
527-
$this->assertArrayHasKey('png:cHRM', $after);
528-
$this->assertArrayNotHasKey('png:gAMA', $after);
480+
$this->assertCount(5, $after);
529481
}
530482
}

user_guide_src/source/libraries/images.rst

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,14 @@ The possible options that are recognized are as follows:
264264
Clearing Image Metadata
265265
=======================
266266

267-
This method provides control over which metadata is preserved or removed from an image.
267+
This method removes metadata (EXIF, XMP, ICC, IPTC, comments, etc.) from an image.
268268

269269
.. important:: The GD image library automatically strips all metadata during processing,
270270
so this method has no additional effect when using the GD handler.
271271
This behavior is built into GD itself and cannot be modified.
272272

273-
.. note:: Some essential technical metadata (dimensions, color depth) will be regenerated
274-
during save operations as they're required for image display. However, all privacy-sensitive
275-
information such as GPS location, camera details, and timestamps can be completely removed.
276-
277-
The method supports three different operations depending on the provided parameters:
278-
279-
**Clear all metadata** - When an empty array is passed, all metadata is stripped from the image.
273+
Some essential technical metadata (dimensions, color depth) will be regenerated during save operations
274+
as they're required for image display. However, all privacy-sensitive information such as GPS location,
275+
camera details, and timestamps will be completely removed.
280276

281277
.. literalinclude:: images/015.php
282-
283-
**Keep only specific properties** - When using the 'except' key, only the specified properties are preserved.
284-
285-
.. literalinclude:: images/016.php
286-
287-
**Delete specific properties** - When providing a list of property names, only those properties are removed.
288-
289-
.. literalinclude:: images/017.php

user_guide_src/source/libraries/images/016.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

user_guide_src/source/libraries/images/017.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3259 errors
1+
# total 3255 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/varTag.type.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# total 8 errors
1+
# total 4 errors
22

33
parameters:
44
ignoreErrors:
55
-
66
message: '#^PHPDoc tag @var with type Imagick is not subtype of type resource\.$#'
7-
count: 6
7+
count: 2
88
path: ../../tests/system/Images/ImageMagickHandlerTest.php
99

1010
-

0 commit comments

Comments
 (0)