3

I have a page while is used only for print, and some of the images on there are uploaded through a script I have. It seems to always reduce the image to 72 dpi, reguardless of what I set imagejpeg() and imagepng() to for quality.

I've used my own personal script and this one on git hub

https://github.com/maxim/smart_resize_image

I was hoping for a little guidance on preserving the dpi at the original 300dpi.

Here is my own personal script

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>';
            }
        }
}

Edit: Even if dpi isn't the answer, as I see jpgs in specific don't retain that information. I need some way of keeping these images very clear and crisp.

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • Does ImageMagick exhibit this behaviour as well? Might give that a try as well. – Ja͢ck May 03 '12 at 23:29
  • I have not tried anything with Image magic yet. I'm looking into it now. (I've never used it before). – d.lanza38 May 04 '12 at 15:38
  • I've ditched GD for imagick, it's just awesome and you can find so many examples to do image manipulation you never knew were so easy ... of course, if it can't do what you described it would still suck ;-) – Ja͢ck May 04 '12 at 16:22
  • I was looking into it and ImageMagick isn't really an option for me. I have an up to date version of GD installed though, and am using the technique here http://us3.php.net/manual/en/function.imageconvolution.php#104006 but while my images come out ten times better on the screen, when printed they are still pixelated. After doing a little bit of math, it seems as if the images are 96dpi rather then the original 300dpi based on the width and height in inches and how many pixels the images are being reported as. 300px X 450px, 3.125" X 4.688". – d.lanza38 May 07 '12 at 15:12
  • I also have a new uploading script, not too much different then the one I originally posted, but it does have some improvements. If you would like it posted I can add it to my original post. – d.lanza38 May 07 '12 at 15:15
  • Hi @d.lanza38, yes could you please upload the final solution you found? I'm having the same problem. – user961627 Jan 13 '13 at 07:56
  • By the way, one of the comments here is useful I think: http://stackoverflow.com/questions/2002814/image-processing-creation-in-php-how-to-create-300dpi-images – user961627 Jan 13 '13 at 07:59

2 Answers2

3

If you generate image and open with a browser, the browser will reduce it to 72dpi before rendering. If you open with gimp/phptoshop/whatever image editor , it should preserve the same dpi quality. Though on a screen, there is no difference since your screen is 72 dpi.

Not tested on new browsers, but it was like this in netscape and first firefox versions, I assume it has not changed since.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • Well if I upload a 300dpi png (as Photoshop displays) to my server via ftp and save it through the web browser to my desktop, it remains 300 dpi. So at least for pngs in firefox 12.0 Windows xp x86, the browser will not reduce the dpi to 72dpi. – d.lanza38 May 04 '12 at 15:37
  • 1
    The web is 72 dpi it does not allow you to show higher resolution on the web – Simon Dragsbæk Nov 21 '12 at 08:41
  • Reposting for others that may stumble here. It appears this is no longer correct since just about any moniter nowadays handles more dpi's. To see how your moniter fares, see this link: http://www.infobyip.com/detectmonitordpi.php. The moniters used to be all 72 DPI, this is no longer the case. – Stephane Gosselin Jan 16 '13 at 18:11
  • @Simon - The web has no dpi's. Your monitor has dpi's. The images on the web can be of any dpi (72, 300, whatever) it is your monitor that decides at what resolution you are viewing. – Stephane Gosselin Jan 16 '13 at 18:14
0

The function posted by lorezyra (at) lorezyra (dot) com here: http://www.php.net/manual/es/function.imagejpeg.php#85712 might do the trick.

Jeff Hines
  • 3,511
  • 1
  • 19
  • 21
  • Okay, I read through the function, and it seems it returns the xdpi and ydpi. How would I make use of these values to retain the resolution of my images? Also, this seems as if it only would work with jpgs, for this particular instance I only NEED jpgs to retain their resolution, but I never realized the quality of my images were never being retained and I usually allow .jpg and .png images to be uploaded. So I'd like to come up with a function that would be able to handle both. – d.lanza38 May 04 '12 at 15:34
  • I think those numbers are the horizontal and vertical density of the image. – Jeff Hines May 04 '12 at 16:50
  • Yeah, but how to I use those numbers? I'm sorry, I really just don't know. – d.lanza38 May 04 '12 at 17:06
  • Hmmm... Maybe this page might help: http://stackoverflow.com/questions/5892731/how-to-check-photo-dpi-with-php You would need to install ImageMagick I think – Jeff Hines May 04 '12 at 17:35
  • EDIT: Posted in the wrong spot, this post is actually up the screen a bit. – d.lanza38 May 07 '12 at 15:10