Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:16:34+00:00 2026-06-13T00:16:34+00:00

Before I start, I just want to note that I am a PHP noob.

  • 0

Before I start, I just want to note that I am a PHP noob. What I want to do is tint a PNG image to one color. So all transparent pixels will remain transparent, and all non-transparent pixels will be that color. I have searched many sites for this answer but for some reason I can’t find what I want.

Here is my first attempt based on different examples I found:

<?php
header('Content-Type: image/png');

$color = $_GET['color'];
$im = imagecreatefrompng($_GET['img']);
$width = imagesx($im);
$height = imagesy($im);
$imn = imagecreatetruecolor($width, $height);
imagealphablending($imn,false);
$col=imagecolorallocatealpha($imn,255,255,255,127);
imagesavealpha($imn,true);
imagefilledrectangle($imn,0,0,$width,$height,$col);
imagealphablending($imn,true);
imagecopy($imn, $im, 0, 0, 0, 0, $width, $height);
imagefilter($imn, IMG_FILTER_GRAYSCALE);


if ($color[0] == '#')
$color = substr($color, 1);

if (strlen($color) == 6)
$r = $color[0].$color[1];
$g = $color[2].$color[3];
$b = $color[4].$color[5];

$r = hexdec($r); 
$g = hexdec($g); 
$b = hexdec($b);

imagefilter($imn, IMG_FILTER_COLORIZE, $r, $g, $b);

imagepng($imn);
imagedestroy($imn);

?>

Essentially a perfect example of what I want can be seen here. The only change will be that instead of black, I want it to be converted to the color the user specifies.
Convert non-transparent pixels to black

Thank You

===============================
10/17/2012 Update

So based on xception’s answer, here is the code that I used to execute his script:

<?php

$source = "test.png";
$temp = "temp.png";
$color = "red";
$final = "FINAL.png";

exec("convert $source -alpha extract -threshold 0 -negate -transparent white $temp");
exec("convert $temp -fill $color -opaque black $final");
?>

It worked, however there is a small problem. As illustrated in the screenshots below, there are jagged edges. Any ideas on how to smooth the image so it looks as nice as it did in the BEFORE screenshot?

BEFORE:

BEFORE

AFTER:

AFTER

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:16:35+00:00Added an answer on June 13, 2026 at 12:16 am

    Example in 2 steps based on the link you pointed to:

    convert <source> -alpha extract -threshold 0 -negate -transparent white <tmp>
    convert <tmp> -fill red -opaque black <destination>
    

    replace <source>, <tmp>, <destination> with appropriate file names, replace red with the color you want.

    EDIT:
    shorter version found by question author:

    exec("convert $source -threshold 100% +level-colors '#00FF00', $final");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get one record before start date and end date DtpFrom means
before I start I want to point out that I tagged this question as
Before I start, I want to make it clear that my code is working
Just want to check I have my theory right before I start implementing. Constants:
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
I want to start by saying that I am a big fan of using
Just want to start out by saying this seems like a great site, hope
I have an assignment and i need working code. Before i start i want
Before start let me tell my experience: I am experienced with C#.NET, web services,
I usually try to do TDD with not much analysis (no diagrams) before start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.