on server side I have an generated SVG XML source code.
This should be changed to an image in order to offer a PNG (or JPG) download from an SVG XML code.
Searching the web for a long time, I only found this solution using ImageMagick.
Convert SVG image to PNG with PHP
But I don't have access to the ImageMagick library so I need a different way to convert SVG XML Code to a bitmap image.
Does anybody have an idea?
Brw: It's not an option to save that svg an execute an binary or script on operating system to convert.
Thank you.
You need to use batik library. Download it, placed somewhere in your project. Then in php call the batik command using shell_exec() function. It will take few seconds and convert you svg to png.
Example:-
outputfile ='path where you want to lace png'
$tempSVG_filename = '/var/www' . $baseUrl . '/png/temp.svg';
$tempSVG_handle = fopen($tempSVG_filename, 'w+');
fwrite($tempSVG_handle, $YourSVG);
fclose($tempSVG_handle);
$mimetype = 'image/png';
$width = '6000';
$result = shell_exec('java -jar /var/www/svgtopng/batik-1.7/batik-rasterizer.jar -m ' . $mimetype . ' -d ' . $outputfile . ' -w ' . $width . ' ' . $tempSVG_filename . ' 2>&1');
unlink($tempSVG_filename);
Related
Okay, so the problem is essentially: convert an svg to a pdf using image magick.
I take an image in a base64 format, do something to it, convert it to a bitmap, do something to is, store it inside an SVG string, write the svg to a file (doesn't have to be done, but wanted to make sure the svg was loading), then from here I need to convert the svg to a pdf.
//I just broke the string up over spaces for sake of readability
$svgStr = "<?xml version=1.0.' encoding='utf-8' standalone='no'?>
<svg xmlns="http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve'>
<desc>
Made by Shane
</desc>
<defs>
</defs>
<style>
</style>
<image width='500' height'auto' xlink:href'" . $path . $filename . $ext . "' />
</svg>";
file_put_contents($path . $svgFile . ".svg", $svgStr);
//Attempt 1 of many
$pdf = New Imagick($path . $svgFile . ".svg", "SVG");
$pdf->setImageFormat("PDF"); //This thows the "Can not process empty imagick object" error.
file_put_contents($path . $svgFile . ".pdf", $pdf);
//Attempt2 of many
$pdf = New Imagick();
//Throws an error here - can not process empty Imagick object...
//which brings me back to attempt 1... the idea in attempt 1 is to initialize
//the Imagick object with the svg format and data, then convert.
//Here the idea is to initialize it step by step
$pdf->setImageFormat("SVG");
//For readImageBlob(), I should be able to read the svg file or use the $svgStr.
$pdf->readImageBlob($svgStr);
$pdf->setImageFormat("PDF");
file_put_contents($path . $svgFile . ".pdf", $pdf);
I am running image magick 6.8.9-9.
I can't upgrade, because I don't have sudo and it can't be accessed is the long story short.
EDIT:
I think this might be part of the problem - I get an error converting a .bmp to a pdf from the command line:
https://askubuntu.com/questions/1081695/error-during-converting-jpg-to-pdf
However, I don't know if the command line utilities operate in the same manner as the class wrapper does...
Please help 0_0
Thank you.
I have audio files on my server and wanted to convert them into flac format in order to convert them into text. Please let me know how can we achieve that..
You can use FFmpeg:
https://ffmpeg.org/
ffmpeg -i input.mp3 output.flac
There is a php wrapper for the ffmpeg binary on github.
https://github.com/PHP-FFMpeg/PHP-FFMpeg
In case you can't install ffmpeg you might want to try this free API:
<?php
$url = 'http://server.com/sound.mp3';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/sound_convert.php?url=' . $url . '&format=flac'));
if (#$data->success !== 1)
{
die('Failed');
}
$flac = file_get_contents($data->file);
file_put_contents('sound.flac', $flac);
Has anybody had any experience with generating 2D Barcodes for Royal Mail via PHP? I've spent a while attempting to get my own routines to write a valid datamatrix sadly to no avail.
I do have working conversion routines for ASCII to C40 and Luhn 16 checksum makers but just can't get anywhere with the graphical representation, or the ECC200 byte creation for that matter.
Are there any pre-written libraries out there with documentation that would help take away a lot of further legwork?
I do need to be able to generate this within the server environment, without using external sites ofr image generation ideally.
We use Zint Barcode Generator Unix packages for QR and PDF417 code generation. Royal Mail is supported as well.
(on CentOS dnf install zint, Ubuntu takes more work).
Zint documentation: http://www.zint.org.uk/
In PHP use the system method, example:
$targetFilePath = dirname(__FILE__).'/test.png';
$contents = 'ABC123';
system('zint ...params... -o"' . $targetFilePath . '" -d"' . $contents . '"');
var_dump(file_exists($targetFilePath));
It will generate an image on the requested $targetFilePath.
For ECC200 Datamatrix Generation in PHP we successfully used:
sudo apt install dmtx-utils
To output a PNG file from the server, with normal apache2 settings you would get
the barcode in PNG when you enter in the browser: http://yourserver.com/datamatrix/?in=yourbarcodetext
<?php
ob_start();
$old_path = getcwd();
$infile = "/var/www/html/datamatrix/message2.txt";
$image = "/var/www/html/datamatrix/image.png";
file_put_contents($infile,$_GET["in"]);
$ex = "export HOME=/tmp && /usr/bin/dmtxwrite {$infile} -o {$image}";
echo "<b>$ex</b>";
$output = shell_exec($ex);
echo var_export($output, TRUE);
echo "done";
chdir($old_path);
$im = imagecreatefrompng($image);
ob_end_clean();
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
here I have to cache about 2000 favicon.ico files for performance enhancements. I grab the files and try to shrink them via IMagick (v.6.6.0) and PHP 5.3.5
The PHP code for this is
try {
$image = new Imagick($im_hint . ':' . BASE . '/upload/favicon.ico');
$image->cropThumbnailImage(16, 16);
$image->setImageFormat('ico');
$image->writeImage(BASE . '/favicons/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id . '.ico');
} catch (Exception $e) { die($e->getMessage()); }
where $im_hint could be ico, png, jpg and so on.
For 99% of the files all is fine and I get a working ICO file. But for one percent of files, I get only a blank ICO file and I don't know why? An example for an ICO file where this code fails is http://www.augensound.de/favicon.ico
I tried to comment out the cropThumbnailImage call and try to use setFormat instead of setImageFormat and tried to save it as PNG...but nothing works. There is also no exception.
Regards
Not an answer to your question, but I get a blank image too when I open the example file in IrfanView or PhotoImpact. It's not a multi-page/multi-resolution file so there is nothing to switch. The canvas just is blank.
Windows 7's built-in preview renders it fine, though.
It could be that IM can't deal with these files because they have the wrong format or sub-format.
Im trying to create vector graphics in PHP. Ive tried Cairo and I havn't been able to get it to work. I understand that imageMagick has vector functionality but the documentation on php.net is very poor can some one lead me in the right direction? The ideas is to be able to save the graphic to EPS. I also need to be able to use different fonts to output text.
Although you're looking to create eps I would still aim to create a PDF. PDF's are fully editable in any major package: Adobe Illustrator, Corel Draw, Xara Pro etc
TCPDF works well and there is a bunch of code samples including fonts and support for vector images eps and ai output to PDF
eps/ai example http://www.tcpdf.org/examples/example_032.pdf
All the examples and php code http://www.tcpdf.org/examples.php
I know what this is quite old question, but I had some problem few weeks ago and solve it for myself, hope this answer helps someone.
Cairo library have PHP bindings, but it also have few bugs which break convertation between formats - forget about it. We need something native here on start. Look at SVG format - open your vector image in editor (I use Inkscape) and save it as SVG file. After that you can change it via php just like xml file.
Adding custom fonts in SVG:
$text_path = 'm 100,200'
$font_name = 'Some_font.ttf';
$font_size = '20px';
$font = base64_encode('font_file_content');
$text = 'Bla bla bla';
$font_svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path d="' . $text_path . '" id="font_id_123"/>
<style type="text/css">
<![CDATA[
#font-face {
font-family: ' . $font_name . ';
src: url("data:font/ttf;charset=utf-8;base64,' . $font . '");
]]>
</style>
</defs>
<text style="font-family: ' . $font_name . '; font-size: ' . $font_size . ';">
<textPath xlink:href="#font_id_123">' . $text . '</textPath>
</text>
</svg>';
$content = file_get_contents($svg_file); // $svg_file - your vector image
$content = substr($content, 0, -6); // cut last '</svg>' tag from file
$newContent = $content . $font_svg . '</svg>'; // add font to the end
file_put_contents($svg_file, $newContent); // save changes
Ok, we have SVG with needed fonts, but we need EPS. For converting SVG to EPS I used Inkscape with simple bash script svg2eps.sh:
#!/bin/bash
inkscape -f $1 -z -T -E $2
You can call it from php:
exec('/path/to/svg2eps.sh /path/to/in.svg path/to/out.eps');
Other tips:
1)Install latest version of Inkscape. I tested it on openSuse 12.3 - works great.
2)Install all custom fonts to system fonts.
Try these links:
http://www.imagemagick.org/script/magick-vector-graphics.php
and
http://www.imagemagick.org/discourse-server/viewtopic.php?f=10&t=10144
I can't tell you how to create vector images in PHP but perhaps you would like a bit different approach- create raster images in PHP and convert them to vectors? It works okay for black & white images not sure about color ones.
<?php
$im = imagecreatetruecolor(500,500);
//draw something on $im
imagepng($im, 'image.png');
$url = 'http://server.com/image.png'; //change to your server's domain
$data = json_decode(file_get_contents('http://api.rest7.com/v1/raster_to_vector.php?url=' . $url . '&format=svg'));
if (#$data->success !== 1)
{
die('Failed');
}
$vec = file_get_contents($data->file);
file_put_contents('vectors.svg', $vec);