使用Ajax问题上传Yii Crop Image

时间:2015-01-20 10:32:04

标签: php jquery yii yii-extensions

您好我只想在yii扩展程序中使用navajcrop上传图片。使用ajax调用上传图像。我可以上传图片了。但问题是上传的图像无法查看。当我查看上传的图像时,它显示错误: - 解释JPEG图像文件时出错(不是JPEG文件:以0x75 0xab开头)

控制器代码: -

public function actionUpload(){
    $model = new Register();
    if(isset($_POST)){
        $rnd = rand(0, 9999);
       $img = $_POST['image']; 

        if($img != '/img/noimage.png' && $img != $model->image){  
            $img = str_replace('data:image/jpeg;base64,', '', $img);


            $img = str_replace(' ', '+', $img);
           $data = base64_decode($img); 

            $file = Yii::app()->basePath . '/../themes/front/assets/img/photo/' . $rnd.'.jpg'; 

                       file_put_contents($file, $data);

        }
    }
}

JS代码: -

function doSomething(obj,res){ //the 'obj' is IMG tag, 'res' is base64image

    $.ajax({
        cache: false,
        type: 'post',
        //url: <?php echo Yii::app()->createUrl('site/upload');?>,
        url: baseUrl+"/admin/ajax/Upload",
        data: 'image='+res,
        success: function(){ 
            obj.attr('src',res);
        }
    });
}

查看代码: -

<?php            $this->widget('ext.NavaJcrop.ImageJcrop', array(
    'config' => array(
        'title'=>$model->image,
        'image'=>$model->image,//required, all field below are not required.
        'id'=>'nava-jcrop',
        //'unique'=>true,
        'buttons'=>array(
            'cancel'=>array(
                'name'=>'Cancel',
                'class'=>'button-crop',
                'style'=>'margin-left: 5px;',
            ),
            /*'edit'=>array(
                'name'=>'Edit',
                'class'=>'button-crop',
                'style'=>'margin-left: 5px;',
            ),*/
            'crop'=>array(
                'name'=>'Crop',
                'class'=>'button-crop',
                'style'=>'margin-left: 5px;',
            )
        ),
        'options'=>array(
            'imageWidth'=>150,
            'imageHeight'=>175,
            'resultStyle'=>'position: fixed;top: 400px;max-width:350px;max-height:350px;z-index: 9999;',
            'resultMaxWidth'=>350,
            'resultMinWidth'=>350,
        ),
        'callBack'=> array(
            'success'=>"function(obj,res){doSomething(obj,res);}",
            'error'=>"function(){alert('error');}",
        )

    )
));  ?>

请帮助解决这个错误。

1 个答案:

答案 0 :(得分:0)

您好我已经解决了我的问题,这是以下语法中的错误。

控制器操作中的

: - 编辑

$ img = str_replace(&#39; data:image / png; base64,&#39;,&#39;&#39;,$ img);

$ file = Yii :: app() - &gt; basePath。 &#39; /../主题/前/资产/ IMG /照片/&#39; 。 $ RND&#39; png格式&#39 ;;

相关问题