在解码php

时间:2015-05-12 09:57:34

标签: php json

嗨,我正在重新获得base64编码的图像版本,我必须通过将其裁剪为小尺寸来存储它。现在我无法裁剪图像请帮助我 我的代码如下.. 请帮助我裁剪基地64图像

    $data = str_replace('data:image/png;base64,', '', $note['file']);
                $data = str_replace(' ', '+', $data);
                $decodedFile = base64_decode($data);
$file = fopen($destination , 'wb');


if(!fwrite($file, $decodedFile)){
                //return("ERROR: can't save file to $destination");
                return '-1';
            }
                fclose($file);

2 个答案:

答案 0 :(得分:2)

您可以使用gd库从二进制代码创建图像文件:

function binaryToFile($binary_imagen, $width, $height, $new_name, $url_destiny) {

    try{
        //actual size
        $info    = getimagesizefromstring($binary_imagen);
        $old_width = $info[0];
        $old_height = $info[1];

        //new resource
        $resource = imagecreatefromstring($binary_imagen);

        $resource_copy  = imagecreatetruecolor($width, $height);

        imagealphablending( $resource_copy , false );
        imagesavealpha( $resource_copy , true );

        imagecopyresampled($resource_copy, $resource, 0, 0, 0, 0,
                   $width, $height, 
                   $old_width, $old_height);

        $url = $url_destiny."/".$new_name".png";
        $final = imagepng($resource_copy, $url, 9);

        imagedestroy($resource);
        imagedestroy($resource_copy);

        return 1;

    }catch (Exception $e) {
        return 0;
    }

}

答案 1 :(得分:1)

不确定您需要什么,但如果您想要实现这样的目标,那么可能会对您有所帮助

PHP crop image from base64_decode