Here's a script algorithm:
- Create a rectangular selection based on logo ratio (square in your example), size: I guess larger than half of a document?
- Check this rectangular selection against layer transparency (using an
intersection
operation).
- If there's a selection in the result (which means that rectangular selection intersects with a layer), you need to create a contracted selection, 2 times smaller than before, storing the reductor.
- If there's no selection, reductor is too big, reduce it and try again.
- When reductor is around 1-4 pixels, your rectangular selection should barely touch the layer. Use dimensions of this selection to transform your logo.
- repeat for all 4 corners of the image.
Update: Ok, here's my variant. Not very fast (about five seconds for this image) but works.
Here's the script (it uses my library which I didn't include, but it's easy to rewrite it including your own helper functions)
function main()
{
var myLayer = L.getInfoTrgt(), // getting ID of the current layer to select it later
myLogo = new File("/C/Users/Sergey Kritskiy/Desktop/temp/mylogo.jpg"); // path to logo
for (var i = 0; i < 4; i++)
{
var doc = new Doc(), // getting width/height and some other properties of the document
size = {
left: 0,
top: 0,
right: doc.w / 1.5,
bottom: doc.w / 1.5, //presuming logo is square
},
myCondition = false,
difference = 50,
newSize,
counter = 0;
L.selectById(myLayer.id); //make sure the original layer is selected
while (!myCondition)
{
D.rectSelection(size); //creating a rectangular selection using `size`
L.loadSelection({operation: 'IntW'}); // loading selection from layer transparency using an `Intersection` operation
var selection = D.getSelection(); //getting resulting selection coordinates: false if there's no selection
if (selection == false) //no selection
{
size.right = newSize.right // restore `size` from previous attempt
size.bottom = newSize.bottom
difference = difference / 2; // make difference smaller
continue;
}
else //there's a selection
{
newSize = U.cloneObject(size); // store this `size` attempt
size.right -= difference //make selection `size` smaller
size.bottom -= difference
}
/////////////////////////////////////////////////////////////////////////////////////
counter++;
if (counter > 50 || difference < 1) myCondition = true; // if difference is small, it's enough
}
L.placeSmartObject({path: myLogo}) // placing logo as a smart object
var curLayer = L.getInfoTrgt(); // getting width, height of the layer, coordinates
L.freeTransform(
{
scale:
{
x: size.right / curLayer.width * 100,
y: size.bottom / curLayer.height * 100,
},
angle: 90 * i,
}).alignTo("left top"); // scaling it to selection size, putting it to left corner of the document
doc.rotate(90); //rotate document 90 degrees so that calculation would be always against top left corner of the document
}
}
app.activeDocument.suspendHistory("rotate", "main()");