Giter Site home page Giter Site logo

dirkgroenen / svgmagic Goto Github PK

View Code? Open in Web Editor NEW
595.0 33.0 69.0 267 KB

Fallback for SVG images by automatically creating PNG versions on-the-fly

Home Page: https://dirkgroenen.github.io/SVGMagic/

License: Apache License 2.0

PHP 24.25% JavaScript 75.75%

svgmagic's Introduction

ScreenShot

SVGMagic - Cross browser SVG

This repository is no longer actively mainted. It has proven to be very usefull back in 2013, but these days SVGs are supported by pretty much all major browsers. The plugin will continue to work, but no guarantees will be given over the API's availability. It has been a great ride!

This ease-to-use jQuery plugin will create a fallback for .SVG images on your website. When the plugin notices that the visitors browser doesn't support .SVG images it will replace those images with new .PNG images. Those .PNG images are created on the run using a serverside script. When the visitors browser does support .SVG images it will just go back to sleep.

A big advantage of SVGMagic is that you don't have to create multiple versions of your images. You can just focus on the .SVG images and let SVGMagic do the rest.

You can find more information and demos on our website.

Build Status

SVG... what/why?

SVG is a vector graphics format, meaning it's perfectly scalable. Whatever size it needs to display at, or whatever screen it needs to display on, an SVG will adapt perfectly. This means that you can use the same image for desktop and mobile (including Retina) visitors. They all get a perfectly sharp image.

Installation

Just include the script in your header and call the plugin in your $(document).ready()

<script src="SVGMagic.min.js"></script>
<script>
	$(document).ready(function(){
		$('img').svgmagic();
	});
</script>

SVGMagic also supports backgroundimages. You need to parse the div containing the backgroundimage including the backgroundimage option.

<script src="SVGMagic.min.js"></script>
<script>
	$(document).ready(function(){
		$('.bgimage').svgmagic({
            handleBackgroundImages: true
        });
	});
</script>

Options

You can parse an options object into SVGMagic. Currently it supports the following options:

$('img').svgmagic({
    temporaryHoldingImage:  null, // Image that will appear when an image gets converted
    forceReplacements:      false, // Force replacement in all browsers
    handleBackgroundImages: false, // Search the dom for CSS background images
    additionalRequestData:  {}, // Add extra data to the ajax request.
    postReplacementCallback:null, // Function to run before replacement

    // New options
    remoteServerUri:        'https://bitlabs.nl/svgmagic/converter/3/', // Uri of the (remote) API script
    remoteRequestType:      'POST', // Request type for the API call
    remoteDataType:         'jsonp', // Data type for the API call
    debug:                  'false' // Show usefull debug information in the console
});

additionalRequestData

The additionalRequestData option gives you the posibility to add extra data to the ajax request. The default API script supports two extra options: {secure: true} and {dumpcache: true}.

Local development

SVGMagic needs public access to the images on your website, which means that you can't use it when developing in a local environment. In case you still need to use the plugin you can download the converter.php script and place it on your local machine.

Support

The plugin is tested in Internet Explorer Version 7 and 8 (other browsers already support SVG files).

Security / How it works

The script makes use of a server side php script that converts the SVG to an PNG. The plugin will send a request to the server containing the images' sources. The server will then grab those images, convert them to PNG, temporarily save them and send the URL of the new images back to the plugin. When the plugin receives the new URL it will replace the .SVG images with the new ones.

This will only happen when the plugin notices that the user's browser doesn't support SVG images. At the moment IE8 and lower and Android 2.* don't support SVG images.

Demo

A demo of SVGMagic can be found on the SVGMagic website.

Known bugs

  • When many images need to be replaced the URL can get too long which will result in a server 414 error.

Changelog

3.0.0 (2014-11-22)

Client:

New features:
    - SVGMagic can now return usefull debug information while replacing SVG images.
    - Added timeout to ajax request. Show debug information when timeout gets exceeded.

New options:
    - debug: Show usefull debug information in the console

Documentation:
    - Added changelog to the bottom of the README
    - Automatically return images over https when request was over https.

Server:

New features:
    - Fully rewrite of the server script. The server will now provide much more information about the convert process.
    - Response will contain the creation date of cached images.
    - Data images are now also cached.

2.4.0 (2014-08-01)

New features:
    - Add extra post data to the ajax request
    - Now also finds data:image SVGs

New options:
    - temporaryHoldingImage:    replacement for preloader
    - forceReplacements:        replacement for testmode
    - handleBackgroundImages:   replacement for backgroundimage
    - additionalRequestData:    send extra data to the server that replaces the SVGs for PNGs
    - postReplacementCallback:  callback function that will be executed before replacement
    - remoteServerUri:          the URI of the remote server that converts the images to PNG
    - remoteRequestType:        set the type of the ajax request (post/get)
    - remoteDataType:           the datatype sent to and received from the remote server

Deprecated options:
    - preloader         > temporaryHoldingImage
    - testmode          > forceReplacements
    - backgroundimage   > handleBackgroundImages
    - secure            > additionalRequestData
    - callback          > postReplacementCallback
    - dumpcache         > additionalRequestData

