问题描述
我想在不使用任何PHP扩展名(例如imageMagick)的情况下检索图像(JPEG,PNG,SVG,GIF)的图像分辨率(DPI).我到处搜索,但是找不到完美的解决方案.我尝试了以下代码(从链接获得)
I want to Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension (like imageMagick). I searched everywhere, but I couldn't find a perfect solution. I tried below code (got from link)
function get_dpi($filename){
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,0,4);
return array(hexdec($x),hexdec($y));
}
但是我没有获得正确的水平和垂直DPI.例如,我使用96dpi和96dpi的图像,但得到了(100,100).此功能仅适用于JPEG文件格式.
But I am not getting the correct Horizontal and vertical DPI. For example, I used an image with 96dpi and 96dpi, but I got (100,100).And this function is only for JPEG file formats.
推荐答案
图像的DPI通常是虚构的.很少是在最终渲染的物理尺寸实际上很重要的情况下创建的图像(就图像本身而言).也就是说,DPI信息存储在JPEG的EXIF数据中,因此您可以使用内置的PHP函数:
The DPI of an image is usually a matter of fiction. Rarely is an image created where the physical dimensions of the final rendering actually matter (as far as the image itself is concerned). That said, the DPI information is stored in the EXIF data of a JPEG so you can read it from there with the built-in PHP function:
<?php
$filename = "/Users/quentin/Dropbox/Camera Uploads/2016-03-30 21.01.09.jpg";
$exif = exif_read_data($filename);
?>
DPI is <?php echo $exif["XResolution"] ?> by <?php echo $exif["YResolution"] ?>
这篇关于无需使用任何PHP扩展即可检索图像(JPEG,PNG,SVG,GIF)的图像分辨率(DPI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!