4

我已经建立了一个图片库并“按原样”保存了图片,没有裁剪它。我想在控制器中加载图像时动态调整图像大小,因此当我在浏览器中加载控制器时,它会显示调整大小为我想要的图像。我已将方法添加到MY_Loader,这是代码。

function show_image($image, $width, $height) {
    $this->helper('file');
    $image_content = read_file($image);

    //resize image
    $image = imagecreatefromjpeg($image);
    $thumbImage = imagecreatetruecolor(50, 50);
    imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, $width, $height);
    imagejpeg($thumbImage,"",85);
    imagedestroy($image);
    imagedestroy($thumbImage);

    header('Content-Length: '.strlen($image_content)); // sends filesize header
    header('Content-Type: '. get_mime_by_extension($image)); // send mime-type header
    header('Content-Disposition: inline; filename="'.basename($image).'";'); // sends filename header
    exit($image_content); // reads and outputs the file onto the output buffer
}

从这段代码中,我收到了很多错误,包括标题错误。我究竟做错了什么?

错误:(如果有用)

Message: imagejpeg(): Filename cannot be empty

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Message: strrchr() expects parameter 1 to be string, resource given

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Message: basename() expects parameter 1 to be string, resource given

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)
4

1 回答 1

5

好的......所以......第一件事是,你不想保存图像,只是显示所以你应该使用只有一个参数的 imagejpeg。

   function show_image($image, $width, $height) {
        //$this->helper('file');                   why need this?
        //$image_content = read_file($image);      We does not want to use this as output.

        //resize image           
        $image = imagecreatefromjpeg($image);
        $thumbImage = imagecreatetruecolor(50, 50);
        imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, $width, $height);
        imagedestroy($image);
        //imagedestroy($thumbImage); do not destroy before display :)
        ob_end_clean();  // clean the output buffer ... if turned on.
        header('Content-Type: image/jpeg');  
        imagejpeg($thumbImage); //you does not want to save.. just display
        imagedestroy($thumbImage); //but not needed, cause the script exit in next line and free the used memory
        exit;
  }

第一轮我推荐这个改变。请写信给我,错误会发生什么变化......并推荐阅读: http: //php.net/manual/en/function.imagecopyresized.php

和这个 :

       Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

说我的异常 php 有问题...请检查,是文件开头的 BOM 字符...还是空格、.. 之后或 php 标记之前的换行符.. 某些代码编辑器放置了这些 irritatign 字符在php代码中...

并且应该像这样检查任何其他文件(出现此错误消息)。有时在 php 标记之前会留下一些字符......并且如果在 web 服务读取文件时关闭了输出缓冲,则使用默认标题立即发送到输出。(最近的 html/文本标题)。(如果已经发送了其他标头,则无法发送某些标头。例如,如果发送了 html/text,则无法发送 image/jpeg)

如果您不想为此工作。我不知道你的系统是什么样子的。我假设 index.php 是您的引导程序,请更改它。

   <?php
       ob_start();
       .... 
       ob_end_flush();
   ?>

但更好的解决方案是检查您的 php 代码,并在 php 标记之前和之后删除行/字符。

于 2013-03-10T07:54:49.770 回答