# Crop two different size images in same place formula?

I have two images at different scales… the first image is at high res and really not suitable to show the user so ive created a resized version to display for cropping…
example:

``````\$original_img_width = 1594;
\$original_img_height = 1063;

\$show_img_width = 664;
\$show_img_height = 443;
\$show_crop_x = 330;
\$show_crop_y = 220;
\$show_crop_w = 55;
\$show_crop_h = 55;
``````

how do i calculate

``````\$original_crop_x = ?;
\$original_crop_y = ?;
\$original_crop_w = ?;
\$original_crop_h = ?;
``````

ive tried using the formula

``````\$original_crop_x = \$show_crop_x / \$show_img_width * \$original_img_width
``````

but not getting correct results…

So… Lets take 1 of these. Original_Crop_W. And call it X. (Because maths and X.)

Original_Img_Width = 1594
Show_Img_Width = 664.
Show_Crop_W = 55

To maintain a proper ratio,
X/1594 = 55/664
Or
X = 55/664 * 1594.

Now take. Original_Crop_H. And call it Y. (Because maths again.)

Original_Img_Height = 1063
Show_Img_Height = 443.
Show_Crop_H = 55

To maintain a proper ratio,
Y/1063 = 55/443
Or
Y = 55/443 * 1063.

Now, for crop_x and crop_y, you’re not looking for ratios anymore.
Orig_Width - X = “The number of pixels that HAVENT been cropped horizontally”
Orig_Height - Y = “The number of pixels that HAVENT been cropped vertically”
So…
Orig_Crop_X = (Orig_Width - X) / 2. (Because i assume you’re trying to crop equally from both sides… you dont quite seem to be doing that though… may need to look into your code for show_crop_x/show_crop_y)
Orig_Crop_Y = (Orig_Height - Y) / 2.

This will give you an X,Y pair that corresponds to the pixel you started cropping at.

im with you all the way up to Orig_Crop_X = (Orig_Width - X) / 2

why divide by two /2

am i missing something?

ive got the new height and width,

still struggling with new x y coords

and thank you

So if i have 1000 pixels.

I crop 500 pixels out of the center. I have 500 pixels left.

But i cropped out of the CENTER.

Which means i have 250 pixels on each side.
500/2.

sorry im still not getting it…

the x y are the top left pixel of the cropped square… why would i need to center anything?

I thought the pixel at 75% of one width would be the same pixel at 75% of larger width

same with the height…

im not cropping from center… im cropping from x y position at w x h

So x/y is arbitrary? Very well.

We’ve determined the W and H of the cropped original area; In your original example, this would be:
W: 132
H: 132
(Note that this makes sense, since your original crop was a square 55x55

to maintain the ratios, you’d have to do the math exactly as you said in your initial post. This gives us:
X = 792
Y = 528

Now here’s where i think you’ve tripped yourself up;
Your \$show_crop X and Y are VERY close to the center of the image - but that isnt the center of the ‘box’ you’re cropping to - it’s the upper left corner. So you’re cropping the area bounded by the coordinates:
330,220
330,275
385,275
385,220

And in the original image:
792,528
924,528
924,660
792,660

This is the expected result. Did your coordinates not come out to the same?

can you believe it i had it right all along… I was asigning width height of the thumbnail wrong when creating it… the coordinates where correct but the thumb i was putting it into was too small…

Im a plonker sometimes

thank you very much for your help i appreciate it