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

わかりました...つまり...最初に、画像を保存したくないので、表示するだけなので、パラメータを1つだけ指定して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サービスがファイルを読み取るときに出力バッファリングがオフになっている場合は、デフォルトのヘッダーを使用してすぐに出力に送信します。(最近html / textヘッダー)。(他のヘッダーがすでに送信されている場合、一部のヘッダーは送信できません。たとえば、html / textが送信された場合、image / jpegは送信できません)

あなたがこれで点灯して働きたくないならば。あなたのシステムがどのように見えるかわかりません。index.phpがブートストラップだと思いますので、変更してください。

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

しかし、phpコードを調べて、phpタグの前後の行/文字を削除するためのより良い解決策。

于 2013-03-10T07:54:49.770 に答える