2

I want to create image in codeigniter as we do in simple php using GD functions. i have tried the following method in controller

function image(){
$my_img = imagecreate( 200, 80 );
        $background = imagecolorallocate( $my_img, 0, 0, 255 );
        $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
        $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
        imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
          $text_colour );
        imagesetthickness ( $my_img, 5 );
        imageline( $my_img, 30, 45, 165, 45, $line_colour );
        //$this->output->set_content_type('image/png');
         header( "Content-type: image/png" );
        imagepng( $my_img );
        imagecolordeallocate( $line_color );
        imagecolordeallocate( $text_color );
        imagecolordeallocate( $background );
        imagedestroy( $my_img );
        }

If i write this code in simple php we can see in url like http://www.domain.com/image.php but if i run in codeigniter like http://www.domain.com/codeigniter/image then i am unable to get image.

i have searched all around but unable to find how to create image in codeigniter . i have also found that the captcha helper create the image in same way.

7
  • This is actually working just fine. What is going wrong in there?
    – enapupe
    May 28, 2014 at 14:01
  • if call this only image is not create.. May 28, 2014 at 14:04
  • Explain what do you want to do with this image, after generating it.
    – enapupe
    May 28, 2014 at 14:19
  • actually i want to create image on the fly, so i want see in url May 28, 2014 at 14:22
  • To do what with the url?
    – enapupe
    May 28, 2014 at 14:24

2 Answers 2

1

you mentioned you are trying to create a url that displays the image. try this:

function image(){
    $my_img = imagecreate( 200, 80 );
    $background = imagecolorallocate( $my_img, 0, 0, 255 );
    $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
    $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
    imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
      $text_colour );
    imagesetthickness ( $my_img, 5 );
    imageline( $my_img, 30, 45, 165, 45, $line_colour );
    ob_start();
    imagepng( $my_img );
    $image_string = ob_get_flush();
    $imageb64 = base64_encode($image_string);
    imagecolordeallocate( $line_color );
    imagecolordeallocate( $text_color );
    imagecolordeallocate( $background );
    imagedestroy( $my_img );
    $url = "data:image/png;base64,".$imageb64;
    return $url;
}

in order to displace this image using a url, you

4
  • could you modify the above code...it would be helpful for me. May 28, 2014 at 14:09
  • i want image data in string...ob_start() not working May 28, 2014 at 14:19
  • @AmmarHayderKhan please see further edits. I'm not sure what you want to do with the string once you get it, but I assumed you want to return it from the function image().
    – jtmanteo
    May 28, 2014 at 14:24
  • 1
    @AmmarHayderKhan this edit is my last try. I'm still not sure what you are trying to do. Do you want the function to return a string that is a url you can display for someone to click on and see the image (that's what I have written up here)? Or do you want the browser to immediately render the image? You haven't made that clear.
    – jtmanteo
    May 28, 2014 at 14:31
0

In case you want to do typical image manipulations like watermarking, resizing, cropping, etc. and since you are already using Code Igniter, I wanted to link you to imagemoo

http://www.matmoo.com/digital-dribble/codeigniter/image_moo/

Of course, it depends on what you want to accomplish as an output image. for me, I find its good to create from base PNG image file and then manipulate it from there.

3
  • i dont want to manipulate image ...i just want to create a new image. May 28, 2014 at 14:24
  • Ammar, just in case you didn't notice. you used imagepng() without filename. it won't save any file and it will dumps all into the browser screen
    – fedmich
    May 28, 2014 at 14:25
  • 1. Try to comment out header( "Content-type: image/png" ); while you are debugging 2. close the browser you have and re-open it
    – fedmich
    May 28, 2014 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.