making smaller image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webweaver6
Forum Newbie
Posts: 2
Joined: Thu Nov 17, 2005 3:30 pm

making smaller image

Post by webweaver6 »

Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am trying to upload an image, and make a smaller version. This keeps dying at the last step. Can anyone tell me what is missing in this code?

Code: Select all

$name2 = imagecreate(150, $height); //create a blank image
    	imagecopyresampled($name2, $image, 0, 0, 0, 0, 150, $height, $mime[0], $mime[1]) or die('work'); //resize and resample the image
    	mysql_query("UPDATE items SET itmImage = '$itmFile' WHERE itmID = '$itmID'") or die(mysql_error()); //put the filename in the database
    	switch ($mimetype) //choose an action for the correct mime type
	    {
	      case 'image/png':
	        imagepng($name2, $name) or die('last step 1'); //make a new image with the modified one
	        break;
	      case 'image/gif':
	        imagegif($name2, $name) or die('last step 2');
	        break;
	      case 'image/jpeg':
	        imagejpeg($name2, $name) or die('last step 3');
	        break;
	      case 'image/wbmp':
	        imagewbmp($name2, $name) or die('last step 4');
	        break;
	    }
    	unset($_GET['itmID']); //destroy the itmID variable
    	$error = 'The file has been added.'; //tell the user it worked.
    }
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
webweaver6
Forum Newbie
Posts: 2
Joined: Thu Nov 17, 2005 3:30 pm

Post by webweaver6 »

ok - here is the clearer post - an image has been uploaded and we need to rename it and resize it- right now it is crashing at the last step. Can anyone help? TIA

Code: Select all

$product = $_GET['type'] == 1 ? 'products/' : ''; //If working on the main image, place it in the products directory
		$mime = getimagesize($_FILES['itmFile']['tmp_name']); //get information about the image
		$mimetype = $mime['mime']; //store mime type in a seperate variable
		if (strlen($_FILES['itmFile']['name']) > 45) //check filename length
		{
			$error = 'The image name must be less than 46 characters.'; //send an error
			$_GET['mode'] = 'file'; //return to the file uploader
		}
		else if ($mimetype != 'image/png' && $mimetype != 'image/jpeg' && $mimetype != 'image/gif' && $mimetype != 'image/wbmp') //if it's not a usable format
		{
			$error = 'The image must be a PNG, JPEG, GIF or BMP file.'; //send an error
			$_GET['mode'] = 'file'; //return to the file uploader
		}
  	$itmFile = $_FILES['itmFile']['name']; //give the filename its own variable
	  $itmID = intval($_GET['itmID']); //remove extra chacters from itmID
		move_uploaded_file($_FILES['itmFile']['tmp_name'], 'images/'.$product.$_FILES['itmFile']['name']); //move the file to a permanent location
	  if ($_GET['type'] == 1) //If working with the main image
	  {
  	  $modify = $mime[0] / 150; //find out how to find the height
	    $height = $mime[1] / $modify; //calculate the height
	    $name = str_replace('.', 'thumb.', $file); //change to the thumbnail filename.
	    $path = 'images/'.$product.$itmFile; //put together the path
	    switch ($mimetype) //choose an action for the correct mime type
	    {
	      case 'image/png':
	        $image = imagecreatefrompng($path) or die('I hate you 1'); //take the image, and make it an image resource that PHP can use
	        break;
	      case 'image/gif':
	        $image = imagecreatefromgif($path) or die('I hate you 2');
	        break;
	      case 'image/jpeg':
	        $image = imagecreatefromjpeg($path) or die('I hate you 3');
	        break;
	      case 'image/wbmp':
	        $image = imagecreatefromwbmp($path) or die('I hate you 4');
	        break;
	    }
    	$name2 = imagecreate(150, $height); //create a blank image
    	imagecopyresampled($name2, $image, 0, 0, 0, 0, 150, $height, $mime[0], $mime[1]) or die('work'); //resize and resample the image
    	mysql_query("UPDATE items SET itmImage = '$itmFile' WHERE itmID = '$itmID'") or die(mysql_error()); //put the filename in the database
    	switch ($mimetype) //choose an action for the correct mime type
	    {
	      case 'image/png':
	        imagepng($name2, $name) or die('last step 1'); //make a new image with the modified one
	        break;
	      case 'image/gif':
	        imagegif($name2, $name) or die('last step 2');
	        break;
	      case 'image/jpeg':
	        imagejpeg($name2, $name) or die('last step 3');
	        break;
	      case 'image/wbmp':
	        imagewbmp($name2, $name) or die('last step 4');
	        break;
	    }
    	unset($_GET['itmID']); //destroy the itmID variable
    	$error = 'The file has been added.'; //tell the user it worked.
    }
Post Reply