This is killing me..
The Image has a background of solid red, a foreground of solid grey. On the edges of the foreground there is some anti-aliasing from grey to white.
What I need to do is change the foreground color to a user specified color and the red to transparent. This is easy enough using imagecolorcloset + imagecolorset/imagecolortransparent. The problem is the antialasing part.
I believe I need to loop through the pixels in the image. This would appear to work: http://www.php.net/manual/en/book.image.php#98153
I believe that I now need to determine the color difference between the foreground color and the anti aliased color.
So...
Foreground Color = rgb (153, 153, 153)
Anti-Alias Pixel = rgb (173, 173, 173)
New Color = rgb (0, 0, 255)
How do I determine the difference in color between the AA Pixel and the Foreground Color then apply it to the new color?
Edit: Turns out it's to do with factors
The anti-alias value should be the same factor so
<?
//old foreground colour
$of = array (220, 220, 220);
//old anti alias colour
$oaa = array (110, 110, 110);
//factors
$factors = array ($of[0]/$oaa[0], $of[1]/$oaa[1], $of[2]/$oaa[2]);
//new foreground
$nf = array (156, 34, 34);
//new anti alias
$naa = array($nf[0]/$factors[0], $nf[1]/$factors[1], $nf[2]/$factors[2]);
?>
Related
I have a script that takes an image and when resampled centers the short dimension (width or height) on a square coloured background.
This works fine for images with a longer WIDTH but for some reason any image with a longer HEIGHT the result is a split background fill - the correct colour on the left but the default black on the right. If I play with the x-axis offset the background fill only extends to the right as far as the image placement.
The calculated values are as expected for the vertical images so I cannot figure out what is happening here. Note that 'imagecopy()' produces the exact same behaviour.
original image is 155 x 400px
adjusted source dimensions for square aspect ratio = 400 x 400px
resulting thumbnail to be 250 x 250px
Here is the code with static values for one example:
$thumb = imagecreatetruecolor(250, 250);
imagecopyresampled($thumb, $source, 77, 0, 0, 0, 250, 250, 400, 400);
$blue = imagecolorallocate($thumb, 0xDE, 0xE6, 0xF9);
imagefill($thumb, 0, 0, $blue);
Using the same image rotated 90 degrees (400 x 155 px) so it is longer horizontally DOES apply the full background fill:
imagecopyresampled($thumb, $source, 0, 77, 0, 0, 250, 250, 400, 400);
For the vertical image, my coordinate values (77, 0) place the image on the imagecreatetruecolor() canvas centered exactly where I want it but changing any of the other imagecopyresampled() values stretch or squeeze the resampled image or crop it.
Am I overlooking something simple? View the screenshots here:
http://i.stack.imgur.com/5CxHU.jpg (vertical issue) and
http://i.stack.imgur.com/wvhzP.jpg (OK horizontally)
This vertical issue must have something to do with PHP's resampling/imagefill algorithm (?) but here is a workaround that now works for centering all of my vertical images within my square canvas:
1) You need to first pad your image placeholder so the background fill will extend to the right edge in the resampled image by extending the thumbnail height with your x-axis offset (sounds odd but it works)...we will trim this off later:
$thadj_height = $th_height + $th_x;
$thumb = imagecreatetruecolor($th_width, $thadj_height);
2) Resample as usual with the background fill (note that the fill is applied AFTER the resampling statement, odd but just works that way)...remember that $thumb has more height than what $th_width, $th_height will occupy:
imagecopyresampled($thumb, $source, $th_x, $th_y, 0, 0, $th_width, $th_height, $src_width, $src_height);
imagefill($thumb, 0, 0, $bgcolor);
3) Temporarily save the image output so a new function can be applied to it next - set quality to lossless since we'll be reusing it:
imagejpeg($thumb, "resampled/output_temp.jpg", 100);
imagedestroy($thumb);
4) Retrieve the temporary file and grab the new dimensions (overwrite the previous variables):
$file = "resampled/output_temp.jpg";
$image = file_get_contents($file);
$source = imagecreatefromstring($image);
list($src_width, $src_height) = getimagesize($file);
5) Create a new image placeholder, square as originally intended in my case:
$thumb = imagecreatetruecolor($th_width, $th_height);
6) Now copy the temporary padded thumbnail into the square placeholder which will result in cropping off the padding:
imagecopy($thumb, $source, 0, 0, 0, 0, $src_width, $src_height);
header('Content-Type: image/jpeg');
echo imagejpeg($thumb);
imagedestroy($thumb);
Again, none of this is necessary for centering my horizontal images on a square canvas but this is a workaround that will work to eliminate the split fill background.
I have a very frustrating situation. I am using PHP GD for the first time, and it's been a bit of a rollercoaster relationship. Basically, I am trying to merge 2 images, a square one (with a height/width of x) onto a rectangle (with a width of x and a height of y).
The square needs to be centered vertically. But this isn't the issue - I've managed to position it correctly.
Whats happening is, my rectangle is white. My square has a white background, so when the images are merged, it should just look like my asset on a white rectangluar background.
When I merge the image though, GD is for some reason changing my background white rectangle to black - so you can see the white square in the middle, with black "bars" on top and bottom. Can anyone help?
Code is:
//create copy of original image to correct size
imagecopyresized($dst_image, $src_image, 0,0,0,0,$x_width,$x_height,$orig_img_x_width,$orig_img_x_height);
imagejpeg($dst_image, "resized_copy.jpg", 100);
$img = imagecreatetruecolor(1333, 2000);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill ( $img, 0, 0, $white );
imagefilledrectangle($img,0,0,1333,2000, $white);
imagejpeg($img, "rectangle.jpg", 100);
//merge images
$dest2 = imagecreatefromjpeg("rectangle.jpg");
$src2 = imagecreatefromjpeg('resized_copy.jpg');
imagecopymerge($dest2, $src2, 0, 0, 0, -333.5, $x_width, $x_height, 100);
imagejpeg($dest2, "final_image.jpg", 100);
I've tried using imagecopy instead of imagecopymerge, but I get the same result. I'm sure there is a simple explanation, but I cant seem to find it trawling through the php manual.
I've read your question a few times but I'm not convinced I understand exactly what you are trying to achieve so I've made a few assumptions in producing the below code.
For the sake of simplicity I've created a 'square.jpg' test image file like so:
(Note that I've used small image sizes here so I can show them inline.)
// read in the square test image.
$square = imagecreatefromjpeg('square.jpg');
$square_x = imagesx($square); // 100px
$square_y = imagesy($square); // 100px
// create the rectangular image to merge with.
$rectangle = imagecreatetruecolor(100, 200);
$rectangle_x = imagesx($rectangle); // 100px
$rectangle_y = imagesy($rectangle); // 200px
// note that this isn't white, but rather a lovely shade of blue to better
// show the image on the white SO background!
$white = imagecolorallocate($rectangle, 128, 128, 255);
imagefill($rectangle, 0, 0, $white);
// merge the images.
imagecopymerge(
$rectangle,
$square,
0,
($rectangle_y / 2) - ($square_y / 2), // to vertically centre the square.
0,
0,
$square_x,
$square_y,
75 // Just to show the merge clearly; change back to 100 for your usage.
);
imagejpeg($rectangle, 'final_image.jpg', 100);
imagedestroy($rectangle);
imagedestroy($square);
This gives me the following image in final_image.jpg:
I'm new here on Stackoverflow.
I would very much like figure out if it's possible to make a specific color of an image transparent using a PHP script. And if it is, how to go about doing it.
Let's say this is the image of which the background color (green) has to be completely transparent: French Hotdog w/ green background
I've got absolutely no idea what a script like this would look like or even where to begin.
I imagine that you'd propably have to set the unwanted RGB color and then "scan" every single pixel in image to determine which pixels have to be transparent. But other than that I'm clueless.
Seriously hope someone here is able to help me out on this one.
demo : http://so.devilmaycode.it/php-how-to-make-a-green-area-background-in-an-image-transparent
first of all the image should be png cause jpeg don't support transparency then the code is like this:
<?php
$image = 'test.png';
$im = imagecreatefrompng($image);
//if you exactly know the RGB color indexes
//$rgb = imagecolorexact($im, 0, 0, 0);
//or keep the rgb color by position so at top 0 left 0
$rgb = imagecolorat($im, 0, 0);
imagecolortransparent($im, $rgb);
header("Content-type: image/png");
//display the image directly
imagepng($im);
// or save it
// imagepng($im, 'test-to-transparent.png');
imagedestroy($im);
?>
imagecolortransparent() will help you:
...
$yourColor = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $yourColor);
...
I have an image and an image sprite. I want to merge certain parts of the sprite on top of the image. I have got this working with just one part of the sprite, however when the sprite is merged on top of the image, the sprite loses its transparency.
$image = $homepath.'/images/orig.png';
$sprite = $homepath.'/images/sprite.png';
$image = imagecreatefromstring(file_get_contents($image));
$sprite = imagecreatefromstring(file_get_contents($sprite));
imagecopymerge($image, $sprite, 50, 50, 80, 0, 80, 100, 100);
imagepng($image, $homepath.'/images/output.png');
Example output: http://i.imgur.com/ZyL9D.png
Whatever the dimensions are that I set for the sprite is what has a background color. The color is the same color as the sprite. So in the case, the word "text" are part of the sprite, but if this text was green, the rectangle would be green.
///EDIT///
Figured it out: imagecopyresampled
You probably want another gd function: imagecopyresampled.
I'm able to draw a circle on a transparent background, but it's pixelated around the edges.
I can also get an anti-aliased circle on a non-transparent background with http://mierendo.com/software/antialiased_arcs/.
So, how do I get both?
The trouble with transparent images is that the half-transparent pixels at the border will be partly colored with the color you specified as the transparent color, if you can live with that then you can use the antialiased arcs library you specified, just put the following at the beginning of their example (I used the optimized version) :
include ("./imageSmoothArc_optimized.php");
$img = imageCreateTrueColor( 648, 800 );
imagealphablending($img,true);
$color = imageColorAllocate( $img, 255, 255, 255);
$transparent_color = imageColorAllocate( $img, 0, 0, 0);
imagefill( $img, 5, 5, $transparent_color );
imagecolortransparent($img, $transparent_color);
This will give you a transparent PNG image. Since I specified black as the transparent color, the circles will go to black near the edge, and you will only be able to use this on a dark background.