php - 调整图像大小并在不保存的情况下显示

标签 php codeigniter gd

我已经建立了一个图片库并“按原样”保存图像而不裁剪它。我想在图像加载到 Controller 中时即时调整图像大小,因此当我在浏览器中加载 Controller 时,它会显示图像大小调整到我想要的大小。我已将方法添加到 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
}

从这段代码中,我遇到了很多错误,包括 header 错误。我做错了什么?

错误:(如果有用的话)

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)

最佳答案

好吧……所以…… 第一件事是,你不想保存图像,只是显示 所以你应该使用只有一个参数的 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 标记之前...一些代码编辑器将这些烦人的字符放在 php 代码中...

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

如果你不想用它来工作。我不知道你的系统是什么样的。 我假设 index.php 是您的 Bootstrap ,更改它。

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

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

关于php - 调整图像大小并在不保存的情况下显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319944/

相关文章:

php - 从上传的图像创建缩略图

php - 如果右键单击复选框,将出现另一个表单填写的高级表单填写

php - 使用 javascript 或 php 脚本清除 cookie

php - Mysql查询脚本奇怪

php - Codeigniter Right Join with Where 条件未给出结果

PHP:如何将 "near white"表示为颜色?

php - Magento 的 Mage::log() 导致白屏

javascript - 将值从 javascript 传递到 html 和 codeigniter

php - 日期时间格式 null 值返回 PHP 中的默认时间

php - 使用 PHP GD 计算文本宽度