调整图像和显示大小,而不保存 [英] Resize image and display without saving it

查看:135
本文介绍了调整图像和显示大小,而不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个图片库,并且照原样保存图片,但不裁剪图片。我想调整图像的大小,而它被加载在控制器中的图像,所以当我在浏览器中加载控制器,它显示图像重新调整大小到任何我想要的。我已将方法添加到 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)); // send fileize header
header('Content-Type:'。get_mime_by_extension($ image)); // send mime-type header
header('Content-Disposition:inline; filename ='。basename($ image)。';'); // send filename header
exit($ image_content); //读取并将文件输出到输出缓冲区
}

得到许多错误,包括标题错误。我错了什么?



错误:(如果有用)

 消息:imagejpeg():文件名不能为空

消息:无法修改标题信息 - 已发送的标题(输出开始于/Volumes/www/vhosts/ci/system/core/Exceptions.php :185)

消息:strrchr()期望参数1是字符串,给定资源

消息:不能修改头信息 - 头发信息/www/vhosts/ci/system/core/Exceptions.php:185)

消息:basename()期望参数1为字符串,给定资源

消息:不能修改头信息 - 已发送的头(输出开始于/Volumes/www/vhosts/ci/system/core/Exceptions.php:185)


解决方案

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

  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);
imagedestroy($ image);
// imagedestroy($ thumbImage);不要在显示之前销毁:)
ob_end_clean(); //清理输出缓冲区...如果打开。
header('Content-Type:image / jpeg');
imagejpeg($ thumbImage); //你不想保存..只是显示
imagedestroy($ thumbImage); //但不需要,导致脚本在下一行退出并释放使用的内存
exit;
}

请写我,有什么变化,错误...
并建议阅读: http ://php.net/manual/en/function.imagecopyresized.php



并且:

 消息:无法修改头信息 - 已发送的头(输出开始于/Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

告诉我,错误的异常php ...
请检查,是BOM字符在一个代码编辑器把这些irritatign字符放在php代码...



和任何其他文件(出现此错误消息)应该像这样检查。
有时候一些字符在php标签之前...如果webservice读取文件时关闭输出bufering,则使用默认标头发送到输出中。
(最近的html /文本标题)。 (如果其他标头已发送,则无法发送某些标头。例如,如果发送了html /文字,则无法发送image / jpeg)



工作照亮这个。我不知道你的系统如何。
我假设index.php是你的引导,改变它。

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

但是更好的解决方案来检查你的php代码,并删除php标记之前和之后的行/ / p>

I've built an image gallery and saved the image "as is" without cropping it. I want to resize the image on the fly while it's loaded in the controller, so when I load the controller in the browser it displays the image re-sized to whatever I want. I've added the method to MY_Loader, here is the code.

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
}

From this code, I'm getting many errors including header errors. What am I doing wrong?

Errors: (if useful)

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)

解决方案

Ok... so... the fist thing is, that you does not want to save image, just display so you should use imagejpeg with only one parameter.

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

fist round i recommend this changes. Please write me, what changes with errors... And recommend to read: http://php.net/manual/en/function.imagecopyresized.php

And this :

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

Says me that something is wrong with the exception php... please check, is BOM character at the begining of the file... or is spaces, newlines after .. or before the php tag.. some code editor puts these irritatign characters in php code...

And any other files (that appears this error message) should be checked like this. Sometimes some characters left before the php tag... and if output bufering is turned off as the webservice reads the file, send to the output inmedietly with the default header. ( recently html/text header ) . ( some headers can't be sent if other header sent already.. for example if html/text sent, image/jpeg cannot be sent )

if you do not want to work a lit with this. I doesnt know how your system looks like. I assume that index.php is your bootstrap, change it.

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

But better solution to examine your php codes, and delete lines / characters before and after php tag.

这篇关于调整图像和显示大小,而不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