Search code examples
phpthumbnails

PHP - Thumbnail created from high definition uploaded image returning blurry


I've got an issue which potentially could be a really easy fix, but I'm struggling to figure it out. So here's the issue:

When I upload a high definition photo in my upload script, the thumbnail returns blurry. For example:

The original photo below of our future supreme leader Elon musk looks like this: https://i.sstatic.net/T91t4.jpg

The thumbnail returns something like this: https://i.sstatic.net/9Vb6B.jpg

As you can see, the thumbnail is so blurry where I wouldn't wanna display it on a webpage. I'll leave a snippet of the PHP code to create the thumbnail:

mkdir($_SERVER["DOCUMENT_ROOT"]."/threads/$id/thumbnails/", 0755);
                            $filepath = "/threads/$id/".$fileName;
                            $filepath_thumb = "/threads/$id/thumbnails/".$fileName;
                                                
                            if($fileType == "image/jpeg")
                            {
                            $imagecreate = "imagecreatefromjpeg";
                            $imageformat = "imagejpeg";
                            }
                            if($fileType == "image/png")
                            {                        
                            $imagecreate = "imagecreatefrompng";
                            $imageformat = "imagepng";
                            }
                            if($fileType == "image/gif")
                            {                        
                            $imagecreate= "imagecreatefromgif";
                            $imageformat = "imagegif";
                            }
                                                        
                            // Adjust size of thumbnail

                            $maxwidth = 250;
                            $maxheight = 250;

                            if ($fileheight > $filewidth) 
                            {   
                            $ratio = $maxheight / $fileheight;  
                            $newheight = $maxheight;
                            $newwidth = $filewidth * $ratio; 
                            }
                            else 
                            {
                            $ratio = $maxwidth / $filewidth;   
                            $newwidth = $maxwidth;  
                            $newheight = $fileheight * $ratio;   
                            }

                            $image = $imagecreate($_SERVER["DOCUMENT_ROOT"]."$filepath");
                            $image_p = imagecreatetruecolor($newwidth, $newheight);
                                                                                        
                            imagecopyresized($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $filewidth, $fileheight);                     
                            $imageformat($image_p, $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb"); // Thumbnail Folder

Solution

  • If you don't pass quality parameters to imagejpeg or imagepng, they default to sub-optimal quality settings. So you need to know if you are you are processing a JPG or PNG, and handle accordingly.

    $outputPath = $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb";
    
    if($fileType == "image/jpeg")
    {
        $imageformat($image_p, $outputPath, 100);
    }
    elseif($fileType == "image/png")
    {
        $imageformat($image_p, $outputPath, 9);
    }
    else
    {
        $imageformat($image_p, $outputPath);
    }
    

    You also may have better results with imagecopyresampled rather than imagecopyresized. I have always use imagecopyresampled for thumbnails, I seem to recall that it produces better results when reducing size.