PHP上传似乎默认为72 dpi,我需要300 dpi。

3
我有一个仅用于打印的页面,其中一些图片是通过我的脚本上传的。似乎无论我在设置imagejpeg()imagepng()的质量时设置了什么,它总是将图像缩小到72 dpi。我使用过自己的个人脚本和github上的脚本。

https://github.com/maxim/smart_resize_image

我希望您能为我提供一些关于如何保留原始300dpi的指导。
这是我的个人脚本。
if (!empty($_FILES['image']['name'])) //checking if file upload box contains a value
    {   
        $saveDirectory = 'pics/';           //name of folder to upload to
        $tempName = $_FILES['image']['tmp_name'];   //getting the temp name on server
        $fileName1 = $_FILES['image']['name'];      //getting file name on users computer

        $count = 1;
        do{
        $location = $saveDirectory . $_GET['section'] . $count . $fileName1;
        $count++; 
        }while(is_file($location));

        if (move_uploaded_file($tempName, $location))   //Moves the temp file on server
            {                                                           //to directory with real name
                $image = $location;

                // Get new sizes
                list($width, $height, $type) = getimagesize($image);    //gets information about new server image

                $framewidth = 932;
                $frameheight = 354;

                $realwidth = $width;    //setting original width and height
                $realheight = $height;

                // Load
                $file1new = imagecreatetruecolor($framewidth, $frameheight);    //creates all black image with target w/h

                if($type == 2){
                    $source = imagecreatefromjpeg($image);
                    imagecopyresampled($file1new, $source , 0, 0, 0, 0, $framewidth, $frameheight, $realwidth, $realheight);
                }
                elseif($type == 3){
                    $source = imagecreatefrompng($image);    
                    imagecopyresampled($file1new, $source , 0, 0, 0, 0, $framewidth, $frameheight, $realwidth, $realheight);
                }
                else{
                    echo "Wrong file type";
                }

                if($type == 2){

                    //creates jpeg image from file1new for file1 (also retains quality)
                    imagejpeg($file1new, $image,100);
                    //frees space
                    imagedestroy($file1new);
                }
                elseif($type == 3){

                    //creates png image from file1new for file1 (also retains quality)
                    imagepng($file1new, $image,10);
                    //frees space
                    imagedestroy($file1new);
                }
                else{
                    echo "Wrong file type";
                } 
            } 
            else 
            {
                echo '<h1> There was an error while uploading the file.</h1>';
            }
        }
}

编辑:即使dpi不是答案,因为我发现特定的jpg图像不保留那些信息。我需要某种方法来保持这些图像非常清晰和清晰。

ImageMagick也会表现出这种行为吗?也可以试一下。 - Ja͢ck
我还没有尝试过使用Image Magic。我现在正在研究它。(我以前从未使用过它)。 - d.lanza38
我已经放弃了GD,转而使用imagick,它真的很棒,你可以找到很多例子来进行图像处理,你从未想过会这么容易...当然,如果它不能做你描述的事情,那还是有点糟糕的;-) - Ja͢ck
嗨@d.lanza38,你能上传一下你找到的最终解决方案吗?我也遇到了同样的问题。 - user961627
顺便提一下,这里的评论我认为很有用:http://stackoverflow.com/questions/2002814/image-processing-creation-in-php-how-to-create-300dpi-images - user961627
显示剩余2条评论
2个回答

3
如果您生成图像并在浏览器中打开,浏览器会在渲染之前将其缩小到72 dpi。如果您使用gimp / phptoshop / 任何图像编辑器打开,则应该保留相同的dpi质量。虽然在屏幕上没有区别,因为您的屏幕是72 dpi。未经新版浏览器测试,但在netscape和第一个firefox版本中是这样的,我认为它自那以后没有改变。

如果我通过FTP上传300dpi的PNG文件(如Photoshop显示)到我的服务器,并通过Web浏览器将其保存到桌面上,它仍然是300dpi。因此,至少对于Firefox 12.0 Windows xp x86中的PNG文件,浏览器不会将dpi降低到72dpi。 - d.lanza38
1
网页的分辨率为72 dpi,无法在网页上显示更高的分辨率。 - Simon Dragsbæk
转发给其他可能会遇到此问题的人。看起来这已经不正确了,因为现在几乎所有的显示器都处理更多的dpi。要查看您的显示器的dpi,请参见此链接:http://www.infobyip.com/detectmonitordpi.php。以前所有的显示器都是72 DPI,但现在情况已经不同了。 - Stephane Gosselin
@Simon - 网络没有dpi。你的显示器有dpi。网络上的图像可以是任何dpi(72、300或其他),而是你的显示器决定了你正在查看的分辨率。 - Stephane Gosselin

0

好的,我已经阅读了这个函数,它似乎返回xdpi和ydpi。我该如何利用这些值来保留我的图像分辨率?此外,这似乎只适用于jpg格式,在这种特定情况下,我只需要jpg保留其分辨率,但我从未意识到我的图像质量从未被保留,我通常允许上传.jpg和.png图像。因此,我想设计一个能够处理两者的函数。 - d.lanza38
我认为这些数字是图像的水平和垂直密度。 - Jeff Hines
是的,但我该如何使用这些数字?很抱歉,我真的不知道。 - d.lanza38
嗯...也许这个页面可以帮助你:https://dev59.com/v1bUa4cB1Zd3GeqPCt0o 你需要安装ImageMagick。 - Jeff Hines
抱歉,我翻译的是程序相关内容,不是您要求的这个。请提供正确的文本以便我进行翻译。 - d.lanza38

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接