2011-09-12 7 views

Respuesta

4

supongo getimagesize:

list($width, $height, $type, $attr) = getimagesize("path/to/image.jpg"); 

if (isset($type) && in_array($type, array(
    IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) { 
    ... 
} 
+5

Leer documentación: No use getimagesize() para verificar que un archivo dado sea una imagen válida. http://php.net/manual/en/function.getimagesize.php –

-1

utilizo esta función ... comprueba las direcciones URL demasiado

function isImage($url){ 
    $params = array('http' => array(
       'method' => 'HEAD' 
      )); 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp) 
     return false; // Problem with url 

    $meta = stream_get_meta_data($fp); 
    if ($meta === false){ 
     fclose($fp); 
     return false; // Problem reading data from url 
    } 
} 
+0

sí, tienes razón ... agregó el corchete. –

24

exif_imagetype es una solución mejor.

Este método es más rápido que usar getimagesize. Para citar php.net "El valor de retorno es el mismo valor que getimagesize() regresa en el índice 2 pero exif_imagetype() es mucho más rápido".

if(exif_imagetype('path/to/image.jpg')) { 
    // your image is valid 
} 
+1

Por supuesto, esto solo funciona cuando la extensión EXIF ​​está habilitada. En mi caso, no es el caso y no tengo control sobre esto :( –

-1

utilizo este:

function is_image($path) 
{ 
    $a = getimagesize($path); 
    $image_type = $a[2]; 

    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP))) 
    { 
     return true; 
    } 
    return false; 
} 
1

exif_imagetype es mucho más rápido que getimagesize y no utiliza GD-Lib (dejando una huella más ligera mem)

function isImage($pathToFile) 
{ 
    if(false === exif_imagetype($pathToFile)) 
    return FALSE; 

    return TRUE; 
} 
2

Según las recomendaciones del PHP documentation:

"No use getimagesize() para verificar que un archivo dado sea una imagen válida. Utilice una p solución construida en el lugar, como la extensión Fileinfo en su lugar ".

Aquí se muestra un ejemplo:

$finfo = finfo_open(FILEINFO_MIME_TYPE); 
$type = finfo_file($finfo, "test.jpg"); 

if (isset($type) && in_array($type, array("image/png", "image/jpeg", "image/gif"))) { 
    echo 'This is an image file'; 
} else { 
    echo 'Not an image :('; 
} 
Cuestiones relacionadas