I am trying to do the following:
I Have a picture I want to make that only text which is written in black remains visible and rest of every colour gets transparent.
I tried to do this using PHP imagecolortransparent function but I am not able to figure out how to make it work.
Any help will be great.
Thanks In advance
Updated Answer
There must be an easier way, but until I think of it, this should work:
// Read original image
$start = imagecreatefrompng("input.png");
// Make true colour image same size
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
$red=imagecolorallocate($im,255,0,0);
imagesavealpha($im, TRUE);
// Fill the new image with transparency
imagefill($im, 0, 0, $transparent);
// Copy only black pixels across from original to new
for($x=0;$x<imagesx($im);$x++){
for($y=0;$y<imagesy($im);$y++){
$rgb = imagecolorat($start,$x,$y);
if($rgb==0){
imagesetpixel($im,$x,$y,$black);
}
}
}
imagepng($im,"result.png");
Original Answer
I would do this:
// Read original image
$start = imagecreatefrompng("input.png");
// Create a proper truecolour image the same size that can support transparency
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$black = imagecolorallocate($im, 0, 0, 0);
$text="This is some quite black text";
imagestring($im,5,0,75,$text,$black);
imagepng($im,"result.png");
input.png
result.png
Related
I have a problem with imagecreatetruecolor function , i write this code but it make my background white can somebody tell me where i go wrong? I have a array of input image and first i make a empty layer of an first image width and height and try to make it transparent
foreach($form_fields as $fm)
{
$layer = imagecreatefromstring(file_get_contents($fm));
$layerWidth = imageSX($layer);
$layerHeight = imageSY($layer);
if (!isset($slate))
{
$slate = imagecreatetruecolor($layerWidth, $layerHeight);
imagealphablending($slate, false);
$trans_colour = imagecolorallocatealpha($slate, 255, 255, 255, 127);
imagefill($slate, 0, 0, $trans_colour);
imagealphablending($slate,true);
imagesavealpha($slate,true);
}
imagecopy($slate, $layer, 0, 0, 0, 0, $layerWidth, $layerHeight);
imagealphablending($slate,true);
imagedestroy($layer);
}
imagetruecolortopalette($slate, false, 256);
imagepng($slate, $CreatedAvatar);
imagedestroy($slate);
I'm struggling with PHP's GDLib just a little bit here. I'm trying to overlay two .PNG images on top of eachother, which so far works fine.
The one problem I run into is that sometimes, the overlay image comes with a white background. I can make this transparent (using the imagecolortransparent function), but this transparency isn't saved when I copy this image onto a new one.
// Load the background image first
$background = imagecreatefrompng($this->background);
// Load the overlaying image next, and set white as a transparent color
$overlay = imagecreatefrompng($this->image);
imagecolortransparent($overlay, imagecolorallocate($overlay, 255, 255, 255));
// So far, this all works. But when I create a new image,
// and paste both $background and $overlay into it,
// $overlay loses transparency and reverts to a white fill.
$image = imagecreatetruecolor(16, 16);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $trans_colour);
imagecopyresampled($image, $background, 0, 0, 0, 0, 16, 16, 16, 16);
imagecopyresampled($image, $overlay, 0, 0, 0, 0, 16, 16, 16, 16);
#mkdir(dirname($file), 0777, true);
imagepng($image, $file);
// The new $image is now mostly white. The transparency on $overlay
// was lost, meaning that the $background image is completely invisible.
How can I keep the transparency when copying $overlay into a new image?
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";
$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);
imagecopy($im, $im2, (imagesx($im) / 2) - (imagesx($im2) / 2), (imagesy($im) / 2) - (imagesy($im2) / 2), 0, 0, imagesx($im2), imagesy($im2));
// Save alpha for the destination image.
imagesavealpha($im, true);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);
// Save final image after placing one on another image
imagepng($im, "output.png", 0);
imagedestroy($im);
imagedestroy($im2);
I have a script that resizes uploaded images. It works fine for PNGs and JPGs but with GIFs it renders the transparency in the resized GIF black.
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($dst, $file);
From the manual page http://us3.php.net/manual/en/function.imagecopyresampled.php#104028
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
is used by one poster to preserve transparency.
Here's a tip... Always check out user comments on php.net as they are generally very helpful in understanding the nuances of a function and providing tips for handling common tasks.
I'm assuming this goes to a web-page, which if it does you could just output the image tag with the correct attributes?
echo "<img src='$file' width='$newWidth' height='$newHeight' />";
You still have to set the transparency in the new image. The following is taken from the php docs:
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// ^^ This is the command you are missing.
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
In your case, you want to make the transparency the same color as the original transaprency - which I always made a hideous purple or something that would a) stick out like a sore thumb in my image manipulation software and secondly, have a RGB key that was almost impossible to have included in an image by mistake.
First: I want to create a PNG image and draw a shape inside it.
Second: Overlap the first PNG on another image (for example a jpg image)
The problem is: When I create the first PNG with a shape inside (the background is not transparent and is dark) so this make a black overlap on second image.
How can I fix it?
I don’t work so much with images function so I got trouble.
Notes: I need to create the first transparent PNG and then overlap it on second image. I don’t want to create shape directly on second image.
And The Code:
<?php
define('EXAMPLE_TMP_SERVERPATH', '');
define('EXAMPLE_TMP_URLRELPATH', '');
$tempDir = EXAMPLE_TMP_SERVERPATH;
$fileName = 'test3img.png';
$imgW = 125;
$imgH = 125;
# First
$base_image = imagecreatetruecolor($imgW, $imgH);
$black = imagecolorallocate($base_image, 0, 0, 0);
imagecolortransparent($base_image, $black);
$col[0] = imagecolortransparent($base_image, $black);
imagealphablending($base_image, true);
imagesavealpha($base_image, true);
imagefill($base_image, 0, 0, $col[0]);
imagefilledrectangle($base_image, 4, 4, 50, 25, 255);
imagepng($base_image, $tempDir.$fileName);
# First2
$target_image = imagecreatetruecolor($imgW*5, $imgH*5);
$black2 = imagecolorallocatealpha($base_image, 0, 0, 0, 127);
imagecolortransparent($target_image, $black2);
imagecopyresized($target_image, $base_image, 0, 0, 0, 0,$imgW, $imgH, $imgW, $imgH);
imagedestroy($base_image);
imagepng($target_image, $tempDir.$fileName);
imagedestroy($target_image);
# First2
# Second
$dest = imagecreatefromjpeg('../avatar.jpg');
$src = imagecreatefrompng(EXAMPLE_TMP_URLRELPATH.$fileName);
imagealphablending($dest, true);
imagesavealpha($dest, true);
imagealphablending($src, true);
imagecopyresampled(
$dest,
$src,
0,0,
0,0,
200, 200,
125, 125
);
imagepng($dest, EXAMPLE_TMP_URLRELPATH.'_m.jpeg');
imagedestroy($dest);
imagedestroy($src);
?>
Need to add the alpha channel too:
Alpha Channel
When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?
Simply do this:
imagealphablending($img, false);
imagesavealpha($img, true);
Before outputting. Make sure that all source files (if you used any) are set to PNG 32-bit with transparency - if not the output may differ with black background or transparency does not comply.
Here is the example
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefill($newimage, 0, 0, $transparentindex);
Here is an example of the imagecolortransparent function (if it helps):
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
There's a function called imagecolortransparent that allows you to set which color is made transparent. I don't know if this answers your question.