Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Image Thumbnails

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
GB
Hi, I'm putting the finishing touches on an image upload script. I have the image uploading successfully but now I wish to create a thumbnail from it. I have the following function setup:

Code:
function upload_thumbnail($file_name, $thumbnail_width, $thumbnail_height) {
	$image = imagecreatefromjpeg($this->path . $file_name);
echo $this->path . $file_name;
	list($width, $height) = getimagesize($image);

	if ($width < $height) {
		$thumbnail_width = ($thumbnail_height / $height) * $width;
	} else {
		$thumbnail_height = ($thumbnail_width / $width) * $height;
	}

	$thumbnail = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
	imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, width, height);
	imagejpeg($thumbnail, $this->path . 'thumbnails/' . $file_name, 100);

	return true;
}

The function is contained within a class where the path is set. The path and file name is the same I use to do the upload so I don't know it should not be working. Here's the error message I'm getting:

Warning: getimagesize(Resource id #18) [function.getimagesize]: failed to open stream: No such file or directory in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 119

Warning: Division by zero in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 124

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 127

Notice: Use of undefined constant width - assumed 'width' in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 128

Notice: Use of undefined constant height - assumed 'height' in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 128

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 128

Warning: imagejpeg(): supplied argument is not a valid Image resource in U:\My Webs\Site\Scripts\CMS\includes\upload.inc.php on line 129

I would appreciate it if someone could help. Thanks

btw I definately have the gd2 library turned on (I check with the phpinfo() function).
 
if you are inside a function you will need to declare the $this object as a global.

try adding this just after the function line
Code:
global $this;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top