svgmagic's People

Contributors

craigfowler avatar dirkgroenen avatar markvaneijk avatar

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  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  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

svgmagic's Issues

Inline SVGs?

First of all, great work, really useful plugin!

Would be nice though if it would also work with inline svg graphics =)! Any thoughts about implementing this?

performance impact

As SVGMagic is presented as a quick, "magic" solution, I think you have a duty to explain and educate users on the real performance impact without brushing it off so nonchalantly! Every developer would benefit from understanding technically how browsers parse the page, when JavaScript is executed, how DNS lookups work etc. Unfortunately too many front-end devs see jQuery plugins as magic.

getReplacementUris vs getReplacementUrisFromRemoteService

@craigfowler I've finally merged your fork of SVGmagic to this main repo, but after merging I stumbled on some problems/questions.

The script checks if images need to be replaced, and if so it calls the getReplacementUris function. However, this function doesn't replace the images. I've edited the function call to the getReplacementUrisFromRemoteService function which, after some little modifications, does replace the images.

May I ask why you've added this function to the script and what it should do?

Demo not working?

Hi there,
First of all, great plugin! Exactly what I was looking for!

If I stand correctly, if you set the forceReplacements: true option, it will replace the svg image for an png no matter what browser you're viewing the content in right?
Well if I view the demo in Firefox for example, I only see two svg images? Am I missing something?
https://dirkgroenen.github.io/SVGMagic/demo.html

Another question, will it matter if my img is inside a picture element like this:

<picture>
    <!--[if IE 9]><video style="display: none;"><![endif]-->
    <source srcset="image.svg">
    <!--[if IE 9]></video><![endif]-->
    <img srcset="image.svg" alt="alt text" />
</picture>

p.s. In Chrome it does swap the left svg image

Stopped working in IE8

Hi there,

The current version 3.0.0 just stopped working for me...
Are you by any chance having problems on your server? The demo doesn't work either anymore in IE8.
I only have the following line in my script:

$('img').svgmagic({forceReplacements: true }); // true for debugging

Thanks, Toine

include svg magic into wordpress plugin

hi dirk, how can i implement svgmagic into a wordpress plugin?

i tried:

##################################################################
 enable svgmagic fallback (auto png creation)
##################################################################

function fallback_svg(){
wp_register_script( 'jquery.svgmagic', WP_PLUGIN_URL.'/svg-images/svgmagic.js', array( 'jquery' ), "2.3.1", 1 );
wp_enqueue_script( 'jquery.svgmagic' ); 
}
add_action( 'wp_enqueue_scripts', 'fallback_svg' );

but it won't work - maybe it's because it was with an older version.
i am not so good in writing plugins, so any help is highly appreciated. also i have not a very good testing area (older browsers etc.)
it would be cool to implement svgmagic with the converter.php

best regards
telemarker

IE8 compatibility

Hi, thx for the great plugin, unfortunately the new version (2.3.1) seems not to work in IE8 anymore. Version 2.1.4 worked fine. Exept after a long page-load-time, the script breaked. Any idea?

Not working on IE8 and IE7

SVGMagic is just not doing anything in IE8 (tested in Developer Console and on a Windows XP System). I do use 2.4.5

svgmagic.bitlabs.nl replace lang param in html tag

Hello guys,

your plugin is awesome. This ticket is for your website. The content is en english, but in your <html> you have this param lang="nl-NL". I know is a really small thing, but it's importat. For example the facebook like plugin is in Dutch. I think the lang also affect in some point the SEO?

Another suggestion: use the gh-pages branch to deploy the website is possible, or you are using technologies that GitHub doesn't support?

Mainly useful for <IE9?

First of: kudos :)

However: is this not mainly/only useful for IE lower than IE9 since these (and Android 2.3 according to http://caniuse.com/svg) are the only browsers not supporting this basic SVG usage?

Whatever your stance on supporting these old browsers I think it might be good mentioning that this script will mainly be useful for these browsers. Why not load and initialise the script in IE Conditional Comments (unless Android <=2.3 is of major concern)?

For all other browsers it seems an extra script/performance cost.

Changing the license to Apache

After some digging in into the licenses I want to change the current SVGMagic license from GPL to Apache. As a formality I have to ask this to the contributers ( @craigfowler ). Do you agree with changing the license to Apache?

Plugin for CSS?

Will this plugin replace SVGs in HTML only as IMG src or also will do css? Seems liek it doesn't at this point. I am using background-image on few button graphics.

Thanks

Watch SRC replacements

Maybe an idea to add a method that will watch images for src replacement and create a SVG on-the-run when replaced.

Or a function that can be called to re-run svgmagic.

Can I force this to run?

I don't like the way any version of IE supports SVG's. Is there a way I can force the script to run and place png's for any version of IE, or any browser for that matter?

Background image on links

Just wanted to say this is an amazing plugin.. really great work!!

I have noticed if you are trying to replace SVG background image on links the replacement images don't show, I have tested in IE8 on W7 and XP. I only had a few links with bg images anyway so just replaced them via CSS.. great plugin again!!

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.