How to anti-alias an image with PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove white background from an image and make it transparent
I currently have a code that removes the white background from an image, it looks like this:
function transparent_background($filename, $color)
{
$img = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].'/'.$filename);
$colors = explode(',', $color);
$remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
imagecolortransparent($img, $remove);
imagepng($img, $_SERVER['DOCUMENT_ROOT'].'/'.$filename);
}
transparent_background('test.png', '255,255,255');
However, once it exports, the edge is very rough. This is what it looks like (note that this is just part of my image):
http://img211.imageshack.us/img211/97/2125c773e32c432b91e1127.png
I added a black background behind that image to show the edge better. So is there a way that I can add a line to the function or edit the function so the edges are smoother/anti-aliased? Thanks!

There is no easy way to do this. The edge of the original image was anti-aliased to the white background. When you remove the pure white, you're left with a lot of pixels near the edge that are close to white. When you see those pixels against a dark color, they're going to stand out and look "rough". You won't get a smooth edge against a transparent background if it's not in the source image.

The final result of the output is being determined by the initial image quality of the image you are importing. PHP can only anti-alias elements that are drawn on an image.
One solution, a total sledgehammer approach, would be to resample the image up, and then down again. Your results will vary depending on the image in question, and it will almost always be not good.
The best solution for your particular problem is to use better quality imported images in the first place.

Related

change color range image

