开发者

Show image in php script

I have some images without extension in their name. Now, when I want to open them some browsers can't detect data as image and show up the binary text.

What is the right script (php) which will take the address of such image, detect it's format开发者_如何转开发 and set right headers with image output?


You can use the exif_imagetype function to properly detect what type of image it is, then the image_type_to_mime_type function to get the mime type you want:

header("Content-type: " . image_type_to_mime_type(exif_imagetype("image.jpg")));

You'll need exif support builtin though. I recommend this because it actually checks the signature of the file versus blindly assuming the mime type based on extension. However, if this isn't an option, you'll have to fallback to fileinfo as others have mentioned.


You should check fileinfo() http://www.php.net/manual/en/function.finfo-file.php Fileinfo detects the filetype and depending on that u can set your header.


You could use this :

<?php $filename = 'http://static.php.net/www.php.net/images/php.gif'; $file = fopen($filename, 'rb'); $size = getimagesize($file); switch ($size['mime']) { case "IMAGETYPE_GIF": echo "<strong class="highlight">Image</strong> is a gif"; break; case "IMAGETYPE_JPEG": echo "<strong class="highlight">Image</strong> is a jpeg"; break; case "IMAGETYPE_PNG": echo "<strong class="highlight">Image</strong> is a png"; break; } ?>


You could use finfo_file() to determine the MIME type of the file in question and then send a Content-Type header with that information in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