4

重複の可能性:
PHP と GD で画像リソースのサイズをバイト単位で取得する方法は?

PHPを使用してオブジェクト$imageのファイルサイズ(画像サイズの寸法ではない)を取得することは可能ですか? これを「Content-Length:」ヘッダーに追加したいと思います。

$image = imagecreatefromjpeg($reqFilename);
4

2 に答える 2

3

これにはfilesize()を使用できます。

 // returns the size in bytes of the file
 $size = filesize($reqFilename);

上記はもちろん、サイズ変更された画像がディスクに保存されている場合にのみ機能し、呼び出し後に画像のサイズを変更する場合はimagecreatefromjpeg()、@One Trick Ponys ソリューションを使用して、次のようにする必要があります。

  // load original image
  $image = imagecreatefromjpeg($filename);
  // resize image
  $new_image = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  // get size of resized image
  ob_start();
  // put output for image in buffer
  imagejpeg($new_image);
  // get size of output
  $size = ob_get_length();
  // set correct header
  header("Content-Length: " . $size);
  // flush the buffer, actually send the output to the browser
  ob_end_flush();
  // destroy resources
  imagedestroy($new_image);
  imagedestroy($image);
于 2013-01-31T23:13:07.683 に答える
3

私はこれがうまくいくと思います:

$img = imagecreatefromjpeg($reqFilename);

// capture output
ob_start();

// send image to the output buffer
imagejpeg($img);

// get the size of the o.b. and set your header
$size = ob_get_length();
header("Content-Length: " . $size);

// send it to the screen
ob_end_flush();
于 2013-01-31T23:19:26.637 に答える