>  기사  >  백엔드 개발  >  php+batik은 하이차트 사진 내보내기 기능을 구현합니다.

php+batik은 하이차트 사진 내보내기 기능을 구현합니다.

巴扎黑
巴扎黑원래의
2016-11-09 14:21:201393검색

최근에는 하이차트 사진과 데이터 보고서를 함께 생성해야 하는 내보내기 요구 사항이 있습니다.
며칠 고생 끝에 드디어 해냈습니다.
1. 먼저 JAVA 환경을 설치해야 합니다. 환경 변수 등을 설정할 필요가 없습니다.
2. 관련 바틱병 패키지를 다운로드합니다. 여러 번 다운로드했는데 잘못되었습니다. 첨부파일은 제가 정리한 것입니다.
3. 공식적으로 작성된 PHP 프로그램을 호출하기 위해 함수로 정리했습니다.

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
"; echo "SVG 변환 중 오류가 발생했습니다. "; } 또 다른 { $outfile을 반환합니다. } } else if ($ext == 'svg') { header("콘텐츠 처리: 첨부 파일; filename="$filename.$ext""); header("콘텐츠 유형: $type"); 에코 $svg; } 또 다른 { echo "잘못된 유형입니다"; } }

파일을 로컬에 저장한 다음 원하는 작업을 수행할 수 있습니다.
정말 JAVA 환경 설치가 번거롭다면 직접 시뮬레이션하여 공식 수출 주소 요청서를 제출할 수 있습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 정적 정적 변수다음 기사:PHP 정적 정적 변수