PHP Imagick转PNG为DDS缺失Cluster-fit与Mipmaps设置参数求助
Great question! The good news is PHP Imagick does support these DDS-specific parameters—they just aren't exposed via dedicated methods like setImageCompression. Instead, you use the setOption() method to pass the same dds:-prefixed definitions you'd use with the convert command.
Here's how to modify your original code to include cluster-fit and mipmap settings without relying on exec():
<?php // Load the image (can use a file path or even an in-memory stream) $img = new Imagick('test.png'); // Set output format to DDS $img->setFormat('dds'); // Set compression method $img->setImageCompression(Imagick::COMPRESSION_DXT5); // Add DDS-specific options $img->setOption('dds:mipmaps', '6'); // Number of mipmaps to generate $img->setOption('dds:cluster-fit', 'true'); // Enable cluster-fit optimization // Save the final DDS file $img->writeImage('test.dds'); // Clean up resources $img->destroy(); ?>
Why this works:
Those dds:-style definitions are ImageMagick's format-specific configuration options. In the command line, you pass them with -define, but in PHP Imagick, the setOption() method is the equivalent way to inject these settings directly into the image processing pipeline.
This approach lets you work entirely in memory (no need to save intermediate PNG files to disk first) just like your initial Imagick code attempt—no more relying on exec() for those extra parameters.
Quick note:
Make sure your underlying ImageMagick library version (not just the PHP Imagick extension) is up to date. These DDS options are supported in ImageMagick 6.8 and later; older versions might not recognize them.
内容的提问来源于stack exchange,提问作者Bourns

