2013-01-31 1 views
4

가능한 중복 :
How to get image resource size in bytes with PHP and GD?PHP : PHP에서 동적으로 생성 된 이미지의 크기를 바이트 단위로 가져올 수 있습니까?

그것은 PHP를 사용하여 객체 $ 이미지의 파일 크기 (안 이미지 크기 치수)를 얻을 수 있습니까? 이것을 "Content-Length :"헤더에 추가하고 싶습니다.

$image = imagecreatefromjpeg($reqFilename); 
+0

'ob_start()','ob_clean()','ob_flush()'등과 같은 http://www.php.net/manual/en/ref.outcontrol.php에 대해 더 자세히 읽어야합니다. – Cyclonecode

답변

2

당신은 단지 이것에 대한 filesize()을 사용할 수 있습니다 크기가 조정 된 이미지는 imagecreatefromjpeg()에 전화 후 이미지 크기를 조정한다면 디스크에 저장 한 경우

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

물론 위의 의지에만 작동 당신은 @One 트릭 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); 
+1

동적으로 생성 된 이미지는 클라이언트에 보내기 전에'filesize'를 사용하기 위해 먼저 서버의 파일에 저장되어야한다는 메모를 추가하십시오. – brezanac

+1

은 저장된 이미지의 크기 만 가져옵니다. 하지만 이미지를 수정하면 크기가 변경됩니다. – user2030856

+0

"imagejpeg ($ new_image);"앞에 헤더를 설정해야합니다. – user2030856

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(); 
+0

+1 부분은'ob_' 부분이지만 파일이 이미 디스크에 저장되어 있다면 간단히'filesize()'를 실행하면됩니다. 이미지를 브라우저에 보내기 전에 크기를 조정하거나 자르기 또는 다른 조작을하면 솔루션이 분명 가장 좋습니다 =) – Cyclonecode

+0

이미지가 표시되는 방법에 따라 다릅니다. 원본 파일이 아니라면 파일 크기가 달라집니다. 왜냐하면 gd가 jpeg 파일을 다시 압축하기 때문입니다 ... –

+0

"imagejpeg ($ new_image); 앞에 헤더를 설정해야합니다. – user2030856

관련 문제