做了一个项,现在碰见一个问题就是在处理头象时要上传图片,上传图片后要进行裁剪,裁剪后的小图预览是BASE64的方式存在,现在想把这个在服务器上生成图片,生成图片就是把这个BASE64数据进行传递,传递过程中要用到AJAX,但是就是得不到数据。
裁剪完成后,得到一个DATAURL,这个要能过AJAX传递到 /uploadimg/index,
clipFinish: function(dataURL) {
// 裁剪完成的回调函数。this指向图片对象,会将裁剪出的图像数据DataURL作为参数传入
data=encodeURIComponent(dataURL);
$.ajax({
url: '/uploadimg/index/',
data: {'str':data},
type: 'post',
dataType: 'JSON',
})
alert(dataURL);
uploadimg/index 如下:
if (! defined ( 'BASEPATH' ))
exit ( 'No direct script access allowed' );
class Uploadimg extends CI_Controller {
function __construct() {
parent::__construct ();
}
public function index() {
if ($this->session->userdata ( 'manage_role' ) == '') {
header ( "Content-Type:text/html;charset=utf-8" );
echo 'alert("请登录 !");';
echo 'window.location.href="' . site_url ( 'index' ) . '";';
exit ();
}
if ($this->session->userdata ( 'manage_role' ) != '10') {
header ( "Content-Type:text/html;charset=utf-8" );
echo 'alert("您没有此操作权限 !");';
echo 'window.location.href="' . site_url ( 'index' ) . '";';
exit ();
}
$php_path = dirname(__FILE__) . '/';
$php_url = dirname($_SERVER['PHP_SELF']) . '/';
//文件保存目录路径
$save_path = $php_path . '/../../public/attached';
echo $php_path."
";
echo $php_url."
";
echo $save_path."
";
$base64_image_contentx = $_POST["str"];;
$base64_image_content = htmlspecialchars($base64_image_contentx);
base64_image_content($base64_image_content,$save_path);
header ( "Content-Type:text/html;charset=utf-8" );
echo 'alert('base64_imaghe_content');';
echo 'window.location.href="#";';
exit ();
}
}
function base64_image_content($base64_image_content,$path){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = $path."/".date('Ymd',time())."/";
if(!file_exists($new_file)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return '/'.$new_file;
}else{
return false;
}
}else{
return false;
}
}
?>
请高手指点一下,如何做?
或有其它方法也可以介绍一下。谢了