Jump to content

resized image quality sucks


aebstract

Recommended Posts

atlanta.jpg

 

Is there a way to make the image quality better after resize? Here is the script I am using:

 

$idir = "gallery/$_POST[category]/";   // Path To Images Directory
$tdir = "gallery/$_POST[category]/thumbs/";   // Path To Thumbnails Directory
$twidth = "200";   // Maximum Width For Thumbnail Images
$theight = "900";   // Maximum Height For Thumbnail Images

// Uploading/Resizing Script
  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
    $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
      print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From
      $currwidth = imagesx($simg);   // Current Image Width
      $currheight = imagesy($simg);   // Current Image Height


    if ($currheight > $currwidth) {   // If Height Is Greater Than Width
        $zoom = $twidth / $currwidth;   // Length Ratio For Height
        $newwidth = $twidth;   // Width Is Equal To Max Width
        $newheight = $currheight * $zoom;   // Creates The New Height
      } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
        $zoom = $twidth / $currwidth;   // Length Ratio For Height
        $newwidth = $twidth;   // Width Is Equal To Max Width
        $newheight = $currheight * $zoom;   // Creates The New Height
      }




    $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete
      $palsize = ImageColorsTotal($simg);
      for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
      }
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
      imagejpeg($dimg, "$tdir" . $url);   // Saving The Image
      imagedestroy($simg);   // Destroying The Temporary Image
      imagedestroy($dimg);   // Destroying The Other Temporary Image
      print 'Image thumbnail created successfully.';   // Resize successful
    } else {
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
    }
  } else {
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
    print $file_ext;   // Show The Invalid File's Extention
    print '.</font>';
  }
}

Link to comment
Share on other sites

Here's what I use:

<?php
	$tn = imagecreatetruecolor($w, $h);
	$img = imagecreatefromjpeg($fn);
	imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh);
?>

$ow is the original width of image

$oh is the original height

$fn is the file

 

Ken

Link to comment
Share on other sites

<?php
                list($ow, $oh, $type, $attr) = getimagesize($fn);
	if ($oh > $ow) {
		$th = 160;
		$tw = ($th / $oh) * $ow;
	}
	if ($ow >= $oh) {
		$tw = 160;
		$th = ($tw / $ow) * $oh;
	}
	$w = round($tw);
	$h = round($th);
?>

My thumbnails have a max height or width of 160 px.

 

Ken

Link to comment
Share on other sites

My script looks just about the same, and my max is 200 width, no max height. So I am resizing dependent on the width. Here is an update:

 


$idir = "gallery/$_POST[category]/";   // Path To Images Directory
$tdir = "gallery/$_POST[category]/thumbs/";   // Path To Thumbnails Directory
$twidth = "200";   // Maximum Width For Thumbnail Images
$theight = "900";   // Maximum Height For Thumbnail Images

// Uploading/Resizing Script
  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
    $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
      print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From
      $currwidth = imagesx($simg);   // Current Image Width
      $currheight = imagesy($simg);   // Current Image Height




$newwidth = 200;
$deci = 200 / $currwidth;
$newheight = $currheight * $deci;




    $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete
      $palsize = ImageColorsTotal($simg);
      for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
      }
      imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
      imagejpeg($dimg, "$tdir" . $url);   // Saving The Image
      imagedestroy($simg);   // Destroying The Temporary Image
      imagedestroy($dimg);   // Destroying The Other Temporary Image
      print 'Image thumbnail created successfully.';   // Resize successful
    } else {
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
    }
  } else {
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
    print $file_ext;   // Show The Invalid File's Extention
    print '.</font>';
  }
}

 

I changed the imagecopyresized to imagecopyresampled and it didn't affect anything.

 

outlawsareback.jpg

Link to comment
Share on other sites

For size yes, but did you look at the quality difference in the two?

 

http://rbnsn.com/phpfreaks/thumb_test.php?s=900

http://outlawracing.com/gallery/Flyers/thumbs/huntsvillecard.jpg

 

I'm thinking it has something to do with this:

 

      ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);  // Tell The Server What Colors This Image Will Use

 

When you look at it bigger like that, it's like it doesn't have enough colors to represent the image correctly?

Link to comment
Share on other sites

Although, there is a severe quality difference between the two. I just checked, and the colours look very different.

 

What version of PHP and imagegd are you running? If you don't know how to check, then run this script as a new file:

 

<?php

phpinfo();

?>

 

--- EDIT ---

Try this:

 

<?php

$idir = "gallery/$_POST[category]/";   // Path To Images Directory
$tdir = "gallery/$_POST[category]/thumbs/";   // Path To Thumbnails Directory
$twidth = "200";   // Maximum Width For Thumbnail Images
$theight = "900";   // Maximum Height For Thumbnail Images

// Uploading/Resizing Script
  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
    $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
      print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From
      $currwidth = imagesx($simg);   // Current Image Width
      $currheight = imagesy($simg);   // Current Image Height
      $newwidth = 200;
      $deci = 200 / $currwidth;
      $newheight = $currheight * $deci;
      $dimg = imagecreatetruecolor($newwidth, $newheight);   // Make New Image For Thumbnail
      imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
      imagejpeg($dimg, "$tdir" . $url, 100);   // Saving The Image
      imagedestroy($simg);   // Destroying The Temporary Image
      imagedestroy($dimg);   // Destroying The Other Temporary Image
      print 'Image thumbnail created successfully.';   // Resize successful
    } else {
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
    }
  } else {
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
    print $file_ext;   // Show The Invalid File's Extention
    print '.</font>';
  }
}

?>

Link to comment
Share on other sites

Ok that sounds about right. Try the edited part of the post above, and see if it makes any difference.

 

http://outlawracing.com/gallery/Flyers/thumbs/huntsvillecard.jpg

Changed 200 to 600 just to look at quality in a better size. Do you mind explaining what you did to fix this? It seems that whole color portion isn't there anymore.

I think this will work! :D

 

 

Here's another example:

2.jpg

SO much better!! Thanks a lot.

Link to comment
Share on other sites

Glad it's working!

 

The solution: I actually noticed a difference in Ken's code to yours, which was the fact that he was creating a true colour image using a built in function. Your code was trying to manipulate a standard image (i'm assuming it has limited colour palettes) to become a true colour image. That was what the imageColorAllocate function was doing.

 

All I did was get rid of the block of code that did this, and replace the imagecreate() function with imagecreatetruecolor().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.