Convert svg to png in PHP

Asked

Viewed 280 times

0

I have been researching and testing implementations allocated in repositories, however I could not apply to my case. I would like to know if there is any native way to do this, considering that the svg file is stored in a folder of my server. I tried to use Imagick as follows:

    <?php
    $im = new Imagick();
    try {
      $im->setBackgroundColor(new ImagickPixel("transparent"));
       $svg = '<svg aria-hidden="true" data-prefix="fas" data-icon="address-book" class="svg-inline--fa fa-address-book fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M436 160c6.6 0 12-5.4 12-12v-40c0-6.6-5.4-12-12-12h-20V48c0-26.5-21.5-48-48-48H48C21.5 0 0 21.5 0 48v416c0 26.5 21.5 48 48 48h320c26.5 0 48-21.5 48-48v-48h20c6.6 0 12-5.4 12-12v-40c0-6.6-5.4-12-12-12h-20v-64h20c6.6 0 12-5.4 12-12v-40c0-6.6-5.4-12-12-12h-20v-64h20zm-228-32c35.3 0 64 28.7 64 64s-28.7 64-64 64-64-28.7-64-64 28.7-64 64-64zm112 236.8c0 10.6-10 19.2-22.4 19.2H118.4C106 384 96 375.4 96 364.8v-19.2c0-31.8 30.1-57.6 67.2-57.6h5c12.3 5.1 25.7 8 39.8 8s27.6-2.9 39.8-8h5c37.1 0 67.2 25.8 67.2 57.6v19.2z"></path></svg>';
      $im->readImageBlob($svg);
      $im->setImageFormat("png24");
      header("Content-Type: image/png");
      echo $im;
      $im->writeImage('convert.png');
    }
    catch (ImagickException $e) {
      echo $e;
    }
    $im->clear();
    $im->destroy();
?>

Could someone help me identify the mistake in this situation?

  • Have you tried using the Imagick? It is an extension that uses the Image Magick

  • I have been testing some examples of repositories that use this Imagick, but have not succeeded. ?>`

  • Did you run this code that you posted? If so, did you print any errors?

  • I ran straight from the server and returns nothing.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.