Image with Transparent Background

Asked

Viewed 2,174 times

4

inserir a descrição da imagem aquiI wonder if it is possible to upload an image and make certain part of the image (the background) transparent. 'Cause I’d like to superimpose that image.

The dummy is the fixed background image, and the dress is the image I want to upload with the transparent background.

  • Yes. PHP? JS? which language would you like to do this?

  • I haven’t thought about the language yet, but PHP would be ideal, because I have more facility. However, if JS is simpler, it can also be. @Marcelobonifazio

  • Just one more thing, how do you define the background of an image? In this case, a white background?

  • For example, I’m going to have a background image one, dummy, and I want to put an outfit on top, but the image can’t have a white background, it has to be transparent. @Marcelobonifazio

  • So deep down you just want to superimpose one image on the other

  • Yes, I need all images of clothes that I upload to the system to be in the same style as the dress in the image, because the intention is for the user to take a picture of an outfit, and put it on the dummy. @Marcelobonifazio

Show 2 more comments

1 answer

2

To remove the white color of an image you can use a php script like the one below.

<?php 
    $img = imagecreatefromjpeg("imagemexemplo.jpg");
    $white = imagecolorallocate($img, 255, 255, 255);
    imagecolortransparent($img, $white);
    imagepng($img,'imagemexemploalterada.png');

It is important to note that this script will only remove white and not near white colors. If the image quality is not the best you will have to remove pigments close to white. For this you can make other iterations of the same functions by taking the output image using imagrecreatefrompng(...) and passing other values instead of $white = imagecolorallocate($img, 255, 255, 255);.

The accuracy of your algorithm will depend a lot on the quality and lighting where photos are taken.

Finally, you’ll need an algorithm to remove transparent areas of your image to make it easier to center on css.

A problem with this solution is the case where a white garment appears transparently inside the garment if it removes many colors close to white (this is the worst case you should test).

Another important point about these image manipulations in php is the memory allocation limit that can be reached if the photos are large (but can be easily changed in php.ini)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.