调整图像大小并显示而不保存

Sar*_* B. 4 php gd codeigniter

我已经建立了一个图片库并“按原样”保存了图像而没有裁剪它。我想在图像加载到控制器中时动态调整它的大小,所以当我在浏览器中加载控制器时,它会显示调整到我想要的任何大小的图像。我已将方法添加到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
}
Run Code Online (Sandbox Code Playgroud)

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

错误:(如果有用)

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)
Run Code Online (Sandbox Code Playgroud)

Kov*_*vge 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;
  }
Run Code Online (Sandbox Code Playgroud)

第一轮我建议进行此更改。请写信给我,错误有什么变化......并推荐阅读: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)
Run Code Online (Sandbox Code Playgroud)

说我的异常 php 有问题...请检查,文件开头是 BOM 字符...还是空格、换行符 ..在 php 代码中...

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

如果你不想用这个点亮。我不知道你的系统是什么样的。我假设 index.php 是你的引导程序,改变它。

   <?php
       ob_start();
       .... 
       ob_end_flush();
   ?>
Run Code Online (Sandbox Code Playgroud)

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