我想将base64_encode图像转换为图像

时间:2016-06-03 11:15:24

标签: php image codeigniter ob-start

我正在使用codeIgniter,并希望在图像上创建动态文本,一切正常,但我很困惑如何在图像上制作文本文本框,以及如何将图像转换为真实图像,这是我的代码。

public function convertimage()
{
    ob_start();
    $font = realpath(APPPATH . '../assets/fonts/OpenSans-Regular.ttf');
    $string = 'here is my text for image';

     $image = imageCreateFromJpeg(base_url('assets/images/winer.jpg'));
     $white = imagecolorallocate($image, 255, 255, 255);

     imagettftext($image, 20, 0, 400, 160, $white, $font, $string);

     imagejpeg($image,NULL,100);
     $rawImageBytes = ob_get_clean();
     echo "<img class='img-responsive' src='data:image/jpeg;base64," . base64_encode( $rawImageBytes ) . "' />";
    imagedestroy($image);
}

我想将此rowimage转换为真实图像。

<img class='img-responsive' src='data:image/jpeg;base64," . base64_encode( $rawImageBytes ) . "' />

1 个答案:

答案 0 :(得分:0)

要将base64编码的图像保存到基于文件的图像,您可以使用这样的smth(如果您的意思是“转换为真实图像”):

    $base64stream = 'data:image/png;base64,iVBORw0K...';
    $imgPath = '/your/image/path/including/name.png';

    list($type, $base64stream) = explode(';', $base64stream);
    list($base, $base64stream)      = explode(',', $base64stream);
    $data = base64_decode($base64stream);

    file_put_contents($imgPath, $data);

编辑:我从来没有尝试过,但快速搜索一下这样的事情可能会有所作为:

function wrap($fontSize, $angle, $fontFace, $string, $width){
    $ret = "";
    $arr = explode(' ', $string);
    foreach ( $arr as $word ){
        $teststring = $ret.' '.$word;
        $testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
        if ( $testbox[2] > $width ){
            $ret.=($ret==""?"":"\n").$word;
        } else {
            $ret.=($ret==""?"":' ').$word;
        }
    }
    return $ret;
}