Click to See Complete Forum and Search --> : [RESOLVED] Image Resize


The Little Guy
05-10-2006, 02:27 PM
Here is a resizing function That I found, and It isn't working correctly. Basically what it is doing is cropping the image, and making the bottom longer.

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight * $ratio;

$thumbImg = imagecreate($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($srcImg), imagesy($srcImg));

imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}

createThumbnail("images/photos", "$name", "images/photos/thumbs", 100);

NogDog
05-10-2006, 02:55 PM
Should the last two args for the imagecopyresized be imagesx($srcImg), imagesy($srcImg)?

The Little Guy
05-10-2006, 03:05 PM
That ended croping, now it is just streching the file vertically. The original height is 599px, and it streches it to 5391px. What could be causing this?

The Little Guy
05-10-2006, 03:11 PM
I got it!
The problem was with the $ratio = ... it was reversed.

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$ratio = $thumbWidth / $origWidth;
$thumbHeight = $origHeight * $ratio;

$thumbImg = imagecreate($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($srcImg), imagesy($srcImg));

imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}

createThumbnail("images/photos", "$name", "images/photos/thumbs", 100);