Image Manipulation with GD

Okay, I’ve been trying to fix this for months (off and on). Here’s the function:

function makeThumbnail($originalImage, $username) // $originalImage is the url
{
	$sidemax = 250;
	
	$removestr = $_SERVER['DOCUMENT_ROOT'] . '/funstuff/funnerstuff/users/' . $username . '/images/';
	$removelen = strlen($removestr);
	$originalImageName = substr_replace($originalImage, '', 0, $removelen);
	$thumbnailName = 'thm-' . $originalImageName;
	$thumbnailName = $removestr . $thumbnailName;
	
	$dimensions = getimagesize($originalImage);
	$ow = $dimensions[0];
	$oh = $dimensions[1];
	$largerval = max($ow, $oh);
	$x = round(($oh * $sidemax) / $ow);
	if ($largerval == $ow)
	{
		$nw = $x;
		$nh = $sidemax;
	} else {
		$nw = $sidemax;
		$nh = $x;
	}
	
	$imageInfo = getimagesize($originalImage);
	$memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
	if (function_exists('memory_get_usage') && memory_get_usage() + $memoryNeeded > (integer) ini_get('memory_limit') * pow(1024, 2))
	{
		ini_set('memory_limit', (integer) ini_get('memory_limit') + 
		ceil(((memory_get_usage() + $memoryNeeded) - (integer) ini_get('memory_limit') * pow(1024, 2)) / pow(1024, 2)) . 'M');
	}
	
	$thumbie = imagecreatetruecolor($nw, $nh);
	if (!imagejpeg($thumbie, $thumbnailName))
	{
		$error = 'Error creating jpeg.';
		include $_SERVER['DOCUMENT_ROOT'] . '/funstuff/funnerstuff/error.html.php';
		exit();
	}
	
	$mimetype = exif_imagetype($originalImage);
	switch ($mimetype) {
		case 1:
			$original = imagecreatefromgif($originalImage);
			break;
		case 2:
			$original = imagecreatefromjpeg($originalImage);
			break;
		case 3:
			$original = imagecreatefrompng($originalImage);
			break;
		case 6:
			$original = imagecreatefromwbmp($originalImage);
			break;
		//default: I dunno, false?
	}
	
	$thumbie = imagecreatefromjpeg($thumbnailName);
	$thumbnail[0] = imagecopyresized($thumbie, $original, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
	$thumbnail[1] = $thumbnailName;
	
	imagedestroy($thumbie);
	imagedestroy($original);
	
	return $thumbnail;
}

All I’m trying to do is MAKE A THUMBNAIL! :sick: The image uploads just fine, but it won’t turn the image into a thumbnail. All I get is a black box of the correct measurements. Any help at all would be really really appreciated. (:

That’s pretty overkill for something as simple as making a thumbnail.
I’m assuming you’re using this with a form to upload images and create a smaller version of them… try something like this:


function MakeCopy( $Source, $Final, $Clean=false )
{
	if( empty( $Source ) || empty( $Final ) ){ return false; } 
	if( false === @copy( $Source, $Final ) ){ return false; } 
	if( $Clean ){ @unlink( $Source ); } 
	return true; 
}
function MakeThumb( $Source, $Final, $Width=200, $Quality=80, $Clean=false )
{
	if( empty( $Source ) || empty( $Final ) ){ return false; } 
		
	$IMG = false; 
	switch( @exif_imagetype( $Source ) ) 
	{
		case 1: $IMG = imagecreatefromgif( $Source );  break;
		case 2: $IMG = imagecreatefromjpeg( $Source ); break;
		case 3: $IMG = imagecreatefrompng( $Source );  break;
		case 6: $IMG = imagecreatefromwbmp( $Source ); break; 
		default: return false; // image type not supported 
	}
	
	$ow     = imagesx( $IMG ); // original width 
	$oh     = imagesy( $IMG ); // original height  
	$Height = floor( $oh * ( $Width / $ow ) ); // calculate height based on width 
	$Canvas = imagecreatetruecolor( $Width, $Height ); // thumbnail placeholder 
	$Copy   = imagecopyresized( $Canvas, $IMG, 0, 0, 0, 0, $Width, $Height, $ow, $oh ); // copy 
	$Create = imagejpeg( $Canvas, $Final, $Quality ); // create final image 
	
	if( !$Create ){ return false; } // failed on create 
	if( $Clean ){ @unlink( $Source ); } // delete original source 
	imagedestroy( $IMG ); // delete "imagecreate" source 
	return true; 
}

$Upload = MakeCopy( $_FILES['img']['tmp_name'], '/images/full.jpg' ); 
$Thumb  = MakeThumb( $_FILES['img']['tmp_name'], '/images/thumb.jpg', 240, 92, true ); 

Two small functions, one to copy a file from source to a new path, the other to create thumbnail version of a source and save to a new path…

I didn’t test it but in theory it should work. There’s also 2 usage examples at the bottom there.

While I appreciate the suggestion… I don’t really know enough about PHP to really tell what difference it makes… sorry :sick:

However, I did read over your code and THANK YOU SOOOOO MUCH!!! I think, honestly, I just had some stuff out of order. But it’s working now. Thank you!
:slight_smile: :cool: (: