Search code examples
phpwordpresswatermark

Why does the watermarked image increase in size so much in WordPress?


I use a code in functions.php file in WordPress for the watermarked image. My problem is the watermarked image become too large. If the original image 1 MB, the watermarked image is 13 MB. What's happening? Why does the watermarked image increase in size so much? This is the code what I use:

function add_watermark_to_image( $attachment_id ) {
    $image_sizes = get_intermediate_image_sizes();
    $watermark_attachment_id = YOUR_ATTACHMENT_ID; // Replace with your watermark attachment ID

   foreach ( $image_sizes as $size ) {
        $image_path     = get_attached_file( $attachment_id );
        $watermark_path = get_attached_file( $watermark_attachment_id );

        if ( $image_path && file_exists( $image_path ) && $watermark_path && file_exists( $watermark_path ) ) {
            $image     = imagecreatefromstring( file_get_contents( $image_path ) );
            $watermark = imagecreatefrompng( $watermark_path );

            if ( $image && $watermark ) {
                list( $image_width, $image_height )         = getimagesize( $image_path );
                list( $watermark_width, $watermark_height ) = getimagesize( $watermark_path );

                // Set the position of the watermark (bottom right)
                $dest_x = $image_width - $watermark_width;
                $dest_y = $image_height - $watermark_height;
                imagecopy( $image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height );

                // Save the watermarked image
                imagepng( $image, $image_path );
                imagedestroy( $image );
                imagedestroy( $watermark );
            } else {
                error_log( 'Image or watermark creation failed for size: ' . $size );
            }
        } else {
            error_log( 'Image or watermark not found for size: ' . $size );
        }
    }
}
add_action( 'add_attachment', 'add_watermark_to_image' );

Solution

  • As we said in the comments, you save your final result as png file which is a compressed lossless format to store images. This may cause your result file to have a (much) bigger size !

    You can use the imagejpeg() function to save your result as a jpg file. You can add a quality parameter which will change the compression ratio. This can significantly reduce the file size but will affect the quality of the image.

    // [...]
    $image = imagecreatefromstring(file_get_contents($image_path));
    $watermark = imagecreatefrompng($watermark_path);
    if ($image && $watermark) {
        list($image_width, $image_height) = getimagesize($image_path);
        list($watermark_width, $watermark_height) = getimagesize($watermark_path);
    
        // Set the position of the watermark (bottom right)
        $dest_x = $image_width - $watermark_width;
        $dest_y = $image_height - $watermark_height;
        imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
    
        // Save the watermarked image
        imagejpeg($image, $image_path, 80); // 80 is the quality (0-100) greater quality will increase the file size.
        imagedestroy($image);
        imagedestroy($watermark);
    } else {
        error_log('Image or watermark creation failed for size: ' . $size);
    }
    // [...]