├── .gitattributes ├── .travis.yml ├── LICENSE.md ├── README.md ├── example ├── all.php └── imagesgen │ └── dummy.svg ├── src ├── client │ └── jquery.svgmagic.js └── server │ ├── converter.class.php │ └── index.php ├── svgmagic.jquery.json └── tests ├── domreplacer.js ├── requests.js └── time.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: javascript 2 | script: phantomjs ./tests/requests.js && phantomjs ./tests/domreplacer.js && phantomjs ./tests/time.js -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Apache License 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "{}" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright {yyyy} {name of copyright owner} 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | 205 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![ScreenShot](http://svgmagic.bitlabs.nl/svgmagic_tweakers.png)](http://svgmagic.bitlabs.nl) 2 | 3 | SVGMagic - Cross browser SVG 4 | ======== 5 | 6 | > **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](https://caniuse.com/#feat=mdn-svg_elements_g). 7 | > The plugin will continue to work, but no guarantees will be given over the API's availability. It has been a great ride!** 8 | 9 | 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. 10 | 11 | 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. 12 | 13 | You can find more information and demos on [our website](http://svgmagic.bitlabs.nl/). 14 | 15 | [![Build Status](https://travis-ci.org/dirkgroenen/SVGMagic.svg?branch=master)](https://travis-ci.org/dirkgroenen/SVGMagic) 16 | 17 | SVG... what/why? 18 | ------------ 19 | 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. 20 | 21 | Installation 22 | ------------ 23 | Just include the script in your header and call the plugin in your ```$(document).ready()``` 24 | ```code 25 | 26 | 31 | ``` 32 | SVGMagic also supports backgroundimages. You need to parse the div containing the backgroundimage including the ```backgroundimage``` option. 33 | ```code 34 | 35 | 42 | ``` 43 | 44 | Options 45 | ------- 46 | You can parse an options object into SVGMagic. Currently it supports the following options: 47 | ```code 48 | $('img').svgmagic({ 49 | temporaryHoldingImage: null, // Image that will appear when an image gets converted 50 | forceReplacements: false, // Force replacement in all browsers 51 | handleBackgroundImages: false, // Search the dom for CSS background images 52 | additionalRequestData: {}, // Add extra data to the ajax request. 53 | postReplacementCallback:null, // Function to run before replacement 54 | 55 | // New options 56 | remoteServerUri: 'https://bitlabs.nl/svgmagic/converter/3/', // Uri of the (remote) API script 57 | remoteRequestType: 'POST', // Request type for the API call 58 | remoteDataType: 'jsonp', // Data type for the API call 59 | debug: 'false' // Show usefull debug information in the console 60 | }); 61 | ``` 62 | 63 | ### additionalRequestData 64 | 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}```. 65 | 66 | Local development 67 | ----------------- 68 | 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. 69 | 70 | Support 71 | ------- 72 | The plugin is tested in Internet Explorer Version 7 and 8 (other browsers already support SVG files). 73 | 74 | Security / How it works 75 | -------- 76 | 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. 77 | 78 | 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. 79 | 80 | Demo 81 | ---- 82 | A demo of SVGMagic can be found on the [SVGMagic website](http://svgmagic.bitlabs.nl/). 83 | 84 | Known bugs 85 | ---------- 86 | - When many images need to be replaced the URL can get too long which will result in a server 414 error. 87 | 88 | Changelog 89 | ---------- 90 | ## 3.0.0 (2014-11-22) 91 | #### Client: 92 | New features: 93 | - SVGMagic can now return usefull debug information while replacing SVG images. 94 | - Added timeout to ajax request. Show debug information when timeout gets exceeded. 95 | 96 | New options: 97 | - debug: Show usefull debug information in the console 98 | 99 | Documentation: 100 | - Added changelog to the bottom of the README 101 | - Automatically return images over https when request was over https. 102 | 103 | #### Server: 104 | New features: 105 | - Fully rewrite of the server script. The server will now provide much more information about the convert process. 106 | - Response will contain the creation date of cached images. 107 | - Data images are now also cached. 108 | 109 | ## 2.4.0 (2014-08-01) 110 | 111 | New features: 112 | - Add extra post data to the ajax request 113 | - Now also finds data:image SVGs 114 | 115 | New options: 116 | - temporaryHoldingImage: replacement for preloader 117 | - forceReplacements: replacement for testmode 118 | - handleBackgroundImages: replacement for backgroundimage 119 | - additionalRequestData: send extra data to the server that replaces the SVGs for PNGs 120 | - postReplacementCallback: callback function that will be executed before replacement 121 | - remoteServerUri: the URI of the remote server that converts the images to PNG 122 | - remoteRequestType: set the type of the ajax request (post/get) 123 | - remoteDataType: the datatype sent to and received from the remote server 124 | 125 | Deprecated options: 126 | - preloader > temporaryHoldingImage 127 | - testmode > forceReplacements 128 | - backgroundimage > handleBackgroundImages 129 | - secure > additionalRequestData 130 | - callback > postReplacementCallback 131 | - dumpcache > additionalRequestData 132 | -------------------------------------------------------------------------------- /example/all.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Example of jQuery SVGMagic 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Background
16 | 17 | 18 | 19 | 34 | 35 | -------------------------------------------------------------------------------- /example/imagesgen/dummy.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 20 | 42 | 44 | 45 | 47 | image/svg+xml 48 | 50 | 51 | 52 | 53 | 54 | 59 | 62 | 73 | 75 | 81 | 84 | 86 | 94 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /src/client/jquery.svgmagic.js: -------------------------------------------------------------------------------- 1 | /* This jQuery plugin replaces SVG images in HTML 'img' elements (and optionally CSS in background-images) with 2 | PNG replacement images. This is accomplished by the use of a remote image replacement service. 3 | 4 | Copyright © 2013-2014 - Authors: 5 | 6 | * Dirk Groenen [Bitlabs Development - dirk@bitlabs.nl] Original author 7 | 8 | * Craig Fowler [CSF Software Limited - craig@csf-dev.com] Redesign and enhancements 9 | 10 | Version 3.0.1 11 | 12 | --- 13 | 14 | Copyright 2013-2014 Dirk Groenen 15 | 16 | Licensed under the Apache License, Version 2.0 (the "License"); 17 | you may not use this file except in compliance with the License. 18 | You may obtain a copy of the License at 19 | 20 | http://www.apache.org/licenses/LICENSE-2.0 21 | 22 | Unless required by applicable law or agreed to in writing, software 23 | distributed under the License is distributed on an "AS IS" BASIS, 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | See the License for the specific language governing permissions and 26 | limitations under the License. 27 | */ 28 | 29 | /** 30 | * Replaces matched SVG images in HTML 'img' elements (and optionally CSS background-image properties) with PNG 31 | * replacement images, generated by a remote server/service API. 32 | * 33 | * ------- 34 | * Options 35 | * ------- 36 | * * temporaryHoldingImage [string] This is a URI to an image which will be used as a "holding" image for the SVG 37 | * replacements until the URI of the appropriate PNG replacement has been retrieved from the 38 | * remote server. If set to null (the default behaviour) then no holding image will be used. 39 | * 40 | * * forceReplacements [boolean] If set to true then SVGMagic will replace SVG images even when the web browser 41 | * reports that it has native SVG support. If set to false (the default behaviour) then 42 | * replacement will only be performed when the web browser does not natively support SVG 43 | * images. 44 | * 45 | * * handleBackgroundImages [boolean] If set to true then SVGMagic will inspect the CSS background-image property of 46 | * matched elements. If the background image is an SVG then replacement will additionally 47 | * be performed upon the background image. If set to false (the default behaviour) then no 48 | * attempt will be made to detect and replace CSS background images. 49 | * 50 | * Note that even when this option is enabled, background images are only detected on matched 51 | * elements. No DOM search is performed to discover background images on (for example) child 52 | * nodes. 53 | * 54 | * * additionalRequestData [object] This is an object representing key/value pairs of information to send to the 55 | * remote server as part of the request. The default is an empty object (resulting in no 56 | * additional data being sent). 57 | * 58 | * This option is affected by the deprecated options 'secure' and 'dumpcache'. Presently, 59 | * in order to preserve backwards-compatibility, the keys 'secure' and 'dumpcache' will 60 | * be added to the request data if they are not already present. The values of these keys 61 | * will contain the respective values of those deprecated options. 62 | * 63 | * Additionally, the key 'svgsources' is reserved and will always be overwritten with an 64 | * array of the URIs to the SVG images to be replaced. For this reason, the key 'svgsources' 65 | * must not be used within the additional request data. 66 | * 67 | * * postReplacementCallback [function(replacedImages)] This is a callback function which is executed after all of the 68 | * image replacement URIs have been retrieved from the remote server and the replacements 69 | * have been performed. 70 | * 71 | * The parameter passed to this callback is a JavaScript array of objects which have the 72 | * following structure: 73 | * { 74 | * element [object] A reference to the jQuery element node upon which the image 75 | * replacement has been performed. 76 | * 77 | * isBackground [boolean] True if the the replacement was made upon a CSS 78 | * background-image. Otherwise this is an HTML 'img' element. 79 | * 80 | * originalUri [string] The original URI of the SVG image which has been replaced. 81 | * 82 | * replacementUri [string] The URI of the PNG replacement image. 83 | * } 84 | * 85 | * * remoteServerUri [string] This is the URI to the remote server API endpoint which converts SVG images into 86 | * PNGs and returns the list of URIs to those PNG replacements. It is set by default to: 87 | * http://svgmagic.bitlabs.nl/converter.php 88 | * 89 | * If you wish to host your own image-conversion server script/service then replace this with 90 | * the URI of your own API endpoint. 91 | * 92 | * * remoteRequestType [string] This is the type of HTTP request which will be used to communicate with the 93 | * remote server/service endpoint. By default this is set to 'POST'. Be careful if hosting 94 | * a service which accepts HTTP GET requests, as it could be vulnerable to CSRF attacks. 95 | * 96 | * * remoteDataType [string] This is the data-type sent to and received from the remote server. By default 97 | * this is set to 'json'. 98 | * 99 | * * replacementUriCreator [function(jQueryElement, originalUri, isBackground)] If provided, this option alters the 100 | * behaviour of SVGMagic, short-cutting out the initial call to the remote server, to 101 | * retrieve the URIs to the replacement PNG images. This is suitable for use in specialised 102 | * scenarios when the creation of the URI for the PNG replacements may be accomplished 103 | * entirely in JavaScript. The server hosting those replacement PNG images must also be 104 | * capable of serving the correct image with only a single GET URI (as the server will not 105 | * have been pre-notified of the path to the SVG source file). As such, it is most likely to 106 | * be used in hosted applications on a single domain, in which server-side logic has been 107 | * created to serve pre-ordained PNG replacement images. 108 | * 109 | * The parameters which this function receives are: 110 | * * [object] A reference to the jQuery object representing the HTML node on which the 111 | * replacement is to be made. 112 | * * [string] The URI of the original SVG image to be replaced. 113 | * * [boolean] True if the replacement is a CSS background-image, false if it is an HTML 114 | * image element. 115 | * 116 | * The function (if present) must return a string. This string indicates the URI to the 117 | * PNG replacement image. If null is returned then the replacement is skipped and the 118 | * original SVG image is left in-place. 119 | * 120 | * The default behaviour (in which this function is null/not-provided) uses a call to a 121 | * remote server/API endpoint containing a list of the URIs of the SVG images to be replaced, 122 | * The response is parsed for the URIs of the replacement PNG images. 123 | * 124 | * * debug [boolean] When enabled the script will provide usefull debug information. By default this option is 125 | * set to 'false'. Please remember to disable this option when in a production environment. 126 | * ------------------ 127 | * Deprecated options 128 | * ------------------ 129 | * * preloader [string or boolean] This is the URI to an image file which is used as a "holding" image 130 | * for your SVG images while the PNG replacements load from the remote server. If set to 131 | * boolean false (the default behaviour) then no such holding image is used. 132 | * 133 | * DEPRECATED: Use 'temporaryHoldingImage' instead. If 'temporaryHoldingImage' is set then 134 | * this option is ignored. 135 | * 136 | * * testmode [boolean] If set to true then the SVG replacement will be forced on all browsers, 137 | * including those which report that they support SVG natively. If set to false (the default 138 | * behaviour) then the SVG replacement will only be performed upon browsers which do not 139 | * support it natively. 140 | * 141 | * DEPRECATED: Use 'forceReplacements' instead. 142 | * 143 | * * secure [boolean] The value (true or false) of this option is passed to the remote SVG replacement 144 | * server as part of the HTTP POST parameters. Whilst the server may honour it or not, it is 145 | * intended that if the value is true, then the remote server will return a series of HTTPS 146 | * URIs (for the PNG replacement images). If set to false (the default behaviour) then the 147 | * remote server should return HTTP (non-secured) URIs. 148 | * 149 | * Regardless of the setting of this option - the initial call to the remote server will be 150 | * performed via unsecured HTTP. 151 | * 152 | * DEPRECATED: Use 'additionalRequestData' instead, adding data which the server will 153 | * interpret in order to serve HTTPS image URIs. The value of this option will be appended 154 | * to the additional request data before it is sent to the server. 155 | * 156 | * * callback [function()] An optional callback function which executes once all of the PNG replacement 157 | * image URIs have been retrieved from the remote server and all of the SVG images have had 158 | * their URIs replaced. This is not quite a callback which executes after the replacement 159 | * images have loaded. If the value is set to false (the default behaviour) then no 160 | * additional callback is executed. No parameters are passed to this callback. 161 | * 162 | * DEPRECATED: Use 'postReplacementCallback' instead. If 'postReplacementCallback' is set 163 | * then this option is ignored. 164 | * 165 | * * backgroundimage [boolean] If set to true then additional inspection will be performed upon all matched 166 | * elements in order to find a CSS background-image property. If such a property is found 167 | * then it will be included in the replacement process. If set to false (the default 168 | * behaviour) then no additional work will be performed to find background-images which are 169 | * SVG. 170 | * 171 | * DEPRECATED: Use 'handleBackgroundImages' instead. 172 | * 173 | * * dumpcache [boolean] The value (true or false) of this option is passed to the remote SVG replacement 174 | * server as part of the HTTP POST parameters. If set to true, then the server is requested 175 | * to clear any cached PNG copy of the replaced SVG image. This will result in the remote 176 | * server re-generating the PNG replacement. If set to false (the default behaviour) then 177 | * the remote server is expected to serve cached PNG replacement images where possible. 178 | * 179 | * DEPRECATED: Use 'additionalRequestData' instead, adding data which the server will 180 | * interpret as a request to drop its cache. The value of this option will be appended 181 | * to the additional request data before it is sent to the server. 182 | */ 183 | 184 | (function($) { 185 | $.fn.svgmagic = function(givenOptions) 186 | { 187 | var 188 | defaultOptions = { 189 | // Deprecated options 190 | preloader: false, 191 | testmode: false, 192 | secure: false, 193 | callback: false, 194 | backgroundimage: false, 195 | dumpcache: false, 196 | 197 | // Replacements for deprecated options 198 | temporaryHoldingImage: null, 199 | forceReplacements: false, 200 | handleBackgroundImages: false, 201 | additionalRequestData: {}, 202 | postReplacementCallback:null, 203 | 204 | // New options 205 | remoteServerUri: 'http://bitlabs.nl/svgmagic/converter/3/', 206 | remoteRequestType: 'POST', 207 | remoteDataType: 'jsonp', 208 | debug: false, 209 | 210 | // TODO: Implement this option 211 | replacementUriCreator: null 212 | }, 213 | untidyOptions = $.extend(defaultOptions, givenOptions), 214 | options = tidyOptions(untidyOptions), 215 | holdingImageTimeouts = {}, 216 | matchedNodes = this, 217 | images = [], 218 | imgElementName = 'img', 219 | srcAttributeName = 'src', 220 | backgroundImagePropertyName = 'background-image', 221 | urlMatcher = /^url\(["']?([^"'()]+)["']?\)$/, 222 | svgExtension = /\.svg$/, 223 | svgDataUri = /^data:image\/svg\+xml/, 224 | holdingImageTimeoutDuration = 500, 225 | VERSION = 3.0; 226 | 227 | /** 228 | * Check if the remoteServerUri has to be replaced with https 229 | */ 230 | if(window.location.protocol == "https:" || options.additionalRequestData.secure) 231 | { 232 | options.remoteServerUri = options.remoteServerUri.replace("http://", "https://"); 233 | 234 | } 235 | 236 | log(false, "Using " + options.remoteServerUri + " as remote server"); 237 | 238 | /** 239 | * The place where all magic starts 240 | */ 241 | if(shouldPerformReplacement(options)) 242 | { 243 | getReplacementUris(options, matchedNodes); 244 | } 245 | 246 | /** 247 | * Include JSON if it's not available in the browser 248 | */ 249 | if (typeof JSON == 'undefined') { 250 | if(typeof JSON!=="object"){JSON={}}(function(){"use strict";function f(e){return e<10?"0"+e:e}function quote(e){escapable.lastIndex=0;return escapable.test(e)?'"'+e.replace(escapable,function(e){var t=meta[e];return typeof t==="string"?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'}function str(e,t){var n,r,i,s,o=gap,u,a=t[e];if(a&&typeof a==="object"&&typeof a.toJSON==="function"){a=a.toJSON(e)}if(typeof rep==="function"){a=rep.call(t,e,a)}switch(typeof a){case"string":return quote(a);case"number":return isFinite(a)?String(a):"null";case"boolean":case"null":return String(a);case"object":if(!a){return"null"}gap+=indent;u=[];if(Object.prototype.toString.apply(a)==="[object Array]"){s=a.length;for(n=0;n 0) 345 | { 346 | if(replacementFunction && typeof replacementFunction == 'function') 347 | { 348 | for(var i = 0; i < images.length; i++) 349 | { 350 | var image = images[i]; 351 | image.replacementUri = replacementFunction(image.element, image.originalUri, image.isBackground); 352 | } 353 | 354 | performReplacements(opts); 355 | } 356 | else 357 | { 358 | getReplacementUrisFromRemoteService(opts); 359 | } 360 | } 361 | } 362 | 363 | /** 364 | * Gets all of the replacement image URIs from a remote server using an API call. 365 | */ 366 | function getReplacementUrisFromRemoteService(opts) 367 | { 368 | var 369 | sources = [], 370 | data = {}; 371 | 372 | for(var i = 0; i < images.length; i++) 373 | { 374 | sources.push(images[i].originalUri); 375 | } 376 | 377 | // Get baseurl 378 | var baseUrl = window.location.href.split('/'); 379 | $.extend(data, opts.additionalRequestData, { svgsources: sources, version: VERSION, origin: baseUrl[2] }); 380 | 381 | $.ajax({ 382 | dataType: opts.remoteDataType, 383 | method: opts.remoteRequestType, 384 | url: opts.remoteServerUri, 385 | data: data, 386 | timeout: 3000, 387 | success: function(response, textStatus, jqXHR) { 388 | for(var i = 0; i < images.length; i++) 389 | { 390 | var image = images[i], 391 | responseUri = response.images[i].image; 392 | 393 | image.replacementUri = responseUri; 394 | image.error = response.images[i].error; 395 | image.responseMsg = response.images[i].msg; 396 | image.filename = response.images[i].filename; 397 | } 398 | 399 | performReplacements(opts); 400 | }, 401 | error: function(){ 402 | log(true, "The request took longer than 3 seconds to complete. No image were replaced. Use the developer tools to check wheter the server responded with an error.") 403 | } 404 | }); 405 | } 406 | 407 | /** 408 | * Performs image replacements using the result from the remote replacement service. 409 | */ 410 | function performReplacements(opts) 411 | { 412 | for(var i = 0; i < images.length; i++) 413 | { 414 | var image = images[i], newUri = image.replacementUri; 415 | 416 | if(!newUri) 417 | { 418 | log(true, image.filename + ": No new url received"); 419 | continue; 420 | } 421 | else if(image.error) 422 | { 423 | log(true, image.filename + ": " + image.responseMsg); 424 | continue; 425 | } 426 | else if(!image.isBackground) 427 | { 428 | log(false, image.filename + ": Image replaced. Server responded with: " + image.responseMsg); 429 | 430 | if(opts.temporaryHoldingImage) 431 | { 432 | clearTimeout(holdingImageTimeouts[i]); 433 | } 434 | image.element.attr(srcAttributeName, newUri); 435 | } 436 | else 437 | { 438 | log(false, image.filename + ": Background image replaced. Server responded with: " + image.responseMsg); 439 | image.element.css(backgroundImagePropertyName, 'url("' + newUri + '")'); 440 | } 441 | } 442 | 443 | if(opts.postReplacementCallback && typeof opts.postReplacementCallback == 'function') 444 | { 445 | opts.postReplacementCallback(images); 446 | } 447 | } 448 | 449 | /** 450 | * Tidies up an object containing options for this plugin. Takes any deprecated options (where set) and writes 451 | * them into the equivalent replacement option. 452 | */ 453 | function tidyOptions(originalOptions) 454 | { 455 | if(!originalOptions.temporaryHoldingImage && originalOptions.preloader && typeof originalOptions.preloader == 'string') 456 | { 457 | originalOptions.temporaryHoldingImage = originalOptions.preloader; 458 | } 459 | 460 | if(originalOptions.testmode && typeof originalOptions.testmode == 'boolean') 461 | { 462 | originalOptions.forceReplacements = true; 463 | } 464 | 465 | if(!originalOptions.postReplacementCallback && originalOptions.callback && typeof originalOptions.callback == 'function') 466 | { 467 | originalOptions.postReplacementCallback = function(replacedImages) 468 | { 469 | originalOptions.callback(); 470 | }; 471 | } 472 | 473 | if(!originalOptions.additionalRequestData['secure']) 474 | { 475 | originalOptions.additionalRequestData.secure = originalOptions.secure; 476 | } 477 | if(!originalOptions.additionalRequestData['dumpcache']) 478 | { 479 | originalOptions.additionalRequestData.dumpcache = originalOptions.dumpcache; 480 | } 481 | 482 | return originalOptions; 483 | } 484 | 485 | /* 486 | * Create console log message of option has been enabled 487 | */ 488 | function log(error, msg){ 489 | if(options.debug){ 490 | if(error) 491 | console.error(msg) 492 | else 493 | console.log(msg); 494 | } 495 | } 496 | 497 | // Return the original jQuery object, standard jQuery behaviour. 498 | return this; 499 | }; 500 | }(jQuery)); 501 | -------------------------------------------------------------------------------- /src/server/converter.class.php: -------------------------------------------------------------------------------- 1 | origin = $origin; 50 | $this->imageurls = $imageurls; 51 | $this->domain = $domain; 52 | $this->version = $version; 53 | 54 | // Setup default response 55 | $this->response = array("error" => false, "msg" => "Images succesfully converted", "images" => array()); 56 | 57 | $this->initialize(); // Initialize site directory 58 | } 59 | 60 | /* 61 | * Initialize the (new) site before converting the images 62 | */ 63 | private function initialize() 64 | { 65 | // Set site directory 66 | $this->sitedirectory = $this->rootdirectory . "/" . $this->origin; 67 | 68 | // Check if file exists. If not: create directory 69 | if(!file_exists($this->rootdirectory . "/" . $this->origin)){ 70 | mkdir($this->sitedirectory, 0777); 71 | } 72 | 73 | $this->publicurl = $this->domain . "/images/results/" . $this->origin; 74 | } 75 | 76 | /* 77 | * Start the image conversion 78 | */ 79 | public function start($dumpcache) 80 | { 81 | // Loop through all the images and add the response to the global response 82 | foreach($this->imageurls as $srcimage){ 83 | $image = $this->convert($srcimage, $dumpcache); 84 | 85 | // Check if response contains an error 86 | if($image["error"]){ 87 | $this->response["error"] = true; 88 | $this->response["msg"] = "Not all images have been converted."; 89 | } 90 | 91 | // Add response to array 92 | if($this->version >= 3) 93 | $this->response["images"][] = $image; 94 | else 95 | $this->response["results"][] = array("url" => $image["image"]); 96 | } 97 | 98 | return $this->response; 99 | } 100 | 101 | 102 | /* 103 | * Start the image conversion 104 | * @param String $image 105 | * @param boolean $force 106 | * @return Array response 107 | */ 108 | private function convert($srcimage, $force) 109 | { 110 | $response = array("image" => $srcimage, "error" => false, "msg" => "Image converted succesfully"); 111 | 112 | // Check if image is datauri or url 113 | if(preg_match("/^data:image\/svg\+xml/", $srcimage) == 1){ 114 | $response["type"] = "datauri"; 115 | $filename = md5($srcimage); 116 | $response["filename"] = $filename; 117 | 118 | // Prepare the new $srcimage and secode 119 | $srcimage = str_replace("data:image/svg+xml;base64,", "", $srcimage); 120 | $srcimage = base64_decode($srcimage); 121 | } 122 | else{ 123 | $response["type"] = "url"; 124 | 125 | // Get the filename from the url 126 | $filename = explode('/', $srcimage); 127 | $filename = array_pop($filename); 128 | $filename = str_replace('.svg' ,'' , $filename); 129 | 130 | $response["filename"] = $filename; 131 | } 132 | 133 | // Check if file already exists 134 | if(file_exists($this->sitedirectory . '/' . $filename . '.png') && $force != "true"){ 135 | $lastmodified = filemtime($this->sitedirectory . '/' . $filename . '.png'); 136 | 137 | $response["image"] = $this->publicurl . "/" . $filename . ".png"; 138 | $response["msg"] = "Image retrieved from cache. Saved at: " . date("Y-m-d H:i:s", $lastmodified); 139 | return $response; // Kill and return response 140 | } 141 | 142 | // Check if we can reach the provided URL 143 | // return the response when the image can't be reached 144 | if($response["type"] == "url"){ 145 | $check = $this->checksrcurl($srcimage); 146 | 147 | if($check > 300 || $check == false){ 148 | $response["error"] = true; 149 | $response["msg"] = "Couldn't download image from provided URL"; 150 | 151 | return $response; // Kill and return response 152 | } 153 | } 154 | 155 | // download the image if given srcimage is an url 156 | if($response["type"] == "url") 157 | $srcimage = file_get_contents($srcimage); 158 | 159 | // Convert $srcimage to PNG 160 | try{ 161 | $desimage = new Imagick(); 162 | $desimage->setBackgroundColor(new ImagickPixel('transparent')); 163 | $desimage->readImageBlob( $srcimage ); 164 | $desimage->setImageFormat("png32"); 165 | $desimage->setImageCompressionQuality(100); 166 | 167 | // Remove if dumpcache is enabled 168 | if($force && file_exists($this->sitedirectory . '/' . $filename . '.png')) 169 | unlink($this->sitedirectory . '/' . $filename . '.png'); 170 | 171 | // Save the file 172 | file_put_contents($this->sitedirectory . '/' . $filename . '.png', $desimage); 173 | 174 | // Set the URL where the PNG can be viewed from 175 | $response["image"] = $this->publicurl . "/" . $filename . ".png"; 176 | } 177 | catch(ImagickException $e){ 178 | $response["error"] = true; 179 | $response["msg"] = $e->getMessage(); 180 | } 181 | 182 | // Return response 183 | return $response; 184 | } 185 | 186 | /* 187 | * Return the response code for the give URL. Returns false if url is not available. 188 | * 189 | * @param string url 190 | * @return mixed responsecode 191 | */ 192 | private function checksrcurl($url) 193 | { 194 | $headers = get_headers($url); 195 | return substr($headers[0], 9, 3); 196 | } 197 | 198 | } 199 | 200 | ?> -------------------------------------------------------------------------------- /src/server/index.php: -------------------------------------------------------------------------------- 1 | . 25 | */ 26 | ?> 27 | start($dumpcache); 56 | 57 | /* 58 | * Set status code if needed 59 | */ 60 | if($result["error"] == true) 61 | http_response_code(206); 62 | 63 | /* 64 | * Show results 65 | * Wrap the $result in a callback if provided 66 | */ 67 | if(isset($_GET['callback'])) 68 | echo $_GET['callback'] . '('. json_encode($result) . ')'; 69 | else 70 | echo json_encode($result); 71 | 72 | ?> -------------------------------------------------------------------------------- /svgmagic.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svgmagic", 3 | "title": "jQuery SVGMagic", 4 | "description": "A simple jQuery plugin that creates a fallback for .SVG images (backgroundimage supported to!). In case your browser doesn't support .SVG images it will create a .PNG file and replace the .SVG with it. This means that you don't have to create two versions of your images to support IE8 and lower.", 5 | "keywords": [ 6 | "SVG", 7 | "fallback", 8 | "image" 9 | ], 10 | "version": "3.0.0", 11 | "author": { 12 | "name": "Bitlabs Development", 13 | "url": "https://bitlabs.nl" 14 | }, 15 | "maintainers": [ 16 | { 17 | "name": "Dirk Groenen", 18 | "email": "dirk@bitlabs.nl", 19 | "url": "https://github.com/dirkgroenen" 20 | } 21 | ], 22 | "licenses": [ 23 | { 24 | "type": "Apache", 25 | "url": "https://github.com/dirkgroenen/SVGMagic/blob/master/LICENSE.md" 26 | } 27 | ], 28 | "homepage": "http://dirkgroenen.github.io/SVGmagic", 29 | "docs": "http://dirkgroenen.github.io/SVGmagic", 30 | "dependencies": { 31 | "jquery": ">=1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/domreplacer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * This requests test will open the all.html test page and check if the images have been replaced. 3 | */ 4 | 5 | // constants 6 | var EXIT_SUCCESS = 0, 7 | EXIT_FAILURE = 1, 8 | EXIT_ERROR = 2; 9 | 10 | var checks = [ 11 | { 12 | domid: "n-one", 13 | background: false, 14 | svg: false 15 | }, 16 | { 17 | domid: "n-two", 18 | background: false, 19 | svg: false 20 | }, 21 | { 22 | domid: "b-one", 23 | background: true, 24 | svg: false 25 | }, 26 | { 27 | domid: "d-one", 28 | background: false, 29 | svg: false 30 | } 31 | ]; 32 | 33 | var pageurl = "http://bitlabs.nl/svgmagic/tests/all.php?dumpcache=false"; 34 | var page = require('webpage').create(); 35 | var returnerror = false; 36 | 37 | page.open(pageurl, function(status) { 38 | console.log("==================="); 39 | console.log("Start checking dom"); 40 | console.log("==================="); 41 | 42 | // Check for error 43 | if (status !== 'success') { 44 | console.log('Unable to do request. Exit.'); 45 | 46 | phantom.exit(EXIT_ERROR); 47 | } 48 | else { 49 | 50 | // Do all checks 51 | for(var x = 0; x < checks.length; x++){ 52 | console.log("\nEvaluating page for #" + checks[x].domid + " src"); 53 | 54 | var getsrc = page.evaluate(function(check){ 55 | if(check.background) 56 | return $("#" + check.domid).css("background-image"); 57 | else 58 | return $("#" + check.domid).attr("src"); 59 | }, checks[x]); 60 | 61 | if(getsrc.match("(.png){1}")){ 62 | console.log("Check #" + checks[x].domid + " succes. Image got replaced by: " + getsrc); 63 | checks[x].svg = true; 64 | } 65 | else { 66 | console.log("Check #" + checks[x].domid + " failed"); 67 | returnerror = true; 68 | } 69 | 70 | } 71 | 72 | // Get the total load time 73 | var runtime = page.evaluate(function(){ 74 | return $("#replacetime").text(); 75 | }); 76 | 77 | console.log("\nTotal runtime: " + runtime + "ms"); 78 | 79 | phantom.exit((returnerror) ? EXIT_FAILURE : EXIT_SUCCESS); 80 | 81 | } 82 | }); 83 | -------------------------------------------------------------------------------- /tests/requests.js: -------------------------------------------------------------------------------- 1 | /* 2 | * This requests test will send two requests to the SVGMagic API and validate the responsed data for any errors. 3 | * The validation only exists out checking if the error variable has been set to true 4 | */ 5 | 6 | // constants 7 | var EXIT_SUCCESS = 0, 8 | EXIT_FAILURE = 1, 9 | EXIT_ERROR = 2; 10 | 11 | // vars 12 | var apiurl = 'http://bitlabs.nl/svgmagic/converter/3/'; 13 | var batches = []; 14 | 15 | // Build data object for version 3 with a normal and data image 16 | var v3data = "dumpcache=true&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_1.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_2.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_3.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_4.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_5.svg&svgsources[]=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8%2BCjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iCiAgIHhtbG5zOmNjPSJodHRwOi8vY3JlYXRpdmVjb21tb25zLm9yZy9ucyMiCiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIKICAgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxuczpzb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lY3QzMDEwIgogICAgICAgICAgICAgICBzdHlsZT0iZmlsbDojZmZmZmZmO2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTojZmZmZmZmO3N0cm9rZS13aWR0aDowLjE1NjtzdHJva2UtbGluZWNhcDpidXR0O3N0cm9rZS1saW5lam9pbjpyb3VuZDtzdHJva2UtbWl0ZXJsaW1pdDo0O3N0cm9rZS1vcGFjaXR5OjA7c3Ryb2tlLWRhc2hhcnJheTpub25lIiAvPgogICAgICAgICAgICA8cmVjdAogICAgICAgICAgICAgICB0cmFuc2Zvcm09InNjYWxlKC0xLC0xKSIKICAgICAgICAgICAgICAgcnk9IjAuNDc2MDAwMDEiCiAgICAgICAgICAgICAgIHk9Ii02Ny4wMjY5MDkiCiAgICAgICAgICAgICAgIHg9Ii04Ni4yODgwNCIKICAgICAgICAgICAgICAgaGVpZ2h0PSIzMy42MDk5ODUiCiAgICAgICAgICAgICAgIHdpZHRoPSIzMy42MDk5ODUiCiAgICAgICAgICAgICAgIGlkPSJyZWN0MzAxMC0xIgogICAgICAgICAgICAgICBzdHlsZT0iZmlsbDojZmZmZmZmO2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTojZmZmZmZmO3N0cm9rZS13aWR0aDowLjQyMjQ3MDI3O3N0cm9rZS1saW5lY2FwOmJ1dHQ7c3Ryb2tlLWxpbmVqb2luOnJvdW5kO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLW9wYWNpdHk6MDtzdHJva2UtZGFzaGFycmF5Om5vbmUiIC8%2BCiAgICAgICAgICA8L2c%2BCiAgICAgICAgPC9nPgogICAgICA8L2c%2BCiAgICA8L2c%2BCiAgPC9nPgo8L3N2Zz4K&version=3&origin=travisCI"; 17 | 18 | // Build data for v2 19 | var v2data = "dumpcache=true&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_1.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_2.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_3.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_4.svg&svgsources[]=http%3A%2F%2Fdev.svgmagic.bitlabs.nl%2Fstyle%2Fresizeicon_5.svg&svgsources[]=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8%2BCjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iCiAgIHhtbG5zOmNjPSJodHRwOi8vY3JlYXRpdmVjb21tb25zLm9yZy9ucyMiCiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIKICAgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxuczpzb2RpcG9kaT0iaHR0cDovL3NvZGlwb2RpLnNvdXJjZWZvcmdlLm5ldC9EVEQvc29kaXBvZGktMC5kdGQiCiAgIHhtbG5zOmlua3NjYXBlPSJodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy9uYW1lc3BhY2VzL2lua3NjYXBlIgogICB3aWR0aD0iMTA4IgogICBoZWlnaHQ9IjEwOCIKICAgaWQ9InN2ZzIiCiAgIHZlcnNpb249IjEuMSIKICAgaW5rc2NhcGU6dmVyc2lvbj0iMC40OC40IHI5OTM5IgogICBzb2RpcG9kaTpkb2NuYW1l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ICA8cGF0aAogICAgICAgICAgIHNvZGlwb2RpOm5vZGV0eXBlcz0iY2NjY2MiCiAgICAgICAgICAgc3R5bGU9Im9wYWNpdHk6MC4zO2ZpbGw6IzI0MWYxYztlbmFibGUtYmFja2dyb3VuZDpuZXciCiAgICAgICAgICAgaW5rc2NhcGU6Y29ubmVjdG9yLWN1cnZhdHVyZT0iMCIKICAgICAgICAgICBkPSJNIDMwLjgwMDkwOCw0Ny40MDQ4OTYgNTUuNzE5Nzg1LDM3LjIxNDg3NCA1NS42OTIxNzIsNzAuNzYxMzM5IDMwLjk0MDM0Niw1OS43MzM1MyB6IgogICAgICAgICAgIGlkPSJwYXRoMjk4OSIgLz4KICAgICAgICA8ZwogICAgICAgICAgIHRyYW5zZm9ybT0idHJhbnNsYXRlKDIuNjQ0ODkyMSwzLjc3NDA4MjIpIgogICAgICAgICAgIGlkPSJnMzgwNSI%2BCiAgICAgICAgICA8ZwogICAgICAgICAgICAgaWQ9ImczODAxIj4KICAgICAgICAgICAgPHJlY3QKICAgICAgICAgICAgICAgcnk9IjAuMzA4IgogICAgICAgICAgICAgICB5PSI0My41ODI5NTgiCiAgICAgICAgICAgICAgIHg9IjE2LjI4ODk0IgogICAgICAgICAgICAgICBoZWlnaHQ9IjEyLjQxMDcxNCIKICAgICAgICAgICAgICAgd2lkdGg9IjEyLjQxMDcxNCIKICAgICAgICAgICAgICAgaWQ9InJlY3QzMDEwIgogICAgICAgICAgICAgICBzdHlsZT0iZmlsbDojZmZmZmZmO2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTojZmZmZmZmO3N0cm9rZS13aWR0aDowLjE1NjtzdHJva2UtbGluZWNhcDpidXR0O3N0cm9rZS1saW5lam9pbjpyb3VuZDtzdHJva2UtbWl0ZXJsaW1pdDo0O3N0cm9rZS1vcGFjaXR5OjA7c3Ryb2tlLWRhc2hhcnJheTpub25lIiAvPgogICAgICAgICAgICA8cmVjdAogICAgICAgICAgICAgICB0cmFuc2Zvcm09InNjYWxlKC0xLC0xKSIKICAgICAgICAgICAgICAgcnk9IjAuNDc2MDAwMDEiCiAgICAgICAgICAgICAgIHk9Ii02Ny4wMjY5MDkiCiAgICAgICAgICAgICAgIHg9Ii04Ni4yODgwNCIKICAgICAgICAgICAgICAgaGVpZ2h0PSIzMy42MDk5ODUiCiAgICAgICAgICAgICAgIHdpZHRoPSIzMy42MDk5ODUiCiAgICAgICAgICAgICAgIGlkPSJyZWN0MzAxMC0xIgogICAgICAgICAgICAgICBzdHlsZT0iZmlsbDojZmZmZmZmO2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTojZmZmZmZmO3N0cm9rZS13aWR0aDowLjQyMjQ3MDI3O3N0cm9rZS1saW5lY2FwOmJ1dHQ7c3Ryb2tlLWxpbmVqb2luOnJvdW5kO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLW9wYWNpdHk6MDtzdHJva2UtZGFzaGFycmF5Om5vbmUiIC8%2BCiAgICAgICAgICA8L2c%2BCiAgICAgICAgPC9nPgogICAgICA8L2c%2BCiAgICA8L2c%2BCiAgPC9nPgo8L3N2Zz4K&origin=travisCI"; 20 | 21 | /* 22 | * Run a post request and return the collected data 23 | * 24 | * @return object batch 25 | */ 26 | function runTest(batch){ 27 | var page = require('webpage').create(); 28 | 29 | page.open(apiurl + "?" + batch.testData, function(status) { 30 | // Check for error 31 | if (status !== 'success') { 32 | console.log('Unable to do request'); 33 | 34 | // Set vars 35 | batch.error = true; 36 | batch.finished = true; 37 | } 38 | else { 39 | // Get response 40 | var response = JSON.parse(page.plainText); 41 | 42 | // Save response in batch and start validating 43 | batch.resultData = response; 44 | validateTestResults(batch); 45 | } 46 | }); 47 | }; 48 | 49 | /* 50 | * Run a post request and return the collected data 51 | * 52 | * @param object batch 53 | */ 54 | function validateTestResults(batch){ 55 | // Loop through check batches 56 | if(batch.resultData.error == true){ 57 | batch.error = true; 58 | } 59 | else{ 60 | batch.success = true; 61 | } 62 | 63 | // Set batch to finished 64 | batch.finished = true; 65 | 66 | // Call next test 67 | runNextTest(); 68 | }; 69 | 70 | /* 71 | * Run the next test in the batch 72 | */ 73 | function runNextTest(){ 74 | // Loop through tests 75 | for(var x = 0; x < batches.length; x++){ 76 | // check if test hasn't been started yet 77 | if(!batches[x].started){ 78 | batches[x].started = true; 79 | runTest(batches[x]); 80 | 81 | // Stop the looping 82 | return; 83 | } 84 | } 85 | 86 | // At this point all tests have been done and batch results need to be finished 87 | validateBatchData(); 88 | }; 89 | 90 | /* 91 | * Validate all results in the batches to check if we had a succesfull test 92 | */ 93 | function validateBatchData(){ 94 | for(var x = 0; x < batches.length; x++){ 95 | 96 | console.log("\n==================="); 97 | console.log("Validating test: " + batches[x].name); 98 | console.log("==================="); 99 | 100 | if(batches[x].started && batches[x].finished){ 101 | if(batches[x].error){ 102 | console.log("Failure in test: " + batches[x].name); 103 | console.log("Server responded with: " + batches[x].resultData.msg); 104 | console.log("\nBatch data: " + JSON.stringify(batches[x])); 105 | 106 | phantom.exit(EXIT_FAILURE); 107 | } 108 | else if(!batches[x].success){ 109 | console.log("Error in test " + batches[x].name + " results. No success or error reported.") 110 | phantom.exit(EXIT_ERROR); 111 | } 112 | else{ 113 | console.log("Test " + batches[x].name + " success"); 114 | } 115 | 116 | } 117 | else{ 118 | console.log("Not all tests have been finished, calling next test again"); 119 | 120 | runNextTest(); 121 | return; 122 | } 123 | } 124 | 125 | phantom.exit(EXIT_SUCCESS); 126 | }; 127 | 128 | 129 | /* 130 | * Add test to batch 131 | * 132 | * @param string name 133 | * @param string testData 134 | */ 135 | function addTest(name, testData){ 136 | batches.push({ 137 | name: name, 138 | testData: testData, 139 | resultData: {}, 140 | success: false, 141 | started: false, 142 | finished: false 143 | }); 144 | }; 145 | 146 | 147 | /* 148 | * Init the testing 149 | */ 150 | function initAndStart(){ 151 | console.log("==========================="); 152 | console.log("Starting API requests test"); 153 | console.log("==========================="); 154 | 155 | addTest("v2", v2data); 156 | addTest("v3", v3data); 157 | 158 | runNextTest(); 159 | }; 160 | 161 | // Run tests 162 | initAndStart(); 163 | 164 | 165 | -------------------------------------------------------------------------------- /tests/time.js: -------------------------------------------------------------------------------- 1 | /* 2 | * This time test will open the test page twice. Once with dumpcache enabled and once with dumpcache disabled. 3 | * Test will only pass when the time is lower than the given var 4 | */ 5 | 6 | // constants 7 | var EXIT_SUCCESS = 0, 8 | EXIT_FAILURE = 1, 9 | EXIT_ERROR = 2; 10 | 11 | // vars 12 | var pageurl = 'http://bitlabs.nl/svgmagic/tests/all.php'; 13 | var tests = [ 14 | { 15 | dumpcache: true, 16 | time: 0, 17 | maxtime: 2500, 18 | success: false, 19 | finished: false, 20 | error: false, 21 | started: false 22 | }, 23 | { 24 | dumpcache: false, 25 | time: 0, 26 | maxtime: 1500, 27 | success: false, 28 | finished: false, 29 | error: false, 30 | started: false 31 | } 32 | ]; 33 | 34 | /* 35 | * Run a post request and return the collected data 36 | * 37 | * @return object test 38 | */ 39 | function runTest(test){ 40 | var page = require('webpage').create(); 41 | 42 | page.open(pageurl + "?dumpcache=" + test.dumpcache, function(status) { 43 | // Check for error 44 | if (status !== 'success') { 45 | console.log('Unable to do request'); 46 | 47 | // Set vars 48 | test.finished = true; 49 | test.error = true; 50 | } 51 | else { 52 | // Get response 53 | var time = page.evaluate(function(){ 54 | return $("#replacetime").text(); 55 | }); 56 | 57 | // Save response in batch and start validating 58 | test.time = time; 59 | validateTestResult(test); 60 | } 61 | }); 62 | }; 63 | 64 | /* 65 | * Run a post request and return the collected data 66 | * 67 | * @param object test 68 | */ 69 | function validateTestResult(test){ 70 | // Loop through check batches 71 | if(test.time < test.maxtime) 72 | test.success = true; 73 | 74 | // Set batch to finished 75 | test.finished = true; 76 | 77 | // Call next test 78 | runNextTest(); 79 | }; 80 | 81 | /* 82 | * Run the next test in the tests 83 | */ 84 | function runNextTest(){ 85 | // Loop through tests 86 | for(var x = 0; x < tests.length; x++){ 87 | // check if test hasn't been started yet 88 | if(!tests[x].started){ 89 | tests[x].started = true; 90 | runTest(tests[x]); 91 | 92 | // Stop the looping 93 | return; 94 | } 95 | } 96 | 97 | // At this point all tests have been done and batch results need to be finished 98 | validateBatchData(); 99 | }; 100 | 101 | /* 102 | * Validate all results in the batches to check if we had a succesfull test 103 | */ 104 | function validateBatchData(){ 105 | for(var x = 0; x < tests.length; x++){ 106 | 107 | console.log("\n==================="); 108 | console.log("Validating test " + x); 109 | console.log("==================="); 110 | 111 | if(tests[x].started && tests[x].finished){ 112 | if(tests[x].error){ 113 | console.log("Error in test: " + x); 114 | 115 | phantom.exit(EXIT_ERROR); 116 | } 117 | 118 | if(!tests[x].success){ 119 | console.log("Failure in test " + x + " results. Time should have been below: " + tests[x].maxtime + ", but was: " + tests[x].time + "."); 120 | phantom.exit(EXIT_ERROR); 121 | } 122 | else{ 123 | console.log("Test " + x + " success (dumpcache: " + tests[x].dumpcache + "). Time was " + tests[x].time + ". Max runtime was set on: " + tests[x].maxtime); 124 | } 125 | 126 | } 127 | else{ 128 | console.log("Not all tests have been finished, calling next test again"); 129 | 130 | runNextTest(); 131 | return; 132 | } 133 | } 134 | 135 | phantom.exit(EXIT_SUCCESS); 136 | }; 137 | 138 | /* 139 | * Init the testing 140 | */ 141 | function initAndStart(){ 142 | console.log("==========================="); 143 | console.log("Starting time test"); 144 | console.log("==========================="); 145 | 146 | runNextTest(); 147 | }; 148 | 149 | // Run tests 150 | initAndStart(); 151 | 152 | 153 | --------------------------------------------------------------------------------