2011-06-07 44 views
17

php cómo obtener el tamaño de la imagen web en kb?php cómo obtener el tamaño de la imagen web en kb?

getimagesize solo obtiene el ancho y alto.

y filesize causaron waring.

$imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg"); 
echo $imgsize; 

Warning: filesize() [function.filesize]: stat failed for http://static.adzerk.net/Advertisers/2564.jpg

¿Hay alguna otra manera de conseguir un tamaño de imagen Web en kb?

+1

posible duplicado de [PHP: tamaño del archivo remoto sin descarga de archivos] (http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file) – deceze

+0

Esto parece estar relacionado: [link] http://stackoverflow.com/questions/2145021/php-getimagesize-alternatives-without-javascript [/ link] – knurdy

Respuesta

18

corto de hacer una petición HTTP completa, no existe una manera fácil:

$img = get_headers("http://static.adzerk.net/Advertisers/2564.jpg", 1); 
print $img["Content-Length"]; 

es probable que pueda utilizar cURL sin embargo para enviar un lighter HEAD request instead.

+0

genial, get_headers se ejecuta más rápido. Gracias. –

+2

Asegúrate de que tu cliente HTTP no esté enviando ningún encabezado diciendo que acepta respuestas HTTP con gzip, o de lo contrario el 'Content-Length' será incorrecto porque el servidor enviará datos comprimidos de vuelta. – Darien

+0

@Darien: Excelente captura! Afortunadamente, 'get_headers' envía una solicitud HTTP/1.0 bastante simple. Pero para cURL esto necesitaría más diligencia. – mario

3

Eso suena como un problema de permisos porque filesize() debería funcionar bien.

Aquí se muestra un ejemplo:

php > echo filesize("./9832712.jpg"); 
1433719 

Asegúrese de que los permisos se establecen correctamente en la imagen y que el camino también es correcto. Tendrá que aplicar algunos cálculos matemáticos para convertir de bytes a KB, pero después de hacerlo, ¡debería estar en buena forma!

5
<?php 
$file_size = filesize($_SERVER['DOCUMENT_ROOT']."/Advertisers/2564.jpg"); // Get file size in bytes 
$file_size = $file_size/1024; // Get file size in KB 
echo $file_size; // Echo file size 
?> 
1

Aquí es un buen enlace con respecto tamaño del archivo()

No puede utilizar filesize() para recuperar información de archivos remoto. En primer lugar, debe ser descargado o determinarse mediante cualquier método

Usando aquí Curl es un buen método:

Tutorial

1

Puede utilizar también esta función

<?php 
$filesize=file_get_size($dir.'/'.$ff); 
$filesize=$filesize/1024;// to convert in KB 
echo $filesize; 


function file_get_size($file) { 
    //open file 
    $fh = fopen($file, "r"); 
    //declare some variables 
    $size = "0"; 
    $char = ""; 
    //set file pointer to 0; I'm a little bit paranoid, you can remove this 
    fseek($fh, 0, SEEK_SET); 
    //set multiplicator to zero 
    $count = 0; 
    while (true) { 
     //jump 1 MB forward in file 
     fseek($fh, 1048576, SEEK_CUR); 
     //check if we actually left the file 
     if (($char = fgetc($fh)) !== false) { 
      //if not, go on 
      $count ++; 
     } else { 
      //else jump back where we were before leaving and exit loop 
      fseek($fh, -1048576, SEEK_CUR); 
      break; 
     } 
    } 
    //we could make $count jumps, so the file is at least $count * 1.000001 MB large 
    //1048577 because we jump 1 MB and fgetc goes 1 B forward too 
    $size = bcmul("1048577", $count); 
    //now count the last few bytes; they're always less than 1048576 so it's quite fast 
    $fine = 0; 
    while(false !== ($char = fgetc($fh))) { 
     $fine ++; 
    } 
    //and add them 
    $size = bcadd($size, $fine); 
    fclose($fh); 
    return $size; 
} 
?> 
0

Usted puede obtener el tamaño del archivo mediante el uso de la función() get_headers. Utilice a continuación código:

$image = get_headers($url, 1); 
    $bytes = $image["Content-Length"]; 
    $mb = $bytes/(1024 * 1024); 
    echo number_format($mb,2) . " MB"; 
Cuestiones relacionadas