【问题标题】:Convert SVG to PNG image将 SVG 转换为 PNG 图像
【发布时间】:2016-10-07 12:27:44
【问题描述】:

我尝试将 svg 转换为 png 图像。我用过下面的php代码。

$image = new Imagick();
$image->readImageBlob(file_get_contents('image.svg'));
$image->setImageFormat("png24");
$image->resizeImage(1024, 768, imagick::FILTER_LANCZOS, 1); 
$image->writeImage('image.png');

发现以下错误

Fatal error: Uncaught exception 'ImagickException' with message 'WriteBlob Failed `image.png' @ error/png.c/MagickPNGErrorHandler/1645' in /var/www/html/image.php:8 Stack trace: #0 /var/www/html/image.php(8): Imagick->writeimage('image.png') #1 {main} thrown in /var/www/html/image.php on line 8

我做错了什么?

【问题讨论】:

  • 你搞清楚了吗?我的意思是你解决了吗?还需要帮助吗?
  • 不,仍然发生错误
  • 你在上传图片吗?还是它们已经存在?
  • 已经存在,我需要一个帮助,如何为所有g标签设置背景
  • 大声笑一一。让我们解决第一个问题。

标签: php


【解决方案1】:

可能的解决方案:

检查路径是否正确,'image.png'是否有文件写入权限

this question

使用 writeImageFile 代替 writeImage

this post

【讨论】:

    【解决方案2】:

    所以我问你imgs是否已经存在或者你正在上传它们,你说它们已经存在了,但我们将努力让你上传,然后转换它们。

    首先像往常一样上传图片:

    <form action="test.php" method="post">
        <input type="file" name="img">
        <input type="submit" name="submit">
    </form> 
    

    其次,创建2个文件夹,一个用于主图像,第二个用于转换后的图像。

    • 第一个文件夹名称是“original”
    • 第二个文件夹名称是“converted”

    现在在与 html 页面相同的文件夹中创建一个名为 test.php 的页面。

    这里我们将把原始图像上传到第一个文件夹,然后调用它并转换它并将新图像存储在第二个文件夹中。

    <?php
    
        if(isset($_POST['submit']))
        {
    
            $filename = $_FILES['file']['name']; // getting the name of the uploaded img 
            $file_loc = $_FILES['file']['tmp_name'];
    
            $folder1="original/"; 
            $folder2="converted/";
    
            if(move_uploaded_file($file_loc,$folder1.$filename)) // moving the uploaded or the original img to the folder 1 
                {
                    rename($folder1.$filename,$folder1.$filename.".SVG"); // adding SVG ext to the file name 
                    $newext =  substr($filename,0, -3);  // taking the .SVG string from the file name 
                    imagepng(imagecreatefromstring(file_get_contents($folder1.$filename)),$folder2.$newext."png"); // Bring the file from folder 1 and add .png to its file name and then moving it to folder 2
    
                }
            else
            {
                echo "Something wrong with moving the file ";
            }
        else
        {
            echo "submit did not done ";
        }
    ?>
    

    【讨论】:

    • 我试图给你投反对票,但你很幸运,因为我的分数很低。
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2022-07-05
    • 2011-06-16
    • 2017-12-21
    • 2017-05-11
    相关资源
    最近更新 更多