Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write a code that converts SVG script to PNG image. I met with two problems:

All the images nodes in the SVG script - their width is stretch. I want to define only the height, and the width leave to auto. In the SVG itself it seem good.
I tried to embed curved text - but in the PNG output I didn't see it.
This is my SVG script:

HTML
<svg  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="350px" height="100px" viewBox="0 0 350 100">
  <defs>
    <path d="M 0,0 a 142.5,142.5 0 1,1 285,0 a 142.5,142.5 0 1,1 -285,0" id="curvedTextPath0" transform="translate(30,162)"></path>
  </defs>
  <image height="100px" width="350px" xlink:href="https://concepta.co.il/wp-content/uploads/2020/02/7-300x208.jpg" x="0px" y="0px" visibility="visible" preserveAspectRatio="xMidYMid meet"></image>
  <text fill="#2f299a" text-anchor="middle" font-size="25px" x="30" y="20" visibility="visible" font-family="M PLUS Rounded 1c"> 
    <textPath startOffset="22%" xlink:href="#curvedTextPath0">lehcar</textPath>
  </text>
</svg>

This is my PHP code:

PHP
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));   
//$im->setResolution(300, 300); // for 300 DPI example
$im->readImageBlob($svg);

/*png settings*/
$im->setImageFormat("png32");

$folder_path = WP_CONTENT_DIR. 'uploads/concepta_orders_files';
if (!file_exists($folder_path)) {
    mkdir($folder_path, 0777, true);
}
$img_path = $folder_path . '/' . $fileName; /*(.jpg or .png)*/
$im->writeImage($img_path);
$im->clear();
$im->destroy();

Thanks for advance!

What I have tried:

I tried to validate the svg code - is valid.
I tried to set the property
preserveAspectRatio="xMidYMid meet"
to the images - id didn't help.
I also changed the image format.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900