【问题标题】:Create image in codeigniter on the fly即时在 codeigniter 中创建图像
【发布时间】:2014-05-28 13:57:48
【问题描述】:

我想在 codeigniter 中创建图像,就像我们在简单的 php 中使用 GD 函数一样。我在控制器中尝试了以下方法

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 编写这段代码,我们可以在 url 中看到 http://www.domain.com/image.php 但是如果我像http://www.domain.com/codeigniter/image这样在codeigniter中运行 然后我无法获得图像。

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

【问题讨论】:

  • 这实际上工作得很好。那里出了什么问题?
  • 如果调用这个唯一的图像不是创建..
  • 说明你想在生成这张图片后做什么。
  • 实际上我想动态创建图像,所以我想在 url 中查看
  • 用 url 做什么?

标签: php codeigniter


【解决方案1】:

您提到您正在尝试创建一个显示图像的 url。试试这个:

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

为了使用 url 替换此图像,您

【讨论】:

  • 你能修改上面的代码吗...对我有帮助。
  • 我想要字符串中的图像数据...ob_start() 不起作用
  • @AmmarHayderKhan 请查看进一步的编辑。我不确定一旦得到字符串你想对它做什么,但我假设你想从函数 image() 中返回它。
  • @AmmarHayderKhan 这次编辑是我最后一次尝试。我仍然不确定您要做什么。您是否希望该函数返回一个字符串,该字符串是您可以显示供某人单击并查看图像的 url(这就是我在这里写的内容)?还是您希望浏览器立即呈现图像?你还没有说清楚。
【解决方案2】:

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

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

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

【讨论】:

  • 我不想操作图像...我只想创建一个新图像。
  • Ammar,以防您没有注意到。您使用了没有文件名的 imagepng()。它不会保存任何文件,它会将所有文件转储到浏览器屏幕中
  • 1.尝试注释掉 header("Content-type: image/png" );调试时 2. 关闭浏览器并重新打开
猜你喜欢
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 2012-01-04
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多