doujing6053 2016-02-01 09:42
浏览 63
已采纳

使用PHP解码时,来自cropit的Base64图像将被剪裁

I'm using the cropit jquery plugin to manage image cropping on my website. The way I have it setup is that cropit will give me a base 64 string that I'll pass to PHP which will decode it and place it in the folder. The issue is that when I decode the string it will only make about 1/10 of the image, the rest will just be white / transparent. My code is as follows:

jQuery:

    var username = "<?php echo $userData['username']; ?>";
    $('#image-cropper').cropit({
        imageState:{
            src:'users/'+username+'/profile_picture.jpg'
        },
    });   

    $('#image-cropper').cropit('previewSize', {width:500, height:500});


    $('.export').click(function() {
        var imageData = $('#image-cropper').cropit('export');
        //$("#code").val(imageData);
        window.open(imageData);
    }); 

PHP:

function decode ($base64) {
    list($type, $base64) = explode(';', $base64);
    list(, $base64)      = explode(',', $base64);
    $code = base64_decode($base64);

    echo $userData['username'];

    file_put_contents('users/' . $userData['username'] . '/profile_picture.png', $base64);
}

The code I have here was working when I had the width/height of $('#image-cropper').cropit('previewSize', {width:500, height:500}); set to 250. I had to change it because without a larger width/height it would save a very low resolution image which is still an issue but not as major. Any help would be great. Thanks!

base64 viewed in browser: enter image description here

base64 when decoded with PHP: enter image description here

  • 写回答

2条回答 默认 最新

  • dongmu9253 2016-02-01 10:28
    关注

    The data URI scheme that the export function is using as size limitations (depending on the browser).

    As the cropit export function allows to tweak the image format and compression factor, you could try to save in jpeg and adjust the quality for best results inside the data URI limits:

    // Returns the cropped image in Data URI format.
    // The second argument `options` takes the following keys:
    // - [type='image/png'] exported image type.
    // - [quality=.75] exported image quality, only works when type is
    //     'image/jpeg' or 'image/webp'.
    // - [originalSize=false] when set to true, export cropped part in
    //     original size, overriding exportZoom.
    // - [fillBg='#fff'] if `type` is 'image/jpeg', this color will be
    //     filled as the background of the exported image.
    
    $imageCropper.cropit('export', {
      type: 'image/jpeg',
      quality: .9,
      originalSize: true
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)