上传大图和&裁剪更容易

时间:2014-02-11 01:34:09

标签: php crop jcrop

我想允许用户上传他们家的大照片并将其裁剪成适合幻灯片放映的内容。所以我设置当用户上传他们的大型家庭照片时,它将存储并复制它并重新调整新副本的大小以使其更易于管理。 (例如,将5000x3333px图像的大小调整为600x400px)然后会向用户显示此新图像以允许它们裁剪该图像。裁剪图像后,返回4个值:x,y,w和h。这些是较小图像上裁剪区域的值,但现在我们要裁剪原始图像而不是较小的图像。这意味着w& h必须增加和x&你必须保持正确的位置,但这是我很困惑的部分。如何正确扩大w& amp; 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&小时。

1 个答案:

答案 0 :(得分:1)

据我所知,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;

我画了这个尝试并想象它。 http://i58.tinypic.com/32zm0it.jpg