@@ -443,4 +443,92 @@ public function testImageReorientPortrait(): void
443443 $ this ->assertSame (['red ' => 62 , 'green ' => 62 , 'blue ' => 62 , 'alpha ' => 0 ], $ rgb );
444444 }
445445 }
446+
447+ public function testClearMetadataEnsuresResource (): void
448+ {
449+ $ this ->expectException (ImageException::class);
450+ $ this ->handler ->clearMetadata ();
451+ }
452+
453+ public function testClearMetadataReturnsSelf (): void
454+ {
455+ $ this ->handler ->withFile ($ this ->path );
456+
457+ $ result = $ this ->handler ->clearMetadata ();
458+
459+ $ this ->assertSame ($ this ->handler , $ result );
460+ }
461+
462+ public function testClearMetadataAll (): void
463+ {
464+ $ this ->handler ->withFile ($ this ->path );
465+ /** @var Imagick $imagick */
466+ $ imagick = $ this ->handler ->getResource ();
467+ $ before = $ imagick ->getImageProperties ();
468+ $ this ->assertCount (14 , $ 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 ();
478+
479+ $ this ->assertCount (9 , $ 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 ->assertCount (14 , $ before );
489+
490+ // Keep 2 properties
491+ $ 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+ $ this ->assertCount (12 , $ after );
505+ }
506+
507+ public function testClearMetadataSpecific (): void
508+ {
509+ $ this ->handler ->withFile ($ this ->path );
510+ /** @var Imagick $imagick */
511+ $ imagick = $ this ->handler ->getResource ();
512+ $ before = $ imagick ->getImageProperties ();
513+
514+ $ this ->assertArrayNotHasKey ('png:tIME ' , $ before );
515+ $ this ->assertCount (14 , $ before );
516+
517+ // Delete only 1
518+ $ this ->handler
519+ ->clearMetadata (['png:gAMA ' ])
520+ ->save ($ this ->root . 'ci-logo-no-metadata.png ' );
521+
522+ $ this ->handler ->withFile ($ this ->root . 'ci-logo-no-metadata.png ' );
523+ /** @var Imagick $imagick */
524+ $ imagick = $ this ->handler ->getResource ();
525+ $ after = $ imagick ->getImageProperties ();
526+
527+ $ this ->assertArrayHasKey ('png:bKGD ' , $ after );
528+ $ this ->assertArrayHasKey ('png:cHRM ' , $ after );
529+ $ this ->assertArrayHasKey ('png:tIME ' , $ after );
530+ $ this ->assertArrayNotHasKey ('png:gAMA ' , $ after );
531+
532+ $ this ->assertCount (14 , $ after );
533+ }
446534}
0 commit comments