【问题标题】:Upload Large Image & Crop Easier上传大图像和裁剪更容易
【发布时间】:2014-03-08 14:53:40
【问题描述】:

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

这里是裁剪的最终函数的代码 sn-p。这是使用我自制的一些功能,理解它们只是为了方便。

//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。

【问题讨论】:

    标签: php crop jcrop


    【解决方案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

    【讨论】:

    • 我觉得自己好蠢,脑子里放了个屁。这是我需要的简单数学,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2013-09-30
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    相关资源
    最近更新 更多