0

I have tried to convert the svg to png image . I have used below php code.

$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');

The below error was found

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

What am I doing wrong?

Abela
  • 1,225
  • 3
  • 19
  • 42
Shanmuga kumar
  • 165
  • 1
  • 4
  • 12

2 Answers2

0

So i asked you if the imgs already exist or you are uploading them , you said they are already exist , but we are going to work on the idea that you are uploading the, then converting them .

First just like always a form to upload an image :

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

Second , create 2 folders , one for the main images and the second for the converted images .

  • First folder name is " original "
  • Second folder name is " converted "

Now create a page called test.php in the same folder as the html page .

Here we are going to take the original images and upload it to the first folder and then call it back and convert it and store the new image in the second folder .

<?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 ";
    }
?>
Laith
  • 428
  • 4
  • 10
-1

Possible solutions:

Check whether the path is right and that there are file writing permissions for the 'image.png'

See this question.

Use writeImageFile instead of writeImage

See this post.

Community
  • 1
  • 1
BVengerov
  • 2,947
  • 1
  • 18
  • 32