1
0
mirror of https://github.com/Chouchen/svgToImage.git synced 2020-02-03 22:08:42 +01:00

Ajout de gd_info.php pour vérifier si le serveur accepte cette classe.

Ajout d'éléments SVG (stroke-width, text)
Ajout d'éléments GD (allocate color)
Modification d'elipse en arc (pour stroke-width... bug GD)
This commit is contained in:
Shikiryu 2010-11-26 15:07:16 +00:00
parent 31a3a2e277
commit 60893584e8
3 changed files with 57 additions and 14 deletions

2
gd_info.php Normal file
View File

@ -0,0 +1,2 @@
<?
print_r( gd_info() );

View File

@ -1,4 +1,10 @@
<?
// TODO
// ajouter header thanks to http://fr.php.net/manual/fr/function.image-type-to-mime-type.php
// prendre en compte l'opacité grâce à imagecolorallocatealpha ?
// conversion des couleurs "black" , "blue", etc en #000 etc.
// stroke-width pour tous \o/
include 'log.php';
class SVGTOIMAGE{
@ -98,7 +104,7 @@ class SVGTOIMAGE{
return isset($this->_height) ? $this->_height : $this->_svgXML->attributes()->height;
}
private function _parseColor($colorCode){
if(strlen($colorCode) == 7){
if($this->_debug) $this->_log->message('Parse Color '.$colorCode);
@ -120,6 +126,11 @@ class SVGTOIMAGE{
return array(0,0,0);
}
private function _allocateColor($color){
$arrayColor = $this->_parseColor($color);
return imagecolorallocate( $this->_image, $arrayColor[0], $arrayColor[1], $arrayColor[2] );
}
private function _parseImage($imageNode){
$x = 0;
$y = 0;
@ -172,10 +183,17 @@ class SVGTOIMAGE{
imagecopy($this->_image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage));
}
private function _parsePath($pathNode){
// tODO with imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
// imagesetbrush
// imagesetstyle (pour dotted, dashed etc)
}
private function _parseCircle($circleNode){
$x = 0;
$y = 0;
$r = 0;
$strokeWidth = 1;
$fill = '';
$stroke = '';
foreach($circleNode->attributes() as $name => $value){
@ -187,20 +205,29 @@ class SVGTOIMAGE{
case 'fill': $fill = ($value == 'none') ? '' : $value; break;
case 'stroke': $stroke = $value; break;
case 'style' : if(strripos($value, 'display: none') || strripos($value, 'display:none')) return; break;
case 'stroke-width' : $strokeWidth = $value; break;
}
}
if($r == 0)
return;
$colorStroke = $this->_parseColor($stroke);
$colorFill = $this->_parseColor($fill);
if($this->_debug) $this->_log->message('Cercle - x : '.$x.' - y : '.$y.' - rayon : '.$r.' - fill : '.$colorFill[0].'-'.$colorFill[1].'-'.$colorFill[2].' - stroke : '.$colorStroke[0].'-'.$colorStroke[1].'-'.$colorStroke[2]);
if($fill == ''){
if($this->_debug) $this->_log->message('Cercle - x : '.$x.' - y : '.$y.' - rayon : '.$r.'-'.$colorStroke[2].' - épaisseur : '.$strokeWidth);
$thickness = imagesetthickness( $this->_image , (int)$strokeWidth );
if($this->_debug && !$thickness) $this->_log->error('Erreur dans la mise en place de l\'épaisseur du trait');
imageellipse ($this->_image , $x , $y , $r*2 , $r*2, imagecolorallocate($this->_image, $colorStroke[0], $colorStroke[1], $colorStroke[2]) );
$colorStroke = $this->_allocateColor($stroke);
$colorFill = $this->_allocateColor($fill);
if($fill == ''){
imagearc($this->_image , $x , $y , $r*2 , $r*2,0,359.9, $colorStroke );
//imageellipse ($this->_image , $x , $y , $r*2 , $r*2, $colorStroke );
}else{
imagefilledellipse($this->_image , $x , $y , $r*2 , $r*2 , imagecolorallocate($this->_image, $colorFill[0], $colorFill[1], $colorFill[2]) );
imagefilledarc($this->_image , $x , $y , $r*2 , $r*2 ,0,359.9, $colorFill, IMG_ARC_PIE );
}
imagecolordeallocate( $this->_image, $colorStroke);
imagecolordeallocate( $this->_image, $colorFill);
imagesetthickness ( $this->_image , 1 );
}
private function _parseRectangle($rectNode){
@ -212,9 +239,11 @@ class SVGTOIMAGE{
$r = 0;
$fill = '';
$stroke = '';
$strokeWidth = 1;
foreach($rectNode->attributes() as $name => $value){
switch($name){
//TODO style display:none
// imagesetthickness ( resource $image , int $thickness )
// imagesetstyle (pour dotted, dashed etc)
case 'x': $x = $value; break;
case 'y': $y = $value; break;
case 'r': $r = $value; break;
@ -222,19 +251,25 @@ class SVGTOIMAGE{
case 'height': $height = $value; break;
case 'fill': $fill = ($value == 'none') ? '' : $value; break;
case 'stroke': $stroke = $value; break;
case 'stroke-width' : $strokeWidth = $value; break;
case 'style' : if(strripos($value, 'display: none') || strripos($value, 'display:none')) return; break;
}
}
if($width == 0 || $height == 0)
return;
$colorStroke = $this->_parseColor($stroke);
$colorFill = $this->_parseColor($fill);
$colorStroke = $this->_allocateColor($stroke);
$colorFill = $this->_allocateColor($fill);
$thickness = imagesetthickness( $this->_image , (int)$strokeWidth );
if($this->_debug && !$thickness) $this->_log->error('Erreur dans la mise en place de l\'épaisseur du trait');
if($this->_debug) $this->_log->message('Rectangle - x : '.$x.' - y : '.$y.' - width : '.$width.' - height : '.$height.' - fill : '.$colorFill[0].'-'.$colorFill[1].'-'.$colorFill[2].' - stroke : '.$colorStroke[0].'-'.$colorStroke[1].'-'.$colorStroke[2]);
if($fill == ''){
imagerectangle($this->_image , $x , $y , $x+$width , $y+$height, imagecolorallocate($this->_image, $colorStroke[0], $colorStroke[1], $colorStroke[2]) ); //resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color
imagerectangle($this->_image , $x , $y , $x+$width , $y+$height, $colorStroke); //resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color
}else{
imagefilledrectangle ($this->_image , $x , $y , $x+$width , $y+$height, imagecolorallocate($this->_image, $colorFill[0], $colorFill[1], $colorFill[2]) );
imagefilledrectangle ($this->_image , $x , $y , $x+$width , $y+$height, $colorFill );
}
imagecolordeallocate($this->_image,$colorStroke);
imagecolordeallocate($this->_image,$colorFill);
imagesetthickness ( $this->_image , 1 );
}
private function _parseDescription($desc){
@ -243,8 +278,11 @@ class SVGTOIMAGE{
}
public function toImage(){
//$test = Imagick::__construct('http://labs.shikiryu.com/experimental-cut/images/pieces/2.png');
$writeDesc = null;
$this->_image = imagecreatetruecolor($this->_getImageWidth(), $this->_getImageHeight());
imagealphablending($this->_image, true);
imageantialias($this->_image, true);
foreach($this->_svgXML->children() as $element){
if($element->getName() == 'image')
$this->_parseImage($element);
@ -256,6 +294,7 @@ class SVGTOIMAGE{
$writeDesc = $element;
}
if($writeDesc) $this->_parseDescription($writeDesc);
//imagefilter ( $this->_image , IMG_FILTER_SMOOTH, 6);
return imagepng($this->_image);
}

View File

@ -8,13 +8,15 @@ $svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height
<desc>Created with Raphael</desc>
<defs></defs>
<image x="0" y="0" width="300" height="512" preserveAspectRatio="none" href="http://labs.shikiryu.com/experimental-cut/images/pieces/fond.jpg"></image>
<rect x="168" y="275" width="52" height="70" r="0" rx="0" ry="0" fill="none" stroke="#FFF" stroke-dasharray="8,3" transform="rotate(21.91207728 194 310)" style="opacity: 1; display: none; " opacity="1"></rect>
<rect x="168" y="275" width="52" height="70" r="0" rx="0" ry="0" fill="none" stroke="#FFF" stroke-width="3" stroke-dasharray="8,3" transform="rotate(21.91207728 194 310)" style="opacity: 1;" opacity="1"></rect>
<circle cx="50" cy="50" r="50" fill="#FFFFFF" stroke="#000"></circle>
<circle cx="100" cy="50" r="40" stroke="#000" stroke-width="2" fill="none"/>
<image x="170" y="277" width="48" height="66" preserveAspectRatio="none" href="http://labs.shikiryu.com/experimental-cut/images/pieces/1.png" style="cursor: move; opacity: 1; " r="90" opacity="1" transform="rotate(21.91207728 194 310)"></image>
</svg>';
$svgtoimage = SVGTOIMAGE::parse($svg);
//$svgtoimage = new SVGTOIMAGE($svg);
$svgtoimage = SVGTOIMAGE::load('basic.svg');
//$svgtoimage = SVGTOIMAGE::load('basic.svg');
$svgtoimage->setShowDesc();
$svgtoimage->setWidth(300);
$svgtoimage->setHeight(512);