我试图在PHP中使用imagemagick调整图像大小。当源图像大于调整大小时,一切正常,但当源图像小于调整大小时,它不能正常工作。然后图像需要得到一些填充
例如: 如果源图像是200px x 200px,脚本会放大图像中的$ smallphoto'至300px x 300px,但我希望源图像保持200px x 200px并添加填充以使其为300px x 300px。源图像永远不会大于它的原始大小,但是' $ smallphoto'总是需要300px x 300px。
有谁知道解决问题的最佳方法?
$imageSource = 'test.jpg';
$newImage = new Imagick($imageSource);
// Remove whitespace around image
$newImage->trimImage(0.04 * \Imagick::getQuantum());
// Get width/height
$newImageWidth = $newImage->getImageWidth();
$newImageHeight = $newImage->getImageHeight();
// Check for longest side
$newImageSize = ($newImageWidth > $newImageHeight) ? $newImageWidth : $newImageHeight;
// Set quality for image
$newImage->setImageCompressionQuality(80);
// Set white background
$newImage->setImageBackgroundColor('#ffffff');
// Make the square and set the image in the center
$newImage->extentImage($newImageSize, $newImageSize, ($newImageWidth - $newImageSize) / 2, ($newImageHeight - $newImageSize) / 2);
// Save image
$newImage->writeImage('test-new.jpg');
// Create smaller image
$smallPhoto = new Imagick('test-new.jpg');
$smallPhoto->resizeImage(300, 300, imagick::FILTER_LANCZOS, 0.9, true);
$smallPhoto->writeImage('s-test-news.jpg');