Giter VIP home page Giter VIP logo

easysvg's Introduction

EasySVG for PHP

Generate SVG images from SVG font easily.

The SVG data produced here is directly extracted from the font .svg file. This does not use the <text> tag.

Simple usage

require 'easySVG.php';

$svg = new EasySVG();
$svg->setFont("om_telolet_om-webfont.svg", 100, '#000000');
$svg->addText("Simple text display");
$svg->addAttribute("width", "800px");
$svg->addAttribute("height", "120px");
echo $svg->asXML();

Advanced usage

require 'easySVG.php';

$text = "Simple text display\netc.";

$svg = new EasySVG();
$svg->setFontSVG("om_telolet_om-webfont.svg");
$svg->setFontSize(100);
$svg->setFontColor('#000000');
$svg->setLineHeight(1.2);
$svg->setLetterSpacing(.1);
$svg->addText($text);
// set width/height according to text
list($textWidth, $textHeight) = $svg->textDimensions($text);
$svg->addAttribute("width", $textWidth."px");
$svg->addAttribute("height", $textHeight."px");
echo $svg->asXML();

This will output inline SVG for you to play with. You can echo it, save it to a file or whatever.

Method reference

setFont($path, $size, $color = null)

Sets the font attributes. This is a shortcut for :

$this->setFontSVG($path);
$this->setFontSize($size);
$this->setFontColor($color);

These 3 methods are explicit enough, I won't go through these in here.

setLineHeight($value)

Adds a CSS-like line-height value. A numeric value (float) where 1 is the line height defined by the font itself.

setLetterSpacing($value)

Adds a CSS-like letter-spacing value. A numeric value (float) expressed in em where 1 is the width of the m character.

addText($text, $x, $y, $attributes=array())

Add text to the SVG (will be converted to simple path)

  • $text : String UTF-8 encoded
  • $x : X position of the text (starting from left), can be center to center the text horizontally
  • $y : Y position of the text (starting from top), can be center to center the text vertically
  • $attributes (optional) : list of tag attributes

asXML()

Return XML string of the whole SVG.

addAttribute($key, $value)

Add an attribute to the main SVG.

SVG data manipulation methods

You may need these to play around with SVG definitions.

defTranslate($def, $x=0, $y=0)

Applies a translate transformation to a definition. This basically applies matrix calculation to a definition.

defRotate($def, $angle, $x=0, $y=0)

Applies a translate transformation to definition. This basically applies matrix calculation to a definition.

defScale($def, $x=1, $y=1)

Applies a scale transformation to definition. This basically applies matrix calculation to a definition.

textDef($text)

Returns a SVG-formatted definition of a string. This method is used by addText method.

  • $text : String UTF-8 encoded

textDimensions($text)

Returns the width and height of a string. This method is also used to set the width/height of the SVG (if none specified).

  • $text : String UTF-8 encoded

unicodeDef($code)

Returns a SVG-formatted definition of an unicode character.

  • $code : Unicode definition (in hex format)

characterWidth($char, $is_unicode=false)

Returns the width of a character.

  • $char : Character
  • $is_unicode : Boolean that tells if the character is a unicode string or a UTF-8 character.

addPath($def, $attributes=array())

Add a path to the SVG data

  • $def : SVG definition
  • $attributes (optional) : list of tag attributes

Utility methods

clearSVG()

Resets the SVG data. Used to start a new SVG without creating a new instance.

defApplyMatrix($def, $matrix)

Apply a matrix to a definition. Used to apply any kind of transformations, you shouldn't need this, but it is available so you may play with it.

License

MIT. Please feel free to pull, fork, and so on.

easysvg's People

Contributors

franzz avatar kartsims avatar mrk-j avatar ppelisset avatar smartman avatar splitbrain avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.