I have a background image with object of some shape (example below). I would like to add new images – layers (simple div with position:absolute – left/top) to this image and only on the shape where I want.

And then with PHP code added images (for example 10, 50,..) to only this shape and no other place:

Is this possible to do on any simple way with PHP/JS/jquery/…? I just need to pass how many items and that many images is added to that area…
Here’s a complete rewrite of my answer.
Given a random picture, fit many smaller pictures only where some
coloris matched:I decided to go with a crab as a starting pictures, because crabs are cool:
I only want to add red dots where there is blue in the picture
To do this, I will split my answer in 3 sections:
HTML
I start with a very simple HTML file:
This gives us a starting point for rendering our dotted crab!
PHP
Now, in PHP, I open both my
crab.pngand mydot.pngto analyze their content, and locate random positions where thedot.pngcan fit in anall bluesection. Right after the<img src="crab.png">, I inserted the following:Here, a few details:
$numDesiredDotsis hardcoded to 10, but it could easily be a parameter!$spawnableWidthand$spawnableHeightrepresent the greatest coordinate adotcan be placed in without going out of the picturesrand(time());is simply used to have a different random everytime$testingForDotSubpartis a small image I will be using to test a given coordinate to see if it only contains pixels of the right color$colorWereLookingForis set tobluenow, because my crab is blue, if you wantedred, it should be something like0xFF0000. For this example I used the same PNG for the HTML and the image processing, but you could easily just create a mask for the image processing and have a full color image for the HTML.Now, we need to create valid coordinates for each dot, which is done with the following php:
Again, some explanation:
X,Ycoordinate$invalidCoordinatesarray to show how many tries failed – the more complex the picture, the more fails there will beNow that we have computed all our positions, we need to clean up the resources:
Finally, I added some debug output that you can get rid of, but we need to output the dots on the crab! To show you that each dot is unique, I attached a
JavaScriptalertthat shows the dot index:Here, I am using the
styleleftandtopto give precise pixel positioning to the dots. To have them match precisely with the parent picture, we need to useposition:relative;andposition:absolute;as described in the next section.CSS
As you can see, I’m using a class for the
div, this is to take advantage of therelative positioning. I added at the top of the file, right after the title, the followingResult
Here’s a run of the given script… you could easily save the HTML generated so you don’t have to recompute positions everytime:
Hope this helps!
Edit: here’s the full code, if you need it: pastebin
Edit 2: note that there is no overlap checking, you might want to check that the rectangle defined by
$randomX,$randomY,$randomX + $dotWidthand$randomY + $dotHeightdoesn’t overlap an existing rectangle from the coordinates in$validCoordinates.Edit 3: Once generated you can simply open the source of the page, and copy the
divto yourHTMLso it’s not regenerated everytime.Also, as long as the color mask described by the
$crabpicture opened in PHP, you can change theimg srcto something else if you wanted a colorful crab. I added acrabc.pngfile which is now used by myimg, but it still has the same outline as thecrab.pngfile:Which gives this final look: