tags:

views:

773

answers:

4
$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 that's great, but the image quality is awful. It looks like the image has had the colours inverted and it looks like it has been squashed. I've had headaches all day doing this. Any one have any ideas?

A: 

Try:

imagejpeg($targetImage, "$thumbPath/$thumbName", 100);
Dominic Rodger
+14  A: 

Use imagecreatetruecolor instead of imagecreate and imagecopyresampled instead of imagecopyresized.

tst
This sorts out the quality, thankyou very much! Any ideas about the squashedness?
Drew
Squashedness - yes. See my answer.
philistyne
+6  A: 

The third parameter is worth including as Dominic points out. It specifies the jpeg quality.

On the issue of "and it looks like it has been squashed", remember, you're making a square thumbnail from a source image which itself may or may not be square.

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

(EDIT: example)

function makeSquareThumb($srcImage, $destSize, $destImage = null)
{
//I'm sure there's 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's 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);
}
philistyne
Thankyou very much! :D
Drew
A: 

Muchas gracias funciono al 100% si se arreglo la imagen

carlos