1<?php
2/**
3 * svg Image Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13
14class helper_plugin_svgimg extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin
15
16	var $database = null;
17	var $dbType = '';
18
19    function getInfo(){
20        return array_merge(confToHash(dirname(__FILE__).'/info.txt'), array(
21				'name' => 'svg Image Helper Plugin',
22		));
23    }
24
25    function getMethods() {
26        $result = array();
27		return $result;
28	}
29
30	function create_svg_image($data, $ext, $cacheFile, $progType=null) {
31			global $conf;
32
33			$cmd = '';
34			$width = intval($data['width']);
35			$height = intval($data['height']);
36
37			if ( empty($progType) ) $progType = $this->getConf('use_programm');
38
39			switch ( $progType ) {
40				case 'inkscape':
41
42							$width = empty( $width ) ? '' : ' -w' . intval($width);
43							$height = empty( $height ) ? '' : ' -h' . intval($height);
44
45							$cmd =	sprintf("%s %s -z%s%s --export-%s=%s",
46									'inkscape',
47									escapeshellarg($data['file']),
48									$width,
49									$height,
50									$ext,
51									escapeshellarg($cacheFile));
52				break;
53				case 'imagemagick':
54
55							$param = '';
56							$cmd = '';
57							if ( !empty($width) ) $param .= intval($width);
58							if ( !empty($width) && !empty($data['height']) ) $param .= 'x';
59							if ( !empty($height) ) $param .= intval($height);
60							if ( !empty($param) ) $cmd = " -resize " . $param;
61
62							if ( !empty( $data['border'] )) { $cmd .= ' -border ' . intval($data['border']); }
63							if ( !empty( $data['bordercolor'] )) { $cmd .= ' -bordercolor "' . escapeshellarg($data['bordercolor']) . '"'; }
64							$cmd .= " -strip -coalesce -quality " . intval($conf['jpg_quality']);
65							$cmd =	sprintf("{$conf['im_convert']} %s %s %s >/dev/null 2>&1",
66									$cmd,
67									escapeshellarg($data['file']),
68									escapeshellarg($cacheFile));
69				break;
70				case 'rsvg':
71
72					$width = empty( $width ) ? '' : ' -w ' . intval($width);
73					$height = empty( $height ) ? '' : ' -h ' . intval($height);
74
75					$cmd =	sprintf("rsvg %s %s --format=%s %s %s",
76							$width,
77							$height,
78							$ext,
79							escapeshellarg($data['file']),
80							escapeshellarg($cacheFile));
81					break;
82			}
83
84			// Alte Datei l�schen, bevor neue kommt
85			@unlink(escapeshellarg($cacheFile));
86			$last_line = system($cmd, $retval);
87			if ( @file_exists($cacheFile)) return true;
88			return false;
89	}
90
91	// Return a cache File Name
92	// $data has:
93	// width, height, background
94	function _get_cache_file($data, $ext=null) {
95
96		if ( empty( $data['file'] ) ) return null;
97		$param = ".media.svgimg";
98		if ( !empty($data['width']) || !empty($data['height']) ) $param .= '.';
99		if ( !empty($data['width']) ) $param .= intval($data['width']);
100		if ( !empty($data['width']) && !empty($data['height']) ) $param .= 'x';
101		if ( !empty($data['height']) ) $param .= intval($data['height']);
102		if ( !empty($data['background']) ) $param .= '.' . ( ! is_array($data['background']) ? $data['background'] : $data['background']['orig'] );
103		if ( empty($ext) ) $ext = $this->getConf('ext');
104		$param .= '.' . $ext;
105
106		return getCacheName($data['file'],$param);
107	}
108
109	// get RGB from HEX Color
110	function _calc_bgcolor( $bgcolor ) {
111
112		if ( empty($bgcolor) ) { $bgcolor = $this->getConf('bgc'); }
113
114		//	Does it start with a hash? If so then strip it
115		$bgcolor = str_replace('#', '', $bgcolor);
116
117		switch (strlen($bgcolor))
118		{
119			case 6:
120				$red = hexdec(substr($bgcolor, 0, 2));
121				$green = hexdec(substr($bgcolor, 2, 2));
122				$blue = hexdec(substr($bgcolor, 4, 2));
123				break;
124
125			case 3:
126				$red = substr($bgcolor, 0, 1);
127				$green = substr($bgcolor, 1, 1);
128				$blue = substr($bgcolor, 2, 1);
129				$red = hexdec($red . $red);
130				$green = hexdec($green . $green);
131				$blue = hexdec($blue . $blue);
132				break;
133
134			default:
135				//	Wrong values passed, default to black
136				$red = 0;
137				$green = 0;
138				$blue = 0;
139		}
140
141		return array('red' => $red, 'green' => $green, 'blue' => $blue, 'orig' => $bgcolor );
142	}
143}
144
145//Setup VIM: ex: et ts=4 enc=utf-8 :