最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fengyuanchen jQuery cropper plugin - how to get cropped canvas - Stack Overflow

programmeradmin2浏览0评论

How to get crop the image using my own button?

I tried to execute this

var canvas = $selector.cropper('getCroppedCanvas')

but it's returning null value.

Is there a way to get the cropped canvas? And how can I put the cropped canvas data into <input type="file"> value and send it to PHP?

How to get crop the image using my own button?

I tried to execute this

var canvas = $selector.cropper('getCroppedCanvas')

but it's returning null value.

Is there a way to get the cropped canvas? And how can I put the cropped canvas data into <input type="file"> value and send it to PHP?

Share Improve this question edited May 14, 2015 at 8:10 Mark Rotteveel 109k229 gold badges156 silver badges220 bronze badges asked May 14, 2015 at 7:40 HitoriHitori 5891 gold badge5 silver badges21 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Your selector should be the HTML container which contains the image: The Javascript and HTML should be like as mentioned below:

$img = $('#uploader-preview');

$img.cropper('getCroppedCanvas');
var canvaURL = canvas.toDataURL('image/jpeg'); // it returns the cropped image / canvas
<img src="" id="uploader-preview">

Send Canvas Image to PHP:

var photo = canvas.toDataURL('image/jpeg');                
$.ajax({
  method: 'POST',
  url: 'photo_upload.php',
  data: {
    photo: photo
  }
});

Here's PHP Script
photo_upload.php

<?php
	
	$data = $_POST['photo'];
	list($type, $data) = explode(';', $data);
	list(, $data)      = explode(',', $data);
	$data = base64_decode($data);

	mkdir($_SERVER['DOCUMENT_ROOT'] . "/photos");

	file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/photos/".time().'.png', $data);
	die;
?>

What is $selector? If it something like this:

var $selector = $(".selector");

Then you need call getCroppedCanvas() function to jQuery wrapper. Because if you write:

var canvas = $selector.cropper('getCroppedCanvas');

It calls cropper getCroppedCanvas function to DOM element, not to jQuery element.

Write something like this:

var $selector = $(".selector"); 
var canvas = $($selector).cropper('getCroppedCanvas');

And it will be work fine. If you want save canvas data as image on server, you can read this answer

发布评论

评论列表(0)

  1. 暂无评论