Wonky issue with thumbnails...

Hello.

I’m ready to bang my head pretty severely; I have what feels like a very simple problem. I’m taking typical image uploads, resizing them to fit my site, then making a second copy for use as a thumbnail. I am not worried about set sized thumbnails that need math (say, square thumbnails) - I am simply trying to set a max dimention of 150 pixels (be it width or height).

The orginal image resize works perfectly. The thumbnail does give me an image of the correct size, but instead of getting the full image resized down, I get a cropped portion of the image. This baffles me, as I am simply copying the working code and changing the maximum size it can generate.

First off, I reference the function getThumbSize, which returns an array of the target width and height. I have verified that the output makes sense (a 450x300 image returns a thumbnail spec of 150x100, which is correct).

[php]
function getThumbSize($width, $height, $target) {
$newsize=array();
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

if(($width > $target) || ($height > $target)){
$newsize[0] = round($width * $percentage); //width
$newsize[1] = round($height * $percentage); //height
}else{
$newsize[0] = $width; //width
$newsize[1] = $height; //height
}

return $newsize;
} //end function
[/php]

My image upload function is where things get wonky.

[php]
function imageupload($filearray, $maxsize=0){

$randfilename=sha1(microtime().rand(0,99999));

global $uploadDir;

$fileid=0;

//crap having to do with file extensions, feel free to ignore
if ( (is_uploaded_file($filearray[‘tmp_name’])) ) {
$src=$filearray[‘name’];
if(eregi(’.jpeg’,$src)||eregi(’.jpg’,$src)) {
$randfilename.=".jpg";
}
elseif(eregi(’.png’,$src)) {
$randfilename.=".png";
}

//nw, we get to the issues
$file_name = $uploadDir . $randfilename;
$file_name = stripslashes($file_name);
$file_name = str_replace("’","",$file_name);
copy($filearray[‘tmp_name’],$file_name);

if(eregi('.jpeg',$src)||eregi('.jpg',$src)) {
	$src=imagecreatefromjpeg($filearray['tmp_name']);
}
elseif(eregi('.png',$src)) {   
$src = imagecreatefrompng($filearray['tmp_name']);   
}  

//this works great, gives me a new jpeg with no dimension larger than 800 pixels
$src2=$file_name;
list($width,$height)=getimagesize($src2);
$thumbsize=getThumbSize($width, $height, 800);
$newwidth=$thumbsize[0];
$newheight=$thumbsize[1];
$awidth=$width;
$aheight=$height;
$new_name = $uploadDir . $randfilename;
$tmp= imagecreatetruecolor($newwidth,$newheight)
or croak(“Cannot Initialize new GD image stream”);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$new_name,100);
imagedestroy($tmp);

//now, we do it again. This gives me a thumbnail of the proper size, but the viewable image area gets cut off.
$src2=$file_name;
list($width,$height)=getimagesize($src2);
$thumbsize=getThumbSize($width, $height, 150);
$newwidth=$thumbsize[0];
$newheight=$thumbsize[1];
$awidth=$width;
$aheight=$height;
$new_name = $uploadDir . ‘thmb_’ . $randfilename;
$tmp= imagecreatetruecolor($newwidth,$newheight)
or croak(“Cannot Initialize new GD image stream”);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$new_name,100);
imagedestroy($tmp);

}

}//end function
[/php]

Any help would be tremendously appreciated!

Hi ,

This may help you. Iam using a really cool image resize script on a few sites of mine ,you simply set what height and width you won’t when the image is showen , really easy to set up also , it doeant distort and shows a full image but resized

Heres a http://shiftingpixel.com/2008/03/03/smart-image-resizer/

Hope this helps

Thanks, but honestly, I want to figure out what I am doing wrong, here. I’m not having any distortion issues, and the script runs quickly.

I’d like to make it work correctly.

I would debug the values first.
Before:
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

echo $newwidth."
";
echo $newheight."
";
echo $width."
";
echo $height."
"; // or die($height);

That would be the starting point to verify you are using the correct values.
(And, what are $awidth and $aheight for? Don’t see them being used!)

Good point as to $awidth and $aheight.

I have done an echo, values look correct.

Well, I looked at your code again… Can’t find anything except the first two lines do not need to be repeated.
Nothing has changed for those two lines, $filename, list($width, $height) both do not change, delete those lines.

Also, get rid of “croak”, it should be die() Croak is not PHP as the rest of the code, it is Pearl/APD and makes a stack call which could cause issues. Unless you actually understand that. It should be “die(“message”);”

Now, the solution is that you are attempting to resize BOTH width and height. In the “getThumbSize” function, you are resizing only one and not the other! Here is a fix for the back code… Replace the second resize with this code:
[php]
//now, we do it again. This gives me a thumbnail of the proper size, but the viewable image area gets cut off.
$newwidth = 150 / $width;
$newheight = 150 / $height;
$new_name = $uploadDir . ‘thmb_’ . $randfilename;
$tmp= imagecreatetruecolor($newwidth,$newheight)
or die(“Cannot Initialize new GD image stream”);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$new_name,100);
imagedestroy($tmp);
}
[/php]
I bet that will work for you! Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service