Imagecopyresampled() not working, "supplied argument is not a valid Image resource"

Okay, so I am trying to write a function for making thumbnails, here’s the function (you can ignore all the string functions, that all works properly):

function makeThumbnail($originalImage, $username)
{
	$sidemax = 250;
	
	$removestr = $_SERVER['DOCUMENT_ROOT'] . '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;
	}
	
	$thumbie = imagecreatetruecolor($nw, $nh);
	if (!imagejpeg($thumbie, $thumbnailName))
	{
		$error = 'Error creating jpeg.';
		include $_SERVER['DOCUMENT_ROOT'] . 'error.html.php';
		exit();
	}
	$thumbnail[0] = imagecopyresampled($thumbnailName, $originalImage, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
	$thumbnail[1] = $thumbnailName;
	
	return $thumbnail;

}

The $originalImage is the url to an image that was uploaded and moved to it’s appropriate directory. The first two image functions I used work how they’re suppose to (I checked the folder and found the black rectangle jpeg).

The error message I’m getting is this:

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in […]\filewriting.inc.php on line 63

Line 63 is the one with the imagecopyresampled function on it.

imagecopyresampled($thumbnailName, $originalImage, 0, 0, 0, 0, $nw, $nh, $ow, $oh);

What are the values of $thumbnailName and $originalImage when you get to the imagecopyresampled() statement?

Can you var_dump them and confirm that they are correct?

I see

Yes, it’s seems you’re passing a path for $originalImage and $thumbnailName, while you shoud really load it (using one of the imagecreatefrom* functions).

For example, if it’s a jpg you’re working with, you need to load using [fphp]imagecreatefromjpeg[/fphp].

$originalImage is the url to the original image file that I’m trying to make a thumbnail of. $thumbnailName is kinda defined in the function… I just took the file name from $originalImage and added “thm-” to the beginning of the file name.

And Immerse - thank you, I will try that.

Okay, so I tried the suggestions, and I now have this (note the switch statement):

function makeThumbnail($originalImage, $username)
{
	$sidemax = 250;
	
	$removestr = $_SERVER['DOCUMENT_ROOT'] . '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;
	}
	
	$thumbie = imagecreatetruecolor($nw, $nh);
	if (!imagejpeg($thumbie, $thumbnailName))
	{
		$error = 'Error creating jpeg.';
		include $_SERVER['DOCUMENT_ROOT'] . '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;
	}
	
	$thumbie = imagecreatefromjpeg($thumbnailName);
	$thumbnail[0] = imagecopyresampled($thumbie, $original, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
	$thumbnail[1] = $thumbnailName;
	
	return $thumbnail;

}

But I get this error:

Fatal error: Call to undefined function exif_imagetype()

There was a suggestion on how to fix it, here: http://us3.php.net/manual/en/function.exif-imagetype.php
under the notes, but I tried that and it did not work x_x

Can you find on the phpinfo() page, exif?

No… no I cannot :mad: Aaaaaaaargggggggg… I have to reinstall it all, don’t I?

It was in the php.ini file tho! Doesn’t that count for something?

Or you could use the mimetype returned by [fphp]getimagetype[/fphp] instead:


$dimensions = getimagesize($originalImage);
$ow = $dimensions[0];
$oh = $dimensions[1];
$mime = $dimensions['mime'];

Yeah… I was thinking about doing that, but I’m a little confused about what it returns, exactly… It says

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

So does that mean it gives the int or does it give it in the imagetype_xxx form? x.X

[fphp]getimagesize[/fphp]'s second index returns an int which you can compare against the IMAGETYPE_XXX constants.

Oooooh, okay. Thank you sooooo much! :smiley:

Once I get my computer working properly again, I will let y’all know how it goes, but I imagine that should do the trick.

Okay, well, it took me to the next page without displaying any errors, but it’s still not working… All it gives me for the thumbnail is a black box… :confused:

function makeThumbnail($originalImage, $username)
{
	$sidemax = 250;
	
	$removestr = $_SERVER['DOCUMENT_ROOT'] . '/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;
	}
	
	$thumbie = imagecreatetruecolor($nw, $nh);
	if (!imagejpeg($thumbie, $thumbnailName))
	{
		$error = 'Error creating jpeg.';
		include $_SERVER['DOCUMENT_ROOT'] . '/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;
	}
	
	$thumbie = imagecreatefromjpeg($thumbnailName);
	$thumbnail[0] = imagecopyresampled($thumbie, $original, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
	$thumbnail[1] = $thumbnailName;
	
	return $thumbnail;

}

I uploaded it onto my host’s server so I could figure out how to fix it while still using that function >.< Anyways, like I said, not giving any errors, but it’s still not working. I’m not sure that it’s even updating the table in the database either because it says the ID number is 0…