counterpart to PILs Image.paste in PHP

2024/10/13 7:26:40

I was asked to port a Python application to PHP (and I'm not very fond of PHP).

The part I'm having trouble to port uses a set of monochromatic "template" images based on the wonderful Map Icons Collection by Nicolas Mollet. These template images are used to create an icon with custom background and foreground colors. PIL's Image.paste is used to "paste" the icon foreground with the selected color using the template Image as alpha mask. For example:

icon creation

How can I replicate this in PHP? Is there any alternative besides doing it pixel-by-pixel?

[update]

I'm not proud of my PHP skills... What I've got so far:

<?phpheader('Content-type: image/png');// read parameters: icon file, foreground and background colors
$bgc = sscanf(empty($_GET['bg']) ? 'FFFFFF' : $_GET['bg'], '%2x%2x%2x');
$fgc = sscanf(empty($_GET['fg']) ? '000000' : $_GET['fg'], '%2x%2x%2x');
$icon = empty($_GET['icon']) ? 'base.png' : $_GET['icon'];// read image information from template files
$shadow = imagecreatefrompng("../static/img/marker/shadow.png");
$bg = imagecreatefrompng("../static/img/marker/bg.png");
$fg = imagecreatefrompng("../static/img/marker/" . $icon);
$base = imagecreatefrompng("../static/img/marker/base.png");
imagesavealpha($base, true); // for the "shadow"// loop over every pixel
for($x=0; $x<imagesx($base); $x++) {for($y=0; $y<imagesy($base); $y++) {$color = imagecolorsforindex($bg, imagecolorat($bg, $x, $y));// templates are grayscale, any channel serves as alpha$alpha = ($color['red'] >> 1) ^ 127; // 127=transparent, 0=opaque.if($alpha != 127) { // if not 100% transparentimagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $bgc[0], $bgc[1], $bgc[2], $alpha));}// repeat for foreground and shadow with foreground colorforeach(array($shadow, $fg) as $im) {$color = imagecolorsforindex($im, imagecolorat($im, $x, $y));$alpha = ($color['red'] >> 1) ^ 127;if($alpha != 127) {imagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $fgc[0], $fgc[1], $fgc[2], $alpha));}}       }
}
// spit image
imagepng($base);
// destroy resources
foreach(array($shadow, $fg, $base, $bg) as $im) {imagedestroy($im);
}?>

It's working and performance is not bad.

Answer

As per my comments, ImageMagick would be able to do this. However you've indicated that this may not be non-optimal for your use case, so consider using GD2. There's a demo on how to do image merging on the PHP site.

I would guess that this can be done on any (fairly recent) default PHP installation.

https://en.xdnf.cn/q/118105.html

Related Q&A

Google Cloud Dataflow fails in combine function due to worker losing contact

My Dataflow consistently fails in my combine function with no errors reported in the logs beyond a single entry of:A work item was attempted 4 times without success. Each time the worker eventually los…

AttributeError: super object has no attribute __getattr__

Ive been searching for the solution of this problem over the all internet but I still cant find the right solution. There are lots of generic answers but none of those have solved my problem..I am tryi…

Selenium load time errors - looking for possible workaround

I am trying to data scrape from a certain website. I am using Selenium so that I can log myself in, and then start parsing through data. I have 3 main errors:Last page # not loading properly. here I am…

How to POST ndb.StructuredProperty?

Problem:I have following EndpointsModels,class Role(EndpointsModel):label = ndb.StringProperty()level = ndb.IntegerProperty()class Application(EndpointsModel):created = ndb.DateTimeProperty(auto_now_ad…

Issue computing difference between two csv files

Im trying to obtain the difference between two csv files A.csv and B.csv in order to obtain new rows added in the second file. A.csv has the following data.acct ABC 88888888 99999999 ABC-G…

How do I display an extremly long image in Tkinter? (how to get around canvas max limit)

Ive tried multiple ways of displaying large images with tkinterreally long image No matter what Ive tried, there doesnt seem to be any code that works. The main issue is that Canvas has a maximum heigh…

NoneType has no attribute IF-ELSE solution

Im parsing an HTML file and searching for status of order in it. Sometimes, status doesnt exist, so BeautifulSoup returns NoneType, when Im using it. To solve this problem I use if-else statement, but …

looking for an inverted heap in python

Id like to comb off the n largest extremes from a timeseries. heapq works perfectly for the nlargestdef nlargest(series, n):count = 0heap = []for e in series:if count < n:count+=1hp.heappush(heap, e…

Concatenating Multiple DataFrames with Non-Standard Columns

Is there a good way to concatenate a list of DataFrames where the columns are not regular between DataFrames? The desired outcome is to match up all columns that are a match but to keep the ones that …

Python Conditionally Add Class to td Tags in HTML Table

I have some data in the form of a csv file that Im reading into Python and converting to an HTML table using Pandas.Heres some example data:name threshold col1 col2 col3 A 10 12 9 13…