I just want to knock out some quick SVGs in Photoshop and get back to playing.
Note: there are different ways to accomplish the same thing. There are certainly other tools that can convert raster images to SVG files. Here I focusing on simple images and using Photoshop... think icons for CSS.
For illustration work or more complicated SVG images you should use the best tools for your task. (i.e.: Illustrator, etc.)
For the sake of those who may be new, lets review some over-simplified concepts, but... TL/DR: If you landed here just searching for a Photoshop / SVG solution you can skip to the next section(s).
- SVG: means Scalable Vector Graphics and SVG is an image format that stores paths, shapes and color in terms of vector-based instructions (i.e.: paths or instructions for some other program to redraw the image) without loss of detail at any scale.
- Using formats such as: PNG, JPG or GIF - images are stored in various sizes (resolution) and depth of color or opacity, on a per-pixel basis. That is, non-SVG image formats are typically created with a pre-determined size in mind. One benefit of those formats is that you can represent detailed or complicated images (such as a photograph, painting or flip-book style animation) but at the expense of requiring a pre-determined / fixed size.
- Resizing non-SVG images when viewing or editing them can affect image quality because the original raster image data (organized into sequential rows of contiguous pixels) needs to be shifted and approximated to fit into the available pixels. This approximation is necessary to account for pixels that are to be added or replaced when scaling the image up or down. An example of this guess-work by Photoshop can be seen below.
Alright that's enough review for now. Lets get back to Photoshop.
Scenario 1: Exporting Simple Shapes as SVG from Photoshop:
As of the time of this writing (December, 2020), Photoshop has had support for simple paths and shapes for quite some time. You can learn more about creating vector shapes in Photoshop from Adobe: here.
Create a new photoshop document with the canvas size you would like to have your SVG initially set to and then add a shape to it. Assuming you have a non-raster path / shape in your photoshop document, you can choose:
File -> Export -> Export As
Once the export dialog opens, you will need to change the Format to "SVG" and then press Export.
You should be prompted to save your .SVG file somewhere on your hard-drive. Inspect the contents of that SVG file in a text editor and you should see path / shape, etc. instructions that other programs will use to render the SVG image.
Yours will probably look different than mine depending on what shapes, canvas size, etc. you created before exporting your SVG.
Caution: If your SVG file contains a base64 encoded string preceded by
data:img/png;base64, or similar then you probably tried to export a rasterized image layer instead of a shape / path layer. What you have done in that case is take your rasterized sequence of pixel values and store them in an SVG wrapper. This is not what you want.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400" viewBox="0 0 400 400"> <image id="Rectangle_Raster" data-name="Rectangle Raster" x="94" y="100" width="201" height="201" xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMkAAADJCAYAAACJxhYFAAAGLUlEQVR4nO3d3avlVR3H8c85jhMNgwbRYDgzyoylE5R6IT6QIAap3YQWBfUnaF33DwQRBl0o3gxWlFBeKCaUdeFNUOmNzo1PqDTOSKD... blah blah blah etc..."/> </svg>
You can usually tell if you are working on a shape layer if it has the path symbol on the lower right corner of the layer icon. This is found in the Layers panel. Here is an example comparing the two layer types.
Alternatively, if you right-click on a layer and "Rasterize layer" is greyed out then you probably need to go back and create your shape layer first. There are other hints all over Photoshop but these are two I would start with.
What if you don't have a shape layer but you still want to export it as an SVG? Don't give up! Read on friends.
Scenario 2: Convert your existing rasterized shape to a path from selection:
Suppose you have a rasterized image that contains the shape of an icon that you want to export. Perhaps you are adding some custom icons to your website and want to go the SVG rout instead of exporting rasterized image files.
First you will want to create a selection of the solid color areas of this raster image using the selection tools. I prefer to use the magic wand tool and hold shift to select multiple areas of the icon as needed. You may need to adjust your selection tool settings to tighten things up.
You can add a solid background color that contrasts with your icon color and then enable "sample all layers". This combined with a lower tolerance value should really really tighten up the selection. In my case, this is just a demo focusing on the SVG exports so I am going to keep it simple.
With the selection still active choose:
Paths -> Make Work Path from Selection (icon on the lower bar of the Paths panel)
This will create a rough shape path of your selection and you may need to tweak this path once it is created to clean up anything you are not happy with. Add new anchor points, move them around, tweak the curve handles, etc. Assuming you are keeping it simple then this should not be too much of an issue.
For me, the path is created with a default name. I just double-clicked on it and renamed mine to "Smile Path".
Select the path layer you just created under the Paths tab and choose:
Edit -> Define Custom Shape...
This will prompt you for a name of the custom shape. Enter something meaningful and then press OK.
Now that you have a custom shape of your originally rasterized icon, you can use the Photoshop Shape tool to create a shape layer from it, which will be a scalable vector path object, instead of a rasterized image.
Now that we have a custom shape created from our selection we can simply create copies of it using the Custom Shape Tool. Add a new layer or start a blank document and then select your custom shape for the Custom Shape Tool and drag one out. If you need to fix the proportions so your icon doesn't stretch out, you can hold Shift wile drawing.
Be sure to hide or delete your rasterized source layer and then follow the steps from Scenario 1 above to create your SVG file. If all goes well hopefully you will end up with something similar to the following XML.
That's all there is to it. This is how I knocked out my Movember-themed SVG navigation icon. I hope you found this useful and that it helps you get more out of Photoshop. Happy SVG'ing. Until next time.