Php 上传大图像&;收成容易

Php 上传大图像&;收成容易,php,crop,jcrop,Php,Crop,Jcrop,我想允许用户上传他们家的大照片,并将其裁剪成适合幻灯片的大小。因此,我设置了这样的设置,当用户上传他们的大家庭照片时,它会存储并复制它,然后重新调整新副本的大小,使其更易于管理。(例如,将5000x333px图像调整为600x400px)然后向用户显示此新图像,以允许用户裁剪该图像。裁剪图像后,将返回4个值:x、y、w和h。这些是较小图像上裁剪区域的值,但现在我们要裁剪原始图像,而不是较小的图像。这意味着w&h必须增加,x&y必须保持在正确的位置,但这是我非常困惑的部分。我如何适当地放大w&h并

我想允许用户上传他们家的大照片,并将其裁剪成适合幻灯片的大小。因此,我设置了这样的设置,当用户上传他们的大家庭照片时,它会存储并复制它,然后重新调整新副本的大小,使其更易于管理。(例如,将5000x333px图像调整为600x400px)然后向用户显示此新图像,以允许用户裁剪该图像。裁剪图像后,将返回4个值:x、y、w和h。这些是较小图像上裁剪区域的值,但现在我们要裁剪原始图像,而不是较小的图像。这意味着w&h必须增加,x&y必须保持在正确的位置,但这是我非常困惑的部分。我如何适当地放大w&h并将x&y保持在正确的位置,以便将小图像的裁剪与原始大图像的裁剪相匹配

下面是最终生成的函数的代码片段。这是使用我的一些自制功能,了解他们只是为了方便

//User inputs from the crop area on the small image
$user_input_x = $_POST['x'];
$user_input_y = $_POST['y'];
$user_input_w = $_POST['w'];
$user_input_h = $_POST['h'];

//Grab original, small, and final image locations 
$original_image_src = '/tmp/original_image';
$small_image_src = '/tmp/small_image';
$final_image_location = '/final/image';

//Return the extension for both the original and small image
if(($image_ext = imageCheck($original_image_src)) === false) die('Could not find original image source!');
$original_image_src .= $image_ext;
$small_image_src .= $image_ext;
$final_image_location .= $image_ext;

//Get the width and height of both the original and small image
list($original_image_width, $original_image_height) = getimagesize($original_image_src);
list($small_image_width, $small_image_height) = getimagesize($small_image_src);

//Converts string location of image into php resource
//This function helps determine the type of image and create the resource
$src_image = createImage($original_image_src);

//This is the area where I am having trouble
//I need to scale up the users input x,y,w,h because they are from small image and need to match to original


//These are the vars to go into all the final fields
$src_x = $user_input_x;
$src_y = $user_input_y;
$src_w = 0;
$src_h = 0;

$crop_x = 0;
$crop_y = 0;
$crop_w = 0;
$crop_h = 0;

$final_image = imagecreatetruecolor($crop_w, $crop_h);
if(!imagecopyresampled($final_image, $src_image, $crop_x, $crop_y, $src_x, $src_y, $crop_w, $crop_h, $src_w, $src_h)) die('Could not resmaple image!');

//Saves image to final location retains the extension and destroys the resource
if(imageSave($final_image, $final_image_location, $image_ext) === false) die('Count not save image!');

哦,如果有帮助的话,这个裁剪是由jCrop完成的,它几乎提供了x,y,w和h.

据我所知,x和w,y和h的比例是相同的

$crop_y = $original_image_height/$small_image_height*$user_input_y;
$crop_h = $original_image_height/$small_image_height*$user_input_h;
$crop_w = $original_image_width/$small_image_width*$user_input_w;
$crop_x = $original_image_width/$small_image_width*$user_input_x;
我画这个是为了让它形象化。

我觉得自己太蠢了,脑子里放了个屁。这是我需要的简单数学,谢谢。