From 1234567890abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
From: Your Name <anastasisgiabanis@gmail.com>
Date: Mon, 28 Jul 2025 20:32:00 +0000
Subject: [PATCH] Fix: Replace writeImage() with getImageBlob() + file_put_contents() to prevent duplicate inotify events

Fixes duplicate CLOSE_WRITE,CLOSE events during thumbnail generation
by replacing ImageMagick's writeImage() method with a combination of
getImageBlob() and file_put_contents() for single file operation.

This eliminates the internal buffering that causes two file operations:
1. Write data to buffer
2. Flush buffer to file

Benefits:
- Eliminates duplicate inotify events
- Reduces file I/O operations
- Improves performance for thumbnail generation
- Maintains full compatibility with all ImageMagick versions

Props: [agiabanis]
Fixes: #[63764]

---
 wp-includes/class-wp-image-editor-imagick.php | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php
index abc1234..def5678 100644
--- a/wp-includes/class-wp-image-editor-imagick.php
+++ b/wp-includes/class-wp-image-editor-imagick.php
@@ -972,7 +972,11 @@ private function write_image( $image, $filename ) {
 			}
 
 			try {
-				return $image->writeImage( $filename );
+				$image_data = $image->getImageBlob();
+				if (file_put_contents($filename, $image_data) === false) {
+					return new WP_Error( 'image_save_error', 'Failed to write image data to file', $filename );
+				}
+				return true;
 			} catch ( Exception $e ) {
 				return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
 			}
-- 
2.40.0 