Thumbnailing using PHP results in degraded image quality

$sourcePath = 'images/'; // Path of original image
$sourceUrl = '';
$sourceName = 'photo1.jpg'; // Name of original image
$thumbPath = 'thumbs/'; // Writeable thumb path
$thumbUrl = 'thumbs/';
$thumbName = "test_thumb.jpg"; // Tip: Name dynamically
$thumbWidth = 100; // Intended dimension of thumb

// Beyond this point is simply code.
$sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName");
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

$targetImage = imagecreate($thumbWidth,$thumbWidth);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));
imagejpeg($targetImage, "$thumbPath/$thumbName");

// By now, the thumbnail is copied into the $thumbpath
// as the file name specified in $thumbName, so display
echo "<img src='$thumbUrl$thumbName' alt=''>";

      

The above code gives me a thumbnail and is great, but the image quality is terrible. It looks like the image has flipped the colors and it looks like it's squashed. I had headaches all day while doing this. Does anyone have any idea?

0


source to share


3 answers


Use imagecreatetruecolor instead of imagecreate and imagecopyresampled instead of imagecopyresized.



+18


source


The third parameter should be included, as Dominique points out. It determines the quality of the jpeg.

In the question "and it looks like it was squashed", remember that you are making a square sketch from an original image, which itself may or may not be square.

One way to get around this is to work with the source's dimensions to work out full width or full height (depending on whether the image is portrait or landscape) square to copy from the source. This means replacing "0,0,0,0" in your arguments with imagecopyresized () with dynamic computation.



(EDIT: example)

function makeSquareThumb($srcImage, $destSize, $destImage = null) {
    //I'm sure there a better way than this, but it works...
    //I don't like my folder and file checking in the middle, but need to illustrate the need for this. 

    $srcFolder = dirname($srcImage); //source folder
    $srcName = basename($srcImage); //original image filename

    //the IF ELSEIF ELSE below is NOT comprehensive - eg: what if the dest folder is the same as the source?
    //writeable nature of the destination is not checked!
    if(!destImage) {
        $destFolder = $srcFolder.'/thumbs/';
        if(!is_dir($destFolder)) {
            //make the thumbs folder if there isn't one!
            mkdir($destFolder);
        }
        $destImage = $destFolder.$srcName;
    } elseif (is_dir($destImage)) {
        $destFolder = $destImage;
        $destImage = $destFolder.'/'.$srcName;
    } else {
        $destFolder = dirname($destImage);
    }


    //Now make it!
    $srcCanvas = imagecreatefromjpeg($srcImage);
    $srcWidth = imagesx($srcCanvas);
    $srcHeight = imagesy($srcCanvas);

    //this let us easily sample a square from the middle, regardless of apsect ratio.
    $shortSide = array($srcWidth,$srcHeight);
    sort($shortSide);

    $src_x = $srcWidth/2 - $shortSide[0]/2;
    $src_y = $srcHeight/2 - $shortSide[0]/2;

    //do it!
    $destCanvas = imagecreatetruecolor($destSize, $destSize);
    imagecopyresampled($destCanvas,$srcCanvas,0,0,$src_x,$src_y,$destSize,$destSize,$shortSide[0],$shortSide[0]);
    imagejpeg($destCanvas, $destImage);
}

      

+7


source


Try:

imagejpeg($targetImage, "$thumbPath/$thumbName", 100);

      

+1


source







All Articles