Give a simple image a transparent background: “Transparentize”

I needed a quick and dirty script (that I took a few minutes to clean up and throw together in a class) that can take a basic JPEG and turn it into a transparent PNG around the edges. I called it “Transparentize” — only because I couldn’t find any proper term for this.

I looked into PHP GD library’s imagefill function but it wasn’t quite giving me the results I needed. It only filled for exact colours, and in a JPEG with millions of colours, there are a number of variations of what the average set of eyes perceives as “white.” I need something where a threshold could be specified and find the edges of an image on a high contract background (for example, a basic black square on a white background).

How it works

Now I knew ahead of time that the images I was transparentizing were of pretty basic shapes, so I kept this in mind. My small script starts from the top image and processes from left to right until it reaches the contrasted edge of the object (as defined by the threshold) and then makes its way from right to left on the same Y axis. Here are some example results with varying thresholds:

Original Image Threshold: 1 Threshold: 50 Threshold: 145

What needs to be done

Firstly, it could certainly use some sort of anti-aliasing, but I haven’t yet looked into this. I experimented a bit, but it might take some deeper research.

Secondly, it only works with images of a basic shape (like a box), it won’t work with more complex images that overlap their own background (like the letter ‘C’). I didn’t need this for my purposes, but if I have time I’ll write it out.

Download the code

View/download the Transparentizer PHP class.
Try it yourself online.

5 thoughts on “Give a simple image a transparent background: “Transparentize”

  1. How can I make an already existing image transparent in Adobe Photoshop 7?

    • Andrew

      I’m not really up to offering any sort of Photoshop advice, but if you’re looking for a similar effect with similar images I would try using the Magic Wand tool.

  2. […] my last post I was looking for a way to give simple images transparent backgrounds. I noted that PHP GD’s […]

  3. Hi Andrew,
    You make my day 🙂

    Having troubles using ImageMagick and the PHP class Imagick on localhost and on shared host, your class save me lot of headaches ! This page deserve more comments 😉

    By the way, to go on the full image (and not only borders), we just have to :
    – comment the “break” in the “left to right” loop
    – delete the “right to left” loop, became useless

    Thank you

  4. Good site you have got here.. It’s difficult to find good
    quality writing like yours nowadays. I really appreciate individuals like you!
    Take care!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>