I am using PHP-Imagick to convert a PDF into images, which works fine.
A simplified example:
$im = new imagick();
$im->setResolution(250,250);
$page = $pdfPath."[0]";
$im->readImage($page);
if($im->getImageColorspace() == Imagick::COLORSPACE_CMYK)
{
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->autoLevelImage();
}
$im->setImageFormat('jpg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(90);
$im->writeImage("page1.jpg");
However the coloring seems to differ from the original PDF, for example (see image)
left side is original PDF.
right side the rendered image, which has different colors than the original.
It seems that the original color of dark-pink turns into purle. Also red colors seem to saturate.
I have tried the following, but none worked:
reduce saturation (e.g. $im->modulateImage(100, 80, 100); ) But this causes red to become orange and yellow colors to turn greenish.(see example below)
playing around with image filter (e.g. imagefilter($im, IMG_FILTER_COLORIZE,100, 0, 0); ), but this also changes all colors.
Thinking it had to do with differences between CMYK and RGB, I have added $im->transformImageColorspace() doesn't do anything to fix this.
playing with various hue filters, but these change all colors of the image also.
Question: it seems I need to reduce the redness of the image? (I think it's the excessive red color causing the difference) without affecting the other colors, so that for example yellow remains yellow. Is there a way to do this in PHP?
The solution is to add the following line before reading the image:
The code should be:
$im->setColorspace(Imagick::COLORSPACE_SRGB);
$im->readImage($page);

How to remove png image white background color using php?

I have to remove image background color and make that transparent. I am using below code to make image transparent but that is not working.
function transparent_background($filename, $color)
{
$img = imagecreatefrompng('image.png'); //or whatever loading function you need
$colors = explode(',', $color);
$remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
imagecolortransparent($img, $remove);
imagepng($img, $_SERVER['DOCUMENT_ROOT'].'/'.$filename);
}
transparent_background('logo_100x100.png', '255,255,255');
Please see screenshot for better understanding that what I am trying to do.
Thanks in advance...
Actual color inside the box (around puma's face) is not 255:255:255 its 252:254:251. at least for most of it.
I set that color as input to your function "transparent_background" and set HTML background color to maroon color, result is
http://www.photojoiner.net/view/?fid=LpEajVuDhXT7yqH6WBBuhJtT0m9I4NnY.jpeg
You can create a loop and try making range of colors transparent. ie from 251:251:251 to 255:255:255. Not sure about the perfomance though.
Just giving you a wiled idea since your screenshot shows a web browser assuming its a web application, you can do the same using HTML5 context, You can select a region on the canvas and edit its imageData on that region. I.e select the white square with puma and set alpha setting of all white (or range of colors) pixels' to 0. More reading on HTML5 canvas pixel manipulation https://dev.opera.com/articles/html5-canvas-basics/#pixelbasedmanipulation

php image filter "colorize" destroys image details

i am trying to to colorize a black and white picture on my server.
i have tried doing this with multiple methods and the only one that kind-of-works is gd imagefilter via filter IMG_FILTER_COLORIZE.
it does kind of colorize the image in exactly the same color that i want, but it loses all the details on the image, as if it just trimmed black dots that were not black enough and thinned all the black lines, making them almost invisible. here is what i'm talking about:
this result was achieved with this code
$im=imagecreatefromjpeg($orig_file);
imagefilter($im, IMG_FILTER_COLORIZE, 71, 92, 10);
imagejpeg($im, $output_file, 95);
why is this happening? are there any other methods how i could colorize the image? my original image is quite large and i can't iterate over it as it is too slow; that's why i'm trying to use a library that would do this
i have managed to achieve the desired result with help of Imagick and compositeImage. here is the result
how i achieved it is kind of a trick that works only in a very specific condition- which is the need to have the background white and only black/gray objects in front (or the exact opposite). this technique wouldn't work on a background with transparency
the idea is to have 2 layers- underneath is the layer with original grayscale image and the top layer- fully filled with the desired color. then you composite these images using "COMPOSITE_LIGHTEN" (you might use other methods, like darken for example- if you have white objects on a black background)
this is the code
$base_color="#475c0a";
$im=new Imagick($orig_file);
$im2 = new Imagick();
$im2->newImage($im->getImageWidth(), $im->getImageHeight(), new ImagickPixel($base_color) );
$im->compositeImage($im2, Imagick::COMPOSITE_LIGHTEN, 0, 0);
$im->writeImage($new_image_path);
hope this will help someone out

Transparency issue with png images

I 've got this image:
i use php5 and gd2 library to achieve a transparent background, but the result is not what i have expected(you need a dark background to see the difference):
and here is my code:
$size = getimagesize($img_url);
$img = imagecreatefrompng($img_url);
$im_tc = imagecreatetruecolor($size[0],$size[1]);
imagecopyresampled($im_tc,$img,0,0,0,0,$size[0],$size[1],$size[0],$size[1]);
imagedestroy($img);
$white = imagecolorallocatealpha($im_tc, 255, 255, 255,127);
imagecolortransparent($im_tc, $white);
imagepng($im_tc,'image.png');
Any help would be appreciated! :)
Your original image doesn't have a transparent background, but you're trying to create one by changing all pure-white pixels to transparent. As the text has a shadow/glow around it, those pixels aren't quite white, and won't become transparent. You have a couple options here:
Use an image editor (GIMP, Photoshop, etc.) and their fine-grained selection tools to perform a smart selection of the "background", then remove that. Photoshop, at least, has extremely useful options available on its magic wand tool to choose threshold, feathering, and more.
Pick more colours to make "transparent." Instead of just white, choose all of the shades of gray, minus the exact colour that ".gr" is set in. This should make all of the shadowed parts of the image transparent as well, although the edges may be less than perfect (i.e.: not feathered, slightly jagged.)

Convert an image color/hue using PHP GD library. The Catch? It has rounded corners

I'm writing code this week to dynamically change the color of images. As long as the image is square or rectangle, the code below works perfectly. However, I have an image that has rounded corners (filled with a spot color and the background outside the corners is white.)
Can someone tell me if there's a simple way to color an image that has rounded corners using the imagecolorset function (or any other php method)?
I only want the non white areas of the image to be colored (In case you're wondering, I'm disallowing the color white to be applied to the image).
Note, perhaps a better way to do this would be to use a PNG image that has transparent background (rather than my gif image). If you think that's a better approach, please advise.
Here's the function that I'm working with...
function set_theme_color_header($hex)
{
$info = hexToRGB($hex); //calls a helper function which translates the hex to RGB
$img = imagecreatefromgif('header-template.gif'); //again, this could be a PNG image, but we always start with this image, then create a color copy
$color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]);
imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]);
imagegif($img, 'header.gif'); //only problem is that the imagecolorset function creates a messy fill at the corners
}
GD does not handle gif's very well. I recommend creating a png image with your rounded corners, then use GD's imagefilter($img, IMG_FILTER_COLORIZE, $info["red"], $info["green"], $info["blue"], $alpha) function. Then save the image as a gif.
Alternatively, use php's imagick library to literally draw the image. imagick is about a billion times better than GD. Poorly documented, but...the basic stuff isnnt too hard. See the tut link below.
Search php.net for "imagick" to see all the functions.
imagick tutorial
i really do not not why you are doing this, but maybe have a look at phpTumb (and its demos here).

Categories