Skip to content

betobetok/ask-svg

Repository files navigation

ASK - PHP Svg Manipulation

Tests Code Style Latest Stable Version Total Downloads

A package to convert an svg graphic file into a Php object. based on Blade UIkit

by using the svg() function we can retrieve an svg file using the file name and convert it into an object of the Svg class and manipulate it as such.

$mySvg = svg('my_svg');
dd($mySvg);

svg file my_svg.svg

ASK\Svg\Svg ({)#427 ▼
  #name: "svg"
  #elements: array:1 [▼
    0 => ASK\Svg\Configurators\G ({)#301 ▼
      #name: "g"
      #elements: array:37 [▶]
      #context: ASK\Svg\Svg ({)#427(})
      #transforms: ASK\Svg\Transformation ({)#424 ▼
        #transformations: array:1 [▼
          0 => array:1 [▼
            "matrix" => NumPHP\Core\NumArray ({)#423 ▶(})
          ]
        ]
      (})
      #isTransformable: true
      -attributes: array:1 [▶]
      +"g": array:37 [▶]
    (})
  ]
  #context: null
  #isTransformable: false
  -attributes: array:6 [▼
    "id" => "my_svg"
    "width" => "473"
    "height" => "477"
    "viewBox" => "0 0 473 477"
    "fill" => "none"
    "xmlns" => "http://www.w3.org/2000/svg"
  ]
  +style: ASK\Svg\Configurators\Style ({)#426 ▼
    #name: "style"
    #context: ASK\Svg\Svg ({)#427(})
    #isTransformable: false
    -attributes: []
    -classes: []
    -rules: []
  (})
  +"g": array:1 [▼
    0 => ASK\Svg\Configurators\G ({)#301 ▶(})
  ]
(})

The Svg class

 Full name: \ASK\Svg\Svg

the Svg document

Class Svg implements: \ASK\Svg\Conteiner

Svg Properties

Class Properties
visibility Property Type Description
public style Configurators\Style

Svg Methods

Class methods

Svg :: constructor

Svg (string fileName, string contents, array attributes = []): mixed
Parameter Type Description
fileName string
contents string
attributes array

returns


Svg :: getStylefromContent

get the Style element from the string content

public getStylefromContent(): string

returns


Svg :: removeStylefromContent

remove the string Style from the string content

public removeStylefromContent(): self

returns


Svg :: style

get the Style element

public style(): Configurators\Style

returns


Svg :: setStyle

set the Style element

public setStyle(Configurators\Style style): self
Parameter Type Description
style Configurators\Style

returns


Svg :: replaceClasses

replace the Classes names in the string content

public replaceClasses(Configurators\Style style, string content): string
Parameter Type Description
style Configurators\Style
content string

returns


Svg :: mergeSvgs

merge one or more Svgs in this svg

public mergeSvgs(Svg[] param): Svg
Parameter Type Description
param Svg[]

returns


Svg :: toHtml

implements of Htmlable, toHtml return a string form of the svg in HTML format

public toHtml(): string

returns


Svg :: getAllSvgElements

get all the Svg elements in this svg, this array conteins all the elements in order

public getAllSvgElements(mixed svg): array
Parameter Type Description
svg mixed

returns


Svg :: cleanContent

cleanContent remove all string contents in the complet object

public cleanContent(): self

returns


Svg :: getContent

Get the value of content (Conteiner implement) //TODO implementation of container

public getContent(): mixed

returns


Svg :: setContent

Set the value of content (Conteiner implement) //TODO implementation of container

public setContent(mixed content): self
Parameter Type Description
content mixed

returns


Svg :: removeSvgAttribute

removes those attributes that belong exclusively to the svg element

public removeSvgAttribute(): void

returns


Svg :: getOnlySvgAttribute

get those attributes that belong exclusively to the svg element

public getOnlySvgAttribute(): array

returns




The SvgElement class

 Full name: \ASK\Svg\SvgElement

An element belonging to a svg structure

This object represents all the elements within an svg document

from the svg parent element to the internal elements or figures such as <path>, <circle> or <g>, passing through configuration elements such as <style>, <defs> among others

all elements are accessible in order through the attribute $elements, as well as through the array in the property with the name of the element

