Coder Social home page Coder Social logo

imagine-svg's Introduction

Contao Imagine SVG library

This project implements the interfaces of Imagine and allows you to make simple modifications to SVG images. It is used in Contao to handle on-the-fly resizing of SVG images.

Installation

php composer.phar require contao/imagine-svg

Usage

use Contao\ImagineSvg\Imagine;
use Imagine\Image\Box;
use Imagine\Image\Point;

$imagine = new Imagine();

$imagine
    ->open('/path/to/image.svg')
    ->crop(new Point(50, 50), new Box(100, 100))
    ->resize(new Box(40, 40))
    ->save('/path/to/thumbnail.svg')
;

$image = $imagine->open('/path/to/image.svg');

$image->effects()
    ->gamma(1.5)
    ->negative()
    ->grayscale()
    ->colorize($color)
    ->sharpen()
    ->blur(2)
;

$image->save('/path/to/image.svg');

Because of the nature of SVG images, the getSize() method differs a little bit from other implementations. You can check the return value like in this example:

use Contao\ImagineSvg\Imagine;
use Contao\ImagineSvg\SvgBox;

$imagine = new Imagine();
$size = $imagine->open('/path/to/image.svg')->getSize();

if (SvgBox::TYPE_NONE === $size->getType()) {
    // The image has no defined size
} elseif (SvgBox::TYPE_ASPECT_RATIO === $size->getType()) {
    // The image has a relative size, $size->getWidth() and $size->getHeight()
    // should be treated as an aspect ratio
} else {
    // The image has a defined size like a regular image
    // $size->getType() would return SvgBox::TYPE_ABSOLUTE in this case
}

imagine-svg's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

imagine-svg's Issues

New major version 1.0.0

We just released a release candidate version for the new major: https://github.com/contao/imagine-svg/releases/tag/1.0.0-RC1

It would probably make sense to test the new "contao/imagine-svg": "1.0.0@RC" version in the dependent packages to verify that we didn’t break their usage of the library:

No Documentation to use Colorize method?

How can I change the color of Black SVG to any RGB color?
I am using this library for modifying size and getting the size. What is the way to change fill color of SVG?

`$imagine = new Imagine();
$image = $imagine->open($svg_file);
$image->resize(new Box(300, 300));

// $image->effects()->colorize($image->palette(), array(255,0,0));

$image->save($file_path);`

So what is the solution to change it to color we want? I have no idea about using colorize method properly.

Deprecate UndefinedBox and RelativeBox

I think we should deprecate the following interfaces and classes:

  • UndefinedBoxInterface
  • RelativeBoxInterface
  • UndefinedBox
  • RelativeBox

And instead add an SvgBox with the methods isUndefined and isRelative. An interface for this class is not needed IMO.

Also getWidth() and getHeight() should probably throw an exception for an undefined box instead of returning zero.

Crop doesn’t set a viewBox attribute

The crop method doesn’t set a viewbox attribute which causes resizing issues in IE11.

The crop method should also check if cropping is necessary and otherwise just skip the process.

How to use flipHorizontally method in this library

Hello there,

Anyone knows how to use flipHorizontally() method in this library.
I referred this docs https://imagine.readthedocs.io/en/stable/index.html but didn't found anything to flip the SVG image.

I simply want to flip the image

$imagine = new Imagine();
  $image = $imagine->open($svg_file);
  $image->resize(new Box($determine_width, $determine_height));
  
  $color_code = $option_list['sic_color_code'] ? $option_list['sic_color_code'] : '#000000';
  $image->effects()->colorize($image->palette()->color($color_code));
  
  $image->flipHorizontally();
  
  $image->save($file_path);

Error is : Uncaught Imagine\Exception\NotSupportedException: This method is not implemented in.........

Any idea what I am missing here?

@ausi you helped me on colorize stuff earlier and it was a great method. Can you please help me here too?
Your help will be highly appreciated.

Update:
I also used following code but same error

$transformation = new Transformation();
$transformation->flipVertically();
$transformation->apply( $imagine->open($file_path) )->save($file_path_2);

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.