2013-03-10 68 views
3

我已經建立了一個圖片庫和保存圖像「原樣」無需裁剪它。我想在加載到控制器中的同時實時調整圖像大小,因此當我在瀏覽器中加載控制器時,它會根據我的需要顯示圖像的大小。我已將該方法添加到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) 
+0

您好!你可以在這裏粘貼錯誤信息嗎? 問題可以是previousli發送了頭,等... PHP代碼之前 HTML代碼..等等... – Kovge 2013-03-10 07:30:57

+0

補充一下上面 – 2013-03-10 07:35:50

+0

沒有HTML正在裝載IM剛剛從控制器測試調用它。 – 2013-03-10 07:49:29

回答

4

好的...所以... 拳頭的事情是,你並不想保存圖像,只顯示 所以你應該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標籤之前留下了一些字符...並且如果在webservice讀取文件時關閉輸出緩衝,則使用默認標題立即發送到輸出。 (最近的html /文本標題)。 (如果要是HTML /文本發送其它頭已經發送。例如一些頭不能被髮送,圖像/ JPEG不能被髮送)

,如果你不想工作,與此點燃。我不知道你的系統是怎樣的。 我認爲index.php是你的引導,改變它。

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

但更好的解決方案來檢查您的php代碼,並刪除php標籤之前和之後的行/字符。

+0

我得到的錯誤是消息:imagejpeg():當我關閉標題('')時,26不是有效的圖像資源;因爲標題說由於錯誤無法加載圖像。 – 2013-03-10 08:01:44

+0

imagedestroy($ thumbImage);我忘了這個。在發送到輸出之前不要銷燬。 我改變了我評論中的代碼。 – Kovge 2013-03-10 08:05:10

+0

tahnk你!它的工作,沒有看到。我還發現了一個代碼庫,它可以實現它http://www.matmoo.com/digital-dribble/codeigniter/image_moo/ – 2013-03-10 10:15:01

相關問題