Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removeRasterImages skips some image formats #996

Open
rmarx opened this issue Jul 9, 2018 · 3 comments
Open

removeRasterImages skips some image formats #996

rmarx opened this issue Jul 9, 2018 · 3 comments

Comments

@rmarx
Copy link

rmarx commented Jul 9, 2018

The removeRasterImages plugin currently checks for the presence of raster images with a limited list of mime types:
item.hasAttrLocal('href', /(\.|image\/)(jpg|png|gif)/)

This misses certain types of mimetypes, mainly jpeg. A quick fix could be:
item.hasAttrLocal('href', /(\.|image\/)(jpg|jpeg|png|gif|webp|bmp|ico|jpe|jfif|tif|tiff)/)

I thought this would be too small a change to warrant a pull request, but if you want I could create one of those as well.

For other people wanting a quick fix without overriding the original plugin file add this to your list of plugins:

,{
	removeRasterImagesExtended: {
		name: 'removeRasterImagesExtended',
		type: 'perItem',
		params: {},
		fn: function(item) { 
			if (
				item.isElem('image') && 
				item.hasAttrLocal('href', /(\.|image\/)(jpg|jpeg|png|gif|webp|bmp|ico|jpe|jfif|tif|tiff)/)
			) {
				return false;
			}
		}
	}
}
@tigt
Copy link
Contributor

tigt commented Sep 14, 2018

Perhaps instead of testing for various bitmap file formats, it should instead test to see if the <image> is SVG? Sort of like this:

if (item.isElem('image') && 
    item.hasAttrLocal('href') && 
    ! item.hasAttrLocal('href', /(\.|image\/)svgz?/)) {
  return false;
}

The bitmap formats that browsers support can get pretty wacky (.jxr, .xbm) and are growing (.webp), so inverting the logic should be more maintainable. It would also guard against false positives from data: URIs randomly matching one of those substrings.

@kalnode
Copy link

kalnode commented Jun 1, 2021

I experienced this issue today. Simple jpg element embedded in a test svg. SVGO failed to remove it.

Maybe a different cause, but this seems like it's still an open issue?

This is with plugin 'removeRasterImages'. Also, to affirm, my plugin config was working because other plugins such as 'removeXMLProcInst' did have an affect when I toggled them.

UPDATE, CAUSE: Simply renaming "JPEG" to "JPG" in the image element, allowed it to be stripped by the plugin.
Looking at: https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js
It doesn't have "JPEG" as one of the listed formats to check for!


INPUT with embedded JPEG :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg id="svg5" width="793.7" height="1122.5" version="1.1" viewBox="0 0 210 297" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <g id="layer1">
  <circle id="path103" cx="158.14" cy="226.17" r="31.059" style="fill:#000000;stroke-width:.26458"/>
  <g id="g189">
   <circle id="path75" cx="97.664" cy="125.12" r="51.547" style="fill:#000000;stroke-width:.26458"/>
   <circle id="path105" cx="71.261" cy="216.04" r="23.074" style="fill:#000000;stroke-width:.26458"/>
  </g>
  <rect id="rect213" x="45.751" y="31.618" width="62.516" height="31.258" style="fill:#800000;stroke-width:.26458"/>
  <image id="image5686" x="107.54" y="82.775" width="81.622" height="81.622" preserveAspectRatio="none" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAA.................................................................

OUTPUT still includes JPEG !:

<svg width="793.7" height="1122.5" viewBox="0 0 210 297" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="158.14" cy="226.17" r="31.059" style="fill:#000;stroke-width:.26458"/><circle cx="97.664" cy="125.12" r="51.547" style="fill:#000;stroke-width:.26458"/><circle cx="71.261" cy="216.04" r="23.074" style="fill:#000;stroke-width:.26458"/><path style="fill:maroon;stroke-width:.26458" d="M45.751 31.618h62.516v31.258H45.751z"/><image x="107.54" y="82.775" width="81.622" height="81.622" preserveAspectRatio="none" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsK CwsNDhIQDQ4RDgsLEBYQERM.........................................

@SethFalco
Copy link
Member

The issue with JPEG files specifically will be resolved in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants