0

I am using codeIgniter and want to create a dynamic text on image everything working fine but i am confused how to make a textbox for text on image and and how to convert that image into real image here is my code.

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);
}

I want to convert this rowimage into real image.

<img class='img-responsive' src='data:image/jpeg;base64," . base64_encode( $rawImageBytes ) . "' />
Komal khan
  • 55
  • 7
  • try it http://stackoverflow.com/questions/17810501/php-get-base64-img-string-decode-and-save-as-jpg-resulting-empty-image – kiran gadhvi Jun 03 '16 at 11:18

1 Answers1

0

To save a base64 encoded image to a file based image you can use smth like this (in case you meant this with "convert into real image"):

    $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);

Edit: I never tried, but giving a quick search a go something like this might do the magic:

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;
}
SoenkeM
  • 48
  • 5
  • what is $type and $base..? list($type, $base64stream) = explode(';', $base64stream); list($base, $base64stream) = explode(',', $base64stream); – Komal khan Jun 03 '16 at 11:43
  • The parts of the standard encoded base64 string you don't need - the solution using list() twice might not be most beautiful... You know how list() works? I took this example out of a bigger method I used for a certain case - in your case the 'data:image/png' and 'base64' are not needed for further handling. – SoenkeM Jun 03 '16 at 11:58
  • i am writing text on textbox with this function imagettftext($image, 20, 0, 400, 160, $white, $font, $string); thanks its working fine please also tell me how to wrape text on textbox..? – Komal khan Jun 03 '16 at 12:03
  • Great, then plz accept the answer. What do you mean with "wrape text on textbox"? – SoenkeM Jun 03 '16 at 12:05
  • I want to place my text on an image (dynamic text) but want to keep the text in a wrapper (it should not cross the width of the wrapper and automatically shift to new row). What should I do for it? – Komal khan Jun 03 '16 at 12:10
  • i am checking and goin to tell you its working or not.. :) – Komal khan Jun 03 '16 at 12:23
  • Might also help: http://stackoverflow.com/questions/28064492/wrap-text-in-imagettftext – SoenkeM Jun 03 '16 at 12:26
  • all this method does is wrapping the text you put in. So you put your String into the method, give it the right size of the image and use the result in your original function. – SoenkeM Jun 03 '16 at 12:36
  • Thank you very much now its working..! :D i am so happy :) – Komal khan Jun 03 '16 at 13:08
  • great @Komalkhan ... Please **accept my answer** then. – SoenkeM Jun 03 '16 at 13:16
  • @Komalkhan just to let you know - if somebody helps you here and it works fine for you, you should hit the button and accept the answer. It's the least you can do to thank for the help. – SoenkeM Jun 06 '16 at 07:35