Crop any image as a square in the center... and more!

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: xbytor, DavideBarranca, Paul MR

Post Reply
Posts: 1
Joined: Mon Apr 30, 2018 7:39 pm

Crop any image as a square in the center... and more!

Post by tirso » Mon Apr 30, 2018 7:59 pm

Hi! These days one of my clients sent me a bunch of pictures and asked to post them on Facebook and other social media. The first step that I had to overcome was to make all of them square and the same size, despite the fact that orientation of those pictures were random.
So I decided to use an script and a batch to:
1 - Take any picture, make a square selection on the center of the picture, considering the larger side;
2 - Crop the picture to that square;
3 - Resize the picture to 1200px wide to 1200px tall;
4 - create a white border around the picture, 10% of the overall size of the picture.
As I couldn't find in any place a similar script, using bits and pieces of other sites, I have created my own, which I share with you because this community was my main source of information. Please, don't ask me tough questions, I am not a professional in this field.

Thank you all.

Code: Select all

if(app.documents.length > 0){
var doc = app.activeDocument;
var savedRuler= app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var w = doc.width;
var h = doc.height;
if (w != h){
if(w < h){
// working in portrait mode:
selectedRegion = Array(
Array(0,(h-w)/2), //top left corner
Array(0, (h-w)/2+w), //bottom left corner
Array(w, (h-w)/2+w), //bottom right corner
Array(w, (h-w)/2)//top right corner
// working in landscape mode:
selectedRegion = Array(
Array((w-h)/2,0), //top left corner
Array((w-h)/2+h,0), //bottom left corner
Array((w-h)/2+h,h), //bottom right corner
Array((w-h)/2,h)//top right corner
}; // make the selection
doc.crop(doc.selection.bounds); // crop the picture;
doc.resizeImage(UnitValue(1200,"px"),UnitValue(1200,"px"),150,ResampleMethod.BICUBIC,100); // change picture size;
var w = doc.width;
var h = doc.height;
// create a white background color;
var backgroundC = new SolidColor(); = 255; = 255; = 255;
backgroundColor = backgroundC;
// resize the canvas size increasing it 10% in all directions from center
doc.resizeCanvas (w*1.1,h*1.1, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = savedRuler;
images attached for reference about the results. The brand/watermark is added later by a common action.
image after
Frans_doces_002.jpg (844.71 KiB) Viewed 3742 times
WhatsApp Image 2018-04-26 at 08.31.59 (1).jpeg
image before
WhatsApp Image 2018-04-26 at 08.31.59 (1).jpeg (400.42 KiB) Viewed 3742 times

Post Reply