Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Last active August 29, 2015 14:05
Show Gist options
  • Save alastairparagas/1e4c229a55ec089f5c38 to your computer and use it in GitHub Desktop.
Save alastairparagas/1e4c229a55ec089f5c38 to your computer and use it in GitHub Desktop.
Image Crop that sets the the image's new width and height based on the ratio of the x/y coordinates when using some Javascript image cropping tool.
<?php
$image = $inputs['image'];
/*
When I refer to the "front-end image", I mean the uploaded version of the image you see
in jCrop/cropping screen in the webpage of the site, NOT the actual file.
Omitted variable declarations:
$imageName = name you want to provide to the image file
$imageDestination = path where the image is being sent to
image-duringHeight = Height of the cropped section of the image from the front-end
image-duringWidth = Width of the cropped section of the image from the front-end
image-x = Top-left x coordinate of the cropped section of the image from the front-end
image-y = Top-left y coordinate of the cropped section of the image from the front-end
image-width = Height of the whole image from the front-end
image-height = Width of the whole image from the front-end
getimagesize($image)[0] = Real width of the image file
getimagesize($image)[1] = Real height of the image file
*/
$imageSize = getimagesize($image);
if($inputs['image-duringWidth'] && $inputs['image-duringHeight']){
/*
Scale of the image from the front-end to the actual image file.
If ratio is 1, they have the same dimensions.
If less than 1, the front-end image is smaller than the actual image.
If greater than 1, the front-end image is bigger than the actual image.
If you multiple the result of the division below by 100, you get the percentage
of how bigger or smaller the front-end image is compared to the actual image
*/
$imageWidthRatio = $imageSize[0] / $inputs['image-duringWidth'];
$imageHeightRatio = $imageSize[1] / $inputs['image-duringHeight'];
/*
imageX and imageY gets the actual top-left x-y coordinates relative to the original
image's dimensions, because our original image's size may be different to the
front-end uploaded image's size.
*/
$imageX = $inputs['image-x'] * $imageWidthRatio;
$imageY = $inputs['image-y'] * $imageHeightRatio;
/*
imageWidth and imageHeight gets the actual width and height of the cropped area
relative to the original image's dimensions because again, our original image's size may
be different to the front-end uploaded image's size.
*/
$imageWidth = $inputs['image-width'] * $imageWidthRatio;
$imageHeight = $inputs['image-height'] * $imageHeightRatio;
switch( $image->getMimeType() ){
case "image/jpeg":
case "image/jpg":
$imageBinary = imagecreatefromjpeg($image);
break;
case "image/png":
$imageBinary = imagecreatefrompng($image);
break;
default:
$imageBinary = imagecreatefromstring(file_get_contents($image));
}
$imageDestination = imagecreatetruecolor($imageWidth, $imageHeight);
imagecopyresampled($imageDestination, $imageBinary, 0, 0, $imageX, $imageY, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
imagepng($imageDestination, FirebaseModel::getStoredPath($imageName));
imagedestroy($imageDestination);
}else{
// Do nothing because the user did not choose an area to crop
//- no cropped section width/height!
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment