composer require meyfa/php-sv
/** * SVG图片转PNG图片 * @param $svgFilePath * @param $pngFilePath * @param $width * @param $height * @return void * @throws BusinessException */ function svgToPng($svgFilePath, $pngFilePath, $width = 200, $height = 200) { // 检查文件 if (!is_file($svgFilePath)) { throw new BusinessException('文件地址不存在'); } // 加载文件 $image = SVG::fromFile($svgFilePath); // 设置图片宽高 $rasterImage = $image->toRasterImage($width, $height); // 保存文件 imagepng($rasterImage, $pngFilePath);
svgToPng('./1.svg','777.png')
(4)、质量最高的方式是下面的方式
// 创建 Imagick 实例 $imagick = new Imagick(); // 读取 SVG 文件 $imagick->readImage('input.svg'); // 设置图像分辨率(DPI)以提高输出质量 $imagick->setResolution(300, 300); // 将图像格式转换为 PNG $imagick->setImageFormat('png'); // 保存为 PNG 文件 $imagick->writeImage('output.png'); // 释放资源 $imagick->clear(); $imagick->destroy();
安装扩展只需要将php dll放在ext 其他的dll扔在php目录就行