Once you have mastered the greyscale effect, you are 95% of the way there with duotone. This effect is essentially a tinted greyscale, which means that we average the red, green, and blue values then add or subtract a little from each of them to get a particular tint. In order to handle the tint values, our function needs to accept four parameters: the image, a red amount, green amount, and blue amount. There is one additional check to be made for each colour, and that is that it is not over 255 - the maximum value for a single colour. Here is the new function:
The first half of that is basically identical to our greyscale function, but note that the new function takes $rplus, $gplus, and $bplus to handle users passing in tinting information. Now, when the colours are average, the value is stored in $red. The $green variable is then set to the value of $red plus the $gplus, which is the green tint amount passed into the function. The same is done for blue, with $blue and $bplus.
Finally, the $red variable itself is changed to reflect the red tint amount, then a new colour is allocated based upon these three and the pixel is coloured in with imagesetpixel().