当前位置:首页 > PHP > 正文内容

使用php将svg图片转换为png图片

高老师2年前 (2024-01-11)PHP441
(1)、安装composer包
composer require meyfa/php-sv
(2)、包装下方法
/**
 * 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);
(3)、调用测试
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目录就行





扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/699.html

分享给朋友:

“使用php将svg图片转换为png图片” 的相关文章

php数组合并 array_merge和+号的区别

php数组合并 array_merge和+号的区别

array_merge是最常用的数组合并方法,+号同样也可以,但是却有很大不同。array_merge遇到相同字符串key,后面数组的key会覆盖前面数组的key,+号正好相反。$a = [ 'one' => 'A on...

 php监听事件,php触发事件

php监听事件,php触发事件

逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event {     /** &nbs...

php finally使用

php finally使用

<?php /**  * @throws Exception  */ function curl() {     throw  new \Exception('err...

php执行慢原因查找

php执行慢原因查找

今天帮朋友查询wordpress执行超级慢的原因,特此记录开启fpm的慢日志,记录执行超过30秒的脚本request_slowlog_timeout = 30 slowlog = var/log/slow.log查看日志[23-May-2019 17...

PHP Warning:  ftok(): Project identifier is invalid

PHP Warning: ftok(): Project identifier is invalid

在使用ftok生成ipc进程通信key尝试将第二个参数项目标识符传入字符串报错:PHP Warning:  ftok(): Project identifier is invalid,查阅资料发现第二个字符串只能是1个字符串,长度为1....

php mcrypt扩展被废弃的解决方案

php mcrypt扩展被废弃的解决方案

使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...