for example:

  • $svg->g returns all <g> elements
  • $svg->elements[0] return the first element in the <svg></svg>
  • $svg->g[0]->elements[0] return the first element in the <g></g>

Class SvgElement implements: \Illuminate\Contracts\Support\Htmlable

SvgElement Properties

Class Properties
visibility Property Type Description
protected name string
protected elements SvgElement[]
protected contents string
protected context SvgElement
protected transforms Transformation
protected isTransformable bool

SvgElement Methods

Class methods

SvgElement :: constructor

SvgElement (string name, string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
name string
contents string
attributes array
context SvgElement

returns


SvgElement :: makeTransformable

Make this element Transformable

public makeTransformable(): void

returns


SvgElement :: makeUntransformable

make this element Untransformable

public makeUntransformable(): void

returns


SvgElement :: name

get or set the name of the element

public name(string arg = &#039;&#039;): string|self
Parameter Type Description
arg string

returns


SvgElement :: id

get or set the id attribute of the element in the attributes property

public id(string arg = &#039;&#039;): string|self
Parameter Type Description
arg string

returns


SvgElement :: transform

set or get the Transformation Object for the element

public transform(mixed arg = null): Transformation|string
Parameter Type Description
arg mixed

returns


SvgElement :: contents

get the string contents of the elements

  • this method is similar at toHtml but get just the string of the content without the tag string
public contents(): string

returns


SvgElement :: removeContents

remove the string Contents property from the object

public removeContents(): self

returns


SvgElement :: setContents

set the string Contents property in the object

public setContents(string contents): self
Parameter Type Description
contents string

returns


SvgElement :: getContext

get the property Context that is the parent SVG element of this element

public getContext(): SvgElement|null

returns


SvgElement :: getAllElements

get all Elements from the string content this method make all found elements as SvgElement Objects and put in a property with the element name

public getAllElements(): void

returns


SvgElement :: setElement

set a neu Element in this object

public setElement(string name, mixed element): void
Parameter Type Description
name string
element mixed

returns


SvgElement :: mergeAttributes

merge the attributes conteined in a string tag with the array attributes

public mergeAttributes(string tag, array attributes): array
Parameter Type Description
tag string
attributes array

returns


SvgElement :: removeComents

remove coments from the string content

public removeComents(): self

returns


SvgElement :: toHtml

implements of Htmlable, toHtml return a string form of the svg element in HTML format

public toHtml(): string

returns


SvgElement :: removeId

remove the property Id

public removeId(): self

returns


SvgElement :: toArray

toArray

public toArray(): void

returns


SvgElement :: serialize

public serialize(mixed data): mixed
Parameter Type Description
data mixed

returns


SvgElement :: unserialize

public unserialize(mixed data): mixed
Parameter Type Description
data mixed

returns


SvgElement :: findFirstGroupElement

find the first group element with the name $element and return the new corresponding SvgElement instance or null if not found

public findFirstGroupElement(string element): SvgElement|null
Parameter Type Description
element string

returns


SvgElement :: findFirstElement

return the first Element in the elements array or null if not found

public findFirstElement(string elementName): SvgElement|null
Parameter Type Description
elementName string

returns


SvgElement :: findFirstNonGroupElement

find the first non-group element with the name $element and return the new corresponding SvgElement instance or null if not found

public findFirstNonGroupElement(string element): SvgElement|null
Parameter Type Description
element string

returns


SvgElement :: getElementAttributes

get the attributes of a element froma string tag ()

public getElementAttributes(string tag): array
Parameter Type Description
tag string

returns


SvgElement :: getTransformations

get the Transformations of the element and make the corresponding Transformation Object

public getTransformations(): void

returns


SvgElement :: getElementById

find an element by its id and return it or null if not found

public getElementById(string id): SvgElement|null
Parameter Type Description
id string

returns


SvgElement :: getStartPointById

get the start position point of an Shape element by its Id and return it or null if not found

public getStartPointById(string id): \NumPHP\Core\NumArray|null
Parameter Type Description
id string

returns


SvgElement :: hasContext

return true if the element has content or false otherwise

public hasContext(): void

returns




The abstract class Shape

 Full name: \ASK\Svg\Shapes\Shape

An element that make a Shape in a svg document

Shape Properties

Class Properties
visibility Property Type Description
protected startPosition \NumPHP\Core\NumArray

Shape Methods

Class methods

Shape :: constructor

Shape (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Shape :: getStartPosition

get the Start position o the element

public getStartPosition(): void

returns


Shape :: renderAttributes

renderAttributes return a string with attributes in a HTML format (overloaded Method from RenderAttributes)

protected renderAttributes(): string

returns


Shape :: toHtml

(overloaded Method from SvgElement)

public toHtml(): string

returns



Class Circle

 Full name: \ASK\Svg\Shapes\Circle

A Circle element in a svg document

Class Circle inherits from class: Shape


Circle Properties

Class Properties
visibility Property Type Description
protected cx float
protected cy float
protected r float

Methods

Class methods

Circle :: constructor

Circle (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Circle :: center

get the center of the circle

public center(): \NumPHP\Core\NumArray

returns


Circle :: diameter

get the Diameter of the circle

public diameter(): float

returns


Circle :: radio

get the Radio of the circle

public radio(): float

returns


Circle :: area

get the Area of the circle

public area(): float

returns



Class Ellipse

 Full name: \ASK\Svg\Shapes\Ellipse

A Ellipse element in a svg document

Class Ellipse inherits from class: Shape


Ellipse Properties

Class Properties
visibility Property Type Description
protected cx float
protected cy float
protected rx float
protected ry float

Methods

Class methods

Ellipse :: constructor

Ellipse (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Ellipse :: center

get the Center of the ellipse

public center(): \NumPHP\Core\NumArray

returns


Ellipse :: radioX

get the x Radio of the ellipse

public radioX(): float

returns


Ellipse :: radioY

get the y Radio of the ellipse

public radioY(): float

returns


Ellipse :: area

get the Area of the ellipse

public area(): float

returns



Class Line

 Full name: \ASK\Svg\Shapes\Line

A Line element in a svg document

Class Line inherits from class: Shape


Line Properties

Class Properties
visibility Property Type Description
protected x1 float
protected y1 float
protected x2 float
protected y2 float

Methods

Class methods

Line :: constructor

Line (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Line :: long

get teh Logitud of the line

public long(): float

returns



Class Path

 Full name: \ASK\Svg\Shapes\Path

A Path element in a svg document

Class Path inherits from class: Shape


Path Properties

Class Properties
visibility Property Type Description
private dString string
protected d array

Methods

Class methods

Path :: constructor

Path (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Path :: toHtml

(overloaded Method from SvgElement)

public toHtml(): string

returns


Path :: content

content get the content string of the svg elemnt to print in a HTML document

public content(): string

returns


Path :: getExistingComands

getExistingComands get the existing comands in a d attribute from a string

public getExistingComands(string d): array
Parameter Type Description
d string

returns


Path :: renderAttributes

renderAttributes return a string with attributes in a HTML format (overloaded Method from RenderAttributes)

protected renderAttributes(): string

returns



Class Polygon

 Full name: \ASK\Svg\Shapes\Polygon

A Polygon element in a svg document

Class Polygon inherits from class: Shape


Polygon Properties

Class Properties
visibility Property Type Description
protected points array

Methods

Class methods

Polygon :: constructor

Polygon (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Polygon :: toHtml

(overloaded Method from SvgElement)

public toHtml(): string

returns


Polygon :: getPoints

get the array Points from a string

public getPoints(string points): array
Parameter Type Description
points string

returns



Class Polyline

 Full name: \ASK\Svg\Shapes\Polyline

A Polyline element in a svg document

Class Polyline inherits from class: Shape


Polyline Properties

Class Properties
visibility Property Type Description
protected points array

Methods

Class methods

Polyline :: constructor

Polyline (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Polyline :: toHtml

(overloaded Method from SvgElement)

public toHtml(): string

returns


Polyline :: getPoints

get the array Points from a string

public getPoints(string points): array
Parameter Type Description
points string

returns



Class Rect

 Full name: \ASK\Svg\Shapes\Rect

A Rect element in a svg document

Class Rect inherits from class: Shape


Rect Properties

Class Properties
visibility Property Type Description
protected x float
protected y float
protected width float
protected height float
protected rx float
protected ry float

Methods

Class methods

Rect :: constructor

Rect (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns


Rect :: center

get the Center of the rectangle

public center(): \NumPHP\Core\NumArray

returns


Rect :: area

get the Area of the rectangle

public area(): float

returns



Class Text

 Full name: \ASK\Svg\Shapes\Text

a Text element in a svg document

Class Text inherits from class: Shape


Text Properties

Class Properties
visibility Property Type Description

Methods

Class methods

Text :: constructor

Text (string contents, array attributes = [], SvgElement context = null): mixed
Parameter Type Description
contents string
attributes array
context SvgElement

returns



The abstract class Command

 Full name: \ASK\Svg\DCommands\Command

A command in a d attribute of a svg path

There are five line commands for <path> nodes.

  • M - Move
  • L - Line
  • H - Horizontal
  • V - Vertical
  • Z - Close

und five arc commands.

  • C - Cubic Curve
  • Q - Quadratic Curve
  • S - Short Cubic Curve
  • T - Together Multiple Quadratic Curve
  • A - Arc

Each command contains a $coordinates array with all the parameters of each point, as well as a reference to the previous command.

Class Command implements: \Illuminate\Contracts\Support\Htmlable

Command Properties

Class Properties
visibility Property Type Description
protected nextPoint int
protected count mixed
protected type string
protected coordinates array
protected prev DCommands\Command
protected position int
protected endPointCoordinates array

Command Methods

Class methods

Command :: constructor

Command (string type, array parameters = [], ?DCommands\Command prev = null): mixed
Parameter Type Description
type string
parameters array
prev ?DCommands\Command

returns


Command :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


Command :: getComand

getComand

return the name of the command. Uppercase if it's absolute lowercase if relative

public getComand(): string

returns


Command :: setEndPoint

setEndPoint set the values of the coordinates of the end point in the command list, both Absolute and Relative

public setEndPoint(array relativePoint, array absolutePoint): void
Parameter Type Description
relativePoint array
absolutePoint array

returns


Command :: toHtml

public toHtml(): mixed

returns


Command :: getEndPoint

getEndPoint returns an array with the x and y value of the end point. If the parameter "absolute" is put to true the Absolute value of the end point is returned, relative is returned otherwise

public getEndPoint(bool absolute = true): array
Parameter Type Description
absolute bool

returns


Command :: resetNext

resetNext

public resetNext(): void

returns


Command :: getLastMComand

getLastMComand returns the last M command in the "d" attribute

public getLastMComand(): DCommands\Command|null

returns


Command :: getPoint

getPoint returns the array with the x and y parameters of the n point, if the parameter "absolute" is set to true, the Absolute values are returned, relative are retuned otherwise

public getPoint(int n = null, bool absolute = false): array
Parameter Type Description
n int
absolute bool

returns


Command :: getDinstance

getDinstance get the distance between tow points, if the second parameter is not gived, returns the Absolut distans of the point

public getDinstance(array fromPoint, array toPoint = []): array
Parameter Type Description
fromPoint array
toPoint array

returns



Class A

 Full name: \ASK\Svg\DCommands\A

A comand "a" in a d attribute of a svg path

Arcs are sections of circles or ellipses. For a given x-radius and y-radius, there are two ellipses that can connect any two points (last end point and (x, y)). Along either of those circles, there are two possible paths that can be taken to connect the points (large way or short way) so in any situation, there are four possible arcs available.

Because of that, arcs require seven parameters: A rx ry x-axis-rotation large-arc-flag sweep-flag x y

a rx ry x-axis-rotation large-arc-flag sweep-flag dx dy

A command hat in aditional to the other commands a getCenter Methode

Class A inherits from class: Command


A Properties

Class Properties
visibility Property Type Description

Methods

Class methods

A :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


A :: getCenter

getCenter get the centero of the n arc

public getCenter(int n = null): array
Parameter Type Description
n int the arc number of which we want the center

returns



Class C

 Full name: \ASK\Svg\DCommands\C

A comand "c" in a d attribute of a svg path

The cubic curve, C, is the slightly more complex curve. Cubic Béziers take in two control points for each point. Therefore, to create a cubic Bézier, three sets of coordinates need to be specified.

C x1 y1, x2 y2, x y c dx1 dy1, dx2 dy2, dx dy

Class C inherits from class: Command


C Properties

Class Properties
visibility Property Type Description

Methods

Class methods

C :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns



Class H

 Full name: \ASK\Svg\DCommands\H

A comand "h" in a d attribute of a svg path

A command draws a horizontal line, this command only take one parameter since they only move in one direction.

H x h dx

Class H inherits from class: Command


H Properties

Class Properties
visibility Property Type Description
protected x float

Methods

Class methods

H :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


H :: getX

getX

public getX(): float

returns


H :: setX

setX Set the value of x

public setX(float x): self
Parameter Type Description
x float

returns



Class L

 Full name: \ASK\Svg\DCommands\L

A comand "h" in a d attribute of a svg path

L x y l dx dy

Class L inherits from class: Command


L Properties

Class Properties
visibility Property Type Description

Methods

Class methods

L :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns



Class M

 Full name: \ASK\Svg\DCommands\M

A comand "m" in a d attribute of a svg path

M x y m dx dy

Class M inherits from class: Command


M Properties

Class Properties
visibility Property Type Description
private x float
private y float

Methods

Class methods

M :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


M :: getMDinstance

getMDinstance get the distance between last m point and the point of parameter

public getMDinstance(array toPoint = []): array
Parameter Type Description
toPoint array

returns


M :: getX

Get the value of x

public getX(): float

returns


M :: setX

Set the value of x

public setX(float x): self
Parameter Type Description
x float

returns


M :: getY

Get the value of y

public getY(): float

returns


M :: setY

Set the value of y

public setY(float y): self
Parameter Type Description
y float

returns



Class Q

 Full name: \ASK\Svg\DCommands\Q

A comand "q" in a d attribute of a svg path

Q x1 y1, x y q dx1 dy1, dx dy

Class Q inherits from class: Command


Q Properties

Class Properties
visibility Property Type Description

Methods

Class methods

Q :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns



Class S

 Full name: \ASK\Svg\DCommands\S

A comand "s" in a d attribute of a svg path

S x2 y2, x y s dx2 dy2, dx dy

Class S inherits from class: Command


S Properties

Class Properties
visibility Property Type Description

Methods

Class methods

S :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns



Class T

 Full name: \ASK\Svg\DCommands\T

A comand "t" in a d attribute of a svg path

T x y t dx dy

Class T inherits from class: Command


T Properties

Class Properties
visibility Property Type Description

Methods

Class methods

T :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns



Class V

 Full name: \ASK\Svg\DCommands\V

A comand "v" in a d attribute of a svg path

V y v dy

Class V inherits from class: Command


V Properties

Class Properties
visibility Property Type Description
protected y float

Methods

Class methods

V :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


V :: getY

getY

public getY(): float

returns


V :: setY

setY

public setY(float y): self
Parameter Type Description
y float

returns



Class Z

 Full name: \ASK\Svg\DCommands\Z

A comand "z" in a d attribute of a svg path

Class Z inherits from class: Command


Z Properties

Class Properties
visibility Property Type Description

Methods

Class methods

Z :: initialization

initialization is a configuration method for the specific type of command

public initialization(mixed parameters): void
Parameter Type Description
parameters mixed

returns


Z :: getPoint

getPoint returns the array with the x and y parameters of the n point, if the parameter "absolute" is set to true, the Absolute values are returned, relative are retuned otherwise

public getPoint(mixed n = null, mixed absolute = true): array
Parameter Type Description
n mixed
absolute mixed

returns



alphabetical listing


This file is public domain. Use it for any purpose, including commercial applications. Attribution would be nice, but is not required. There is no warranty of any kind, including its correctness, usefulness, or safety.

Author: Alberto Solorzano Kraemer ( alberto.kraemer@gmail.com, @betobetok )


This document was automatically generated from source code comments on 2022-03-25 using phpDocumentor and cvuorinen/phpdoc-markdown-public