dongxiangchan0743 2017-06-06 19:03
浏览 31

Uploadcare没有正确裁剪

Trying to trigger the uploadcare php function with a jquery ajax call, and I get everything to work fine, except for the user selected crop region. It crops to the right ration, but defaults to top left all the time.

What am I missing to pass on the user selected crop area?

Here is the jQuery code that runs the script

$(function() { 
  $('#avatar-uc').each(function() { 
     $("#avatar-uc").find('.uploadcare-widget-button-open').html('<i class="fa fa-camera"></i> Edit Photo'); 
     $("#avatar-uc").find('.uploadcare-widget-button-remove').html('<i class="fa fa-camera"></i> Edit Photo'); 
       WidgetResetAvatar( 
        uploadcare.SingleWidget($(this).children('input'))
      ); 
});
});
function WidgetResetAvatar(widget) { 
  widget.onChange(function(file) {
    if (file) { 
      file.done(function(fileInfo) { 
        $.ajax({
            url: '/asset/create/avatar',
            method: 'POST',
            disablePreview: true,
            headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
            data: {uuid: fileInfo.uuid},
        })
        .done(function( data ) {
          $('#avatarPhoto').css('background-image', 'url("'+data.avatar_url+'")')
        });
      }); 
    }  
    widget.value(null)
  }); 
} 

And the PHP Function

public function CreateAvatar(Request $request)
{
if (!Auth::check()) {
    return Response::json(array('message' => 'not logged in'));
}

$user = Auth::user();

if ($request->ajax()) {
    if (!empty($request->uuid)) {
        $api = app()->uploadcare;
        $original_file = $api->getFile($request->uuid);
        $cropped_file = $original_file->crop(150,150)->copy();
        $cropped_file->store();
        $cropped_file_url = $cropped_file->data['original_file_url'];
        $upload_url = "https://ucarecdn.com";
        $remove_upload_care_url = "";
        $cropped_file_path = str_replace($upload_url, 
$remove_upload_care_url, $cropped_file_url);
    }
    else {
        $cropped_file_path = "";
    }

    $user->avatar = $cropped_file_path;
    $user->save();

    return Response::json(['avatar_url' => $cropped_file_url]);
}
}
  • 写回答

1条回答 默认 最新

  • douzangdang2225 2017-06-07 09:57
    关注

    For some reason, crop() function does not accept coordinates for the cropping area, just to center it or not (see the code).

    Easiest way is to build the URL by hand. Bonus points for sending PR to the library for accepting coordinates.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算