[php]SVG to PNG

參考網址:

安裝 imagick 套件 https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-install-imagemagick-for-php-on-ubuntu-18-04/#google_vignette

apt install imagemagick
apt list imagemagick -a

特定版本
apt install imagemagick:6.9.7.4


Installing Imagick PHP Extension
apt install php-imagick
systemctl restart apache2

https://www.phpclasses.org/
 

<?php
// Load the SVG file
$svgFilePath = 'path/input.svg';
$svgContent = file_get_contents($svgFilePath);

// Create an Imagick object and set the SVG content
$imagick = new Imagick();
$imagick->readImageBlob($svgContent);

// Set the image format for the output (e.g., PNG, JPEG)
$outputFormat = 'png'; // You can change this to your desired image format

// Optionally, you can adjust the image properties as needed
$imagick->setImageFormat($outputFormat);
$imagick->setImageBackgroundColor(new ImagickPixel('white')); // Set background color if needed
$imagick->setResolution(300, 300); // Set resolution (dots per inch)

// Perform the conversion
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); // Remove alpha channel if present
$imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); // Flatten the image

// Save the converted image to a file
$outputFilePath = 'path/output.' . $outputFormat;
$imagick->writeImage($outputFilePath);

// Clean up resources
$imagick->clear();
$imagick->destroy();

echo "Conversion completed. The image has been saved to: $outputFilePath";
?>

 

本篇發表於 程式設計。將永久鏈結加入書籤。