How to display an image without an extension through the direct link in the browser?

3

Example - http://meusite/imagem/001

Result that I do not want, I want to see the image.

ÿØÿà�JFIF��H�H��ÿÛ�C�



ÿÛ�C        

ÿÀ�p�"�ÿÄ������������
    ÿÄ�7����!1A"Q2a¡Bq‘#3R$CbScðÿÄ�����������ÿÄ�)�������!1A"Q2a#BÿÚ���?�ù¶¨/d99ñI­”s/•[0ò¹mömç_'ŒkY™å?e¬I~FŽfŒ™Nk³©àévÊ=¿"øÝãøÏÎ×GUÅüQÛ¯Ñþ  ~9ƧâÚ=/ƒÁ¦1ŽÒ93ÉÑŽs\Äÿ�Sðÿ�Œð?—†µ£³t×_Z0¹)AïG?zÙp‘Ée×Ûèϳô™Uù6P–®–’65sµdÜž‰1p'‘%¨¶tœwÄ­Êš~
Ç   ð7œ c­¸ëµÃqÿ�œâ·XÊø‹œè=Z

My server has Apache you can use something like AddType application/x-httpd- jpg

    
asked by anonymous 12.09.2016 / 16:51

2 answers

2

Using only .htaccess is not possible, this is because there are several types of image and it would be complicated to add mime-type to each one, however you can redirect to a php handle, for example:

Create a .htaccess: file with this content in the image folder:

RewriteEngine On
RewriteRule ^ proxy_image.php?path=$0 [L]

In the same folder, create a file named proxy_image.php with the following content:

<?php

function mimeType($file)
{
    $mimetype = false;

    if (class_exists('finfo')) {//PHP5.4+
        $finfo     = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype  = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
        $mimetype = mime_content_type($file);
    }

    return $mimetype;
}

if (empty($_GET['path'])) {
    echo 'Caminho invalido';
    exit;
}

$path = $_GET['path'];

$extensoes = array('jpeg', 'jpg', 'gif', 'png', 'ico', 'svg');

$fullPath = null;

//Busca pela primeiro arquivo que contiver a extensão (pode implementar glob também, é opicional)
foreach ($extensoes as $extensao) {
    if (is_file($path . '.' . $extensao)) {
        $fullPath = $path . '.' . $extensao;
        break;
    }
}

if ($fullPath) {
    //pega o mime-type do arquivo
    $mime = mimeType($fullPath);

    //Verifica se o mime-type é do tipo imagem
    if (strpos($mime, 'image/') === 0) {
        header('Content-Type: ' . $mime);

        readfile($path);
        exit;
    }
}

echo 'Isto não é uma imagem';
exit;

Then just navigate to something like http://localhost/images/foobar , if there is an image with this

Note that it is possible to implement caching and use Etag along with this code above, as I used in this example of the question (not the answer):

12.09.2016 / 20:00
0
  

This response is due to the fact that it has the [PHP] tag in the question.

With PHP:

You can enter Header in PHP:

header('Content-type: image/png');

The type of the document will vary, so you should set image/png only for images are of the same format, you can usually identify the format through its extension, such as .png .

So I could do something like:

$tipo = [
'gif' => 'image/gif',
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg'
];

$nome = 'image.png';
$extensao = strtolower(substr(strrchr($nome,"."),1));

if( isset( $tipo[$extensao] )) {
   header('Content-type: '.$tipo[$file_extension]);
}

//... { Exibe um "echo" da imagem }

With Apache

You can use .htaccess (or change in httpd.conf if off) to force all files to have a same content-type .

<Location /imagem>
  ForceType image/jpeg
</Location>

This will cause all requests to /imagem to exit with the previously set Content-type . ;)

More information about ForceType can be viewed here.

    
12.09.2016 / 18:53