在codeigniter中动态创建图像

时间:2014-05-28 13:57:48

标签: php codeigniter

我想在codeigniter中创建图像,就像在使用GD函数的简单php中一样。我在控制器

中尝试了以下方法
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 );
        }

如果我在简单的php中编写此代码,我们可以在网址中看到http://www.domain.com/image.php  但如果我在http://www.domain.com/codeigniter/image之类的codeigniter中运行 然后我无法获得图像。

我已经四处搜索但无法找到如何在codeigniter中创建图像。我还发现验证码助手以相同的方式创建图像。

2 个答案:

答案 0 :(得分:1)

您提到过您正在尝试创建一个显示图片的网址。试试这个:

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

为了使用网址取代此图片,您

答案 1 :(得分:0)

如果您想进行典型的图像处理,如水印,调整大小,裁剪等。 由于您已经在使用Code Igniter,我想将您链接到 imagemoo

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

当然,这取决于您想要作为输出图像完成的任务。 对我来说,我发现从基础PNG图像文件创建它然后从那里进行操作就很好。