Dean Bayley
Dean Bayley

Reputation: 27

Calculating Crop Coordinates

I am trying to figure out how to calculate the cropping coordinates for the full size image from a much smaller image, lets say the full size image is 1775 x 2664, the image that the user sees on the cropping page (uses jCrop) is 533 X 800.

The cropping coordinates on the smaller image are: 20,11,230,305

(start from left, start from top, end from left, end from top)

How do i scale the coordinates to the full size image.

I need to allow the user to select the portion of the image on the preview page and then use php to extract the appropriate part of the image from the full size image..

Upvotes: 0

Views: 796

Answers (2)

Timothée Groleau
Timothée Groleau

Reputation: 1950

To give you some reference: http://en.wikipedia.org/wiki/Rule_of_three_%28mathematics%29#Rule_of_Three

// assuming fixed ratio on width and height
// which is the case in your example: ~33% for both dimensions
$FULL_WIDTH = 1775;
$SCALED_WIDTH = 533;

$ratio = $SCALED_WIDTH / $FULL_WIDTH;

$scaled_crop_coordinates = array(20, 11, 230, 305);

$full_crop_coordinates = array();
foreach($scaled_crop_coordinates as $val)
{
    $full_crop_coordinates[] = floor($val / $ratio);
}
var_dump($full_crop_coordinates);

Upvotes: 1

bitWorking
bitWorking

Reputation: 12655

that should work:

left = 20 * (1775/533)
top = 11 * (2664/800)
right = 230 * (1775/533)
bottom = 305 * (2664/800)

Upvotes: 0

Related Questions