为什么WordPress中带水印的图片会变大这么多?

问题描述 投票:0回答:1

我使用 WordPress 中的functions.php 文件中的代码作为带水印的图像。我的问题是带水印的图像变得太大。如果原始图像1MB,则水印图像为13MB。发生了什么?为什么带水印的图像尺寸会增大这么多?这是我使用的代码:

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' );
php wordpress watermark
1个回答
0
投票

好吧,你正在添加到图像中,所以它会更大

© www.soinside.com 2019 - 2024. All rights reserved.