Let’s say you have the picture of a flame and you want to change the background. In order to do that you first must make the background transparent. But how can you do that? You can do it by hand using a brush but it’s not quite the effect I was looking for. So I figured there must be a way to automatically adjust the transparency of each pixel depending on it’s darkness.
This gave me the chance to play around with PHP GD, and I must say I’m quite impressed. It seems to be a really powerful image processing tool. Here is the script I came up with, and here it is in action.

Before

After
Pretty neat, huh? Here’s the script so you can see it without having to download the zip file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | // $url: the URL of the image you want to be processed. It can also be a local filename. // $type: 0 for brightness, anything else for darkness. // $mode: 0 for theoretical mode, anything else for Photoshop mode. function transparent($url, $type=1, $mode=1){ // Create the old image $old_image = imagecreatefromstring(file_get_contents($url)); // Create the new image of the same size as the old one list($x_size, $y_size) = getimagesize($url); $new_image = imagecreatetruecolor($x_size, $y_size); // Turn off alpha blending and set alpha flag for our new image imagealphablending($new_image, false); imagesavealpha($new_image, true); // Do the following for each pixel for($y=0; $y<$y_size; $y++){ for($x=0; $x<$x_size; $x++){ // get it's color $rgb = imagecolorat($old_image, $x, $y); // and get the R, G and B components of the color $old_color = imagecolorsforindex($old_image, $rgb); $r = $old_color['red']; $g = $old_color['green']; $b = $old_color['blue']; // Define brightness: if ($mode) $brightness = max($r, $g, $b); // photoshop mode OR else $brightness = (max($r, $g, $b) + min($r, $g, $b)) / 2; // theoretical mode // Define darkness: $darkness = (255 - $brightness); // Generate transparence: if($type) $alpha = $darkness / 2; // for darkness OR else $alpha = $brightness / 2; // for brightness // Define the new color, same as the old one, but with alpha transparency $new_color = imagecolorallocatealpha($new_image, $r, $g, $b, $alpha); // Set that color to the pixel in the new image imagesetpixel($new_image, $x, $y, $new_color); } } // Output image to browser header('Content-Type: image/png'); imagepng($new_image); // Destroy all images in the end imagedestroy($old_image); imagedestroy($new_image); } |
Update: There’s also a Photoshop plugin for doing something similar, but it costs 27 euros.
5 Comments
Great! The results (the little program you made and the transparency you achieved) are quite impressive and highly useful, i must say
ia uite, ca am revenit pe la tine!
spune-mi te rog, cum se face in wordpress optimizarea linkurilor, sa fie user friendly?
Pai, văd că ți-ai cam făcut deja. Admin -> Settings -> Permalinks
Și sincer să fiu nici nu știu cum ai reușit să-ți pui numele categoriei în permalinkul articolelor, dar arată fain.
habar nu am nici eu
)
norocul incepatorului cred ca
am acum o alta intrebare: cum setez sa-mi apara pe prima pagina doar rezumatul articolului, asta chiar nu am gasit…
ms mult
Când scrii un articol, bagi tag-ul < ! - - more - - > până unde vrei să apară pe prima pagină. (numai fără atâtea spaţii). detalii aici.
One Trackback
[...] ett ställe där man kan skapa transparence i en bild där bakgrunden är ljus eller mörk Making darkness or brightness transparent | | | | | | Copyright © 2010 Ingers blogg. Powered by WordPress and dkret3. | Logga [...]