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!