duanpei4455 2013-01-21 14:35
浏览 54

裁剪图像无法正常工作,存储错误的部分

So this is the original picture including the rectangle, I want to create a cropped Image from enter image description here

And this is what I get after the cropping enter image description here

So it can be seen, that the new image has the right dimensions but the wrong part is being cropped. Here's the JS:

$(document).ready(function()
    {
        $('#cropimage').Jcrop(
        {
            aspectRatio: 3 / 4,
            maxSize: [150,200],
            onSelect: updateCoords
        });
    });
    function updateCoords(c)
    {
        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);
    };

And here is the PHP code

function crop($_POST)
{   
    $clipX      = (int)$_POST['x'];
    $clipY      = (int)$_POST['y'];
    $filename   = (string)$_POST['image'];
    $resizedHeight  = (int)$_POST['h'];
    $resizedWidth   = (int)$_POST['w'];

    // Original image's details
    $original   = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'images/user_pictures/' . DIRECTORY_SEPARATOR . $filename;

    $dimensions     = getimagesize($original);
    $old_width  = $dimensions[0];
    $old_height     = $dimensions[1];

    // image = original_image
    $old_image  = call_user_func('imagecreatefrom' . 'jpeg', $original);


    // Crop image
    if (function_exists('imagecreatetruecolor') && ($new_image = imagecreatetruecolor($resizedWidth, $resizedHeight)))
        imagecopyresampled($new_image, $old_image, 0, 0, $clipX, $clipY, $resizedWidth, $resizedHeight, $old_width, $old_height);

    imagejpeg($new_image,'images/user_pictures/'.$this->getUserID().'_picture.jpg');
}

I never used those php functions before but I've read some tutorials and I dont see any errors there. But there must be at least 1 ... what am I doing wrong ? The while original Image seems to be resized for whatever reason.

  • 写回答

1条回答 默认 最新

  • duanheyi7147 2013-01-21 14:42
    关注

    if you want to crop based on (x,y,w,h) (10,15,30,35) then your function would be:

    imagecopyresampled ( $dst_image , $src_image , 0, 0 , 10 , 15 , 30-10 , 35-15 , 30-10 , 35-15 )
    

    since you are copying 20x20 from the original into a new image, those dimensions are your new and dst_w, dst_h as well as your src_w, src_h.

    $old_width and $old_height are now the complete width of the original image while they should be the width and height of the cropped part.

    $old_width = $resizedWidth;
    $old_height = $resizedHeight;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)