How to view image without an extension via direct link in the browser?

Asked

Viewed 865 times

3

Example - http://meusite/imagem/001

Result I do not want, I want to view 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 will be able to use something similar to AddType application/x-httpd- jpg

  • I think you should wear image/jpeg.

2 answers

2

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

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

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

In the same folder create a file called 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 implement cache and use Etag along with this code above, as I used in this question example (not the answer):

0

This answer is because you have the [PHP] tag on the question.

With PHP:

You can enter the Header in PHP:

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

The document type will vary, so you should define the image/png only for images are of the same format, it is usually possible to identify the format through its extension, as .png.

So you 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 the .htaccess (or change in the httpd.conf if turned off) to force all files to have one content-type.

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

This will cause all requests to /imagem leave with the Content-type predefined. ;)

More information on the ForceType can be seen here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.