php+batik implements the function of exporting highchart pictures

巴扎黑
Release: 2016-11-09 14:21:20
Original
1390 people have browsed it

Recently there is an export requirement that requires highchart pictures and data reports to be generated together.
After struggling for a few days, I finally got it done.
1. First you need to install the JAVA environment. No need to set environment variables or anything.
2. Download the relevant batik jar package. I downloaded this many times but it was wrong. The attachment is what I compiled.
3. To call the officially written PHP program, I organized it into a function.

function svgToImg($type, $svg, $filename){
clearFile(SAVE_PATH);//删除过期文件
ini_set('magic_quotes_gpc', 'off');
$svg = (string) $svg;
$filename = (string) $filename;
// prepare variables
if (!$filename or !preg_match('/^[A-Za-z0-9\-_ ]+$/', $filename)) {
$filename = 'chart';
}
if (get_magic_quotes_gpc()) {
$svg = stripslashes($svg);
}
// check for malicious attack in SVG
if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
exit("Execution is stopped, the posted SVG could contain code for a malicious attack");
}
$tempName = md5(rand());
// allow no other than predefined types
if ($type == &#39;image/png&#39;) {
$typeString = &#39;-m image/png&#39;;
$ext = &#39;png&#39;;
} elseif ($type == &#39;image/jpeg&#39;) {
$typeString = &#39;-m image/jpeg&#39;;
$ext = &#39;jpg&#39;;
} elseif ($type == &#39;application/pdf&#39;) {
$typeString = &#39;-m application/pdf&#39;;
$ext = &#39;pdf&#39;;
} elseif ($type == &#39;image/svg+xml&#39;) {
$ext = &#39;svg&#39;;
} else { // prevent fallthrough from global variables
$ext = &#39;txt&#39;;
}
$outfile = SAVE_PATH.$tempName.&#39;.&#39;.$ext;
if (isset($typeString)) {
// size
$width = &#39;&#39;;
if ($_POST[&#39;width&#39;]) {
$width = (int)$_POST[&#39;width&#39;];
if ($width) $width = "-w $width";
}
// generate the temporary file
if (!file_put_contents(SAVE_PATH."$tempName.svg", $svg)) {
die("Couldn&#39;t create temporary file. Check that the directory permissions for
the /temp directory are set to 777.");
}
if(IS_WIN){
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width ".SAVE_PATH."$tempName.svg");
}else{
$output = shell_exec("/usr/local/jdk1.8.0_66/bin/java -jar ". BATIK_PATH ." $typeString -d $outfile $width ".SAVE_PATH."$tempName.svg");
}
 
// catch error
if (!is_file($outfile)) {
 echo "<pre class="brush:php;toolbar:false">$output
Copy after login
"; echo "Error while converting SVG. "; } else { return $outfile; } } else if ($ext == 'svg') { header("Content-Disposition: attachment; filename="$filename.$ext""); header("Content-Type: $type"); echo $svg; } else { echo "Invalid type"; } }

Save the file locally, and then you can do whatever you want.
If you really find it troublesome to install the JAVA environment, you can directly simulate and submit a request for the official export address.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!