John Davidson

svg - calling inkscape cli from php with shell_exec, return different value than running directly from cmd

0 comments
Message:


I need to get height of a text from SVG files (id : textmaster) with php.


When running inkscape with shell_exec, the return value is different than running it directly from cmd :


<?php
$heightpx = shell_exec('inkscape --query-id="textmaster" --query-height drawing_edit.svg');
echo $heightpx;
?>

the return value was :


74.5301

But when run directly from cmd :


inkscape --query-id="textmaster" --query-height drawing_edit.svg

the return value was :


128.778

the svg files :


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1"
id="svg5"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs2" />
<g
id="layer1">
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:25.4773px;line-height:1.25;font-family:Austria;-inkscape-font-specification:Austria;stroke-width:0.367463"
x="20.467245"
y="47.263172"
id="textmaster"><tspan
id="tspan7489"
x="20.467245"
y="47.263172"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:Austria;-inkscape-font-specification:Austria;stroke-width:0.367463">Hello World!</tspan></text>
</g>
</svg>

The right value was when it run directly from cmd prompt.
How to make the shell_exec called from php return the same values?



My Answer:
There could be several reasons why the return value is different when running Inkscape with shell_exec in PHP compared to running it directly from the command line.

1. Environment variables: When running a command from PHP using shell_exec, the environment variables may be different than when running the command directly from the command line. This can affect the behavior of the command and the return value.

2. Permissions: The user running the PHP script may not have the necessary permissions to execute the Inkscape command, which can result in a different return value.

3. Path issues: The path to the Inkscape executable may not be set correctly in the PHP environment, leading to a different return value.

To troubleshoot this issue, you can try the following:

1. Check the environment variables in PHP by running `print_r($_ENV);` in your PHP script to see if there are any differences compared to the command line environment.

2. Check the permissions of the Inkscape executable and make sure that the user running the PHP script has permission to execute it.

3. Use the full path to the Inkscape executable in your shell_exec command to ensure that the correct executable is being called.

By investigating these potential issues, you may be able to determine the cause of the different return values and find a solution to get the height of a text from SVG files accurately in PHP.

Rate this post

5 of 5 based on 1116 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us