Jump to content

GD Image resizing and saving thumb


punstc

Recommended Posts

I'm really new at php and trying to figure out some of the image resizing things. but i'm having problems.  I'm working with some reference that my lab instructor gave me but trying to combine it with my own code I'm running into problems. It keeps giving me this error

 

Warning: move_uploaded_file(../gallery_images/large/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/gallery2.php on line 21

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpl9AFmE' to '../gallery_images/large/' in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/gallery2.php on line 21

 

I could really use some help on this heres my code

 

<?php
$this_file = basename($_SERVER['PHP_SELF']);

if(empty($_POST)){
$status = ' ';	
}
else {
	$desc = $_POST['desc'];
	$destination = '../gallery_images/large/';
	$large = $_FILES['large']['name'];
	$large_tmp = $_FILES['large']['tmp_name'];
	$error_list = array();

	if(empty($large)){
		$error_list[] = 'Image';
	}
	if(empty($desc) OR ($desc == 'Description')){
		$error_list[] = 'Description';
	}
	else {
			if(move_uploaded_file($large_tmp, $destination)){


				$image_path = "../gallery_images/large/$large"; // Gets temorary image name from the server
				$dest_path = "../gallery_images/thumb/"; // Sets path to thumbnail images


				$max_width = 200;
				$max_height = 150;

				$size = GetImageSize($image_path);
				$width = $size[0];
				$height = $size[1];
				$x_ratio = $max_width / $width;
				$y_ratio = $max_height / $height;

				if(($width <= $max_width) && ($height <= $max_height)){
				  $new_width = $width;
				  $new_height = $height;
				}
				elseif(($x_ratio * $height) < $max_height){
				  $new_height = ceil($x_ratio * $height);
				  $new_width = $max_width;
				}
				else{
				  $new_width = ceil($y_ratio * $width);
				  $new_height = $max_height;
				}

				$src = imagecreatefromjpeg($image_path);
				$dst = imagecreatetruecolor($new_width,$new_height);
				imagecopyresized($dst, $src, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
				imagejpeg($dst,"$dest_path"."$large", 90);
				$thumb = "$dest_path"."$large";
				imagedestroy($src);
				imagedestroy($dst);

				$status = "<p>The image \". $image. \" has been uploaded!</p>";


				$sql = "INSERT INTO jchamb_gallery SET large='$large', thumb='$thumb' desc='$desc'";
				$result = mysql_query($sql);

			}

			else{
				$status = "<p>There was an error uploading the image, please try again.</p>";
			}
		}
	}
if(count($error_list) > 0) {
	$status = "<p>You did not provide a(n):</p>\n";
	foreach($error_list as $error){
	    $status .= "\t<p><strong>$error</strong></p>\n";
	}
}	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<div id="wrapper">
  <?php echo $status; ?>
      <form enctype="multipart/form-data" action="<?php echo $this_file; ?>" method="post">
          <ul>
              <li><input name="desc" type="text" onfocus="if (value=='Description') value='';" onblur="if(value=='') value='Description';" value="Description" size="55" /></li>
              <li><input name="large" type="file" size="55"  /></li>
          </ul>
          <p><input type="submit" value="Upload Image" /></p>
      </form>
    
    </div>
</body>
</html>

 

thanks for taking a look

 

Jake

Link to comment
Share on other sites

I'm starting to go crazy i've been working on this forever .  now i'm getting this error and I don't understand why can anyone help?

 

Fatal error: Call to undefined function imagecreatefromjpeg() in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/image_add.php on line 58

 

<?php

if(empty($_POST)) {
$status = 'To add a new image, fill out the form below. When you are finished, click the Add Image button once.';
}
else {
	$description = $_POST['desc'];
	$large = basename($_FILES['large']['name']);
	$large_tmp = $_FILES['large']['tmp_name'];
	$destination = '../gallery_images/large';
	$error_list = array();

	if(empty($description)) {
		$error_list[] = 'Description';
		}

	if(empty($large)) {
		$error_list[] = 'Image';
		}

	if(empty($error_list)) {

		mysql_connect('localhost', 'jchamberlain', 'satchel');
		mysql_select_db('jchamberlain_0806');

	$sql = "INSERT INTO jchamb_gallery SET description='$description' ";

	if(!empty($large)) {
	if(move_uploaded_file($large_tmp,"$destination/$large")) {

			$sql .= ", large='$large' ";
			$image_path = "$destination/$large"; // Gets temorary image name from the server
			$dest_path = "../gallery_images/thumb"; // Sets path to thumbnail images

				// Sets maximum width and height of thumbnail
				$max_width = 200;
				$max_height = 150;

				$size = GetImageSize($image_path);
				$width = $size[0];
				$height = $size[1];
				$x_ratio = $max_width / $width;
				$y_ratio = $max_height / $height;

				if(($width <= $max_width) && ($height <= $max_height)){
				  $new_width = $width;
				  $new_height = $height;
				}
				elseif(($x_ratio * $height) < $max_height){
				  $new_height = ceil($x_ratio * $height);
				  $new_width = $max_width;
				}
				else{
				  $new_width = ceil($y_ratio * $width);
				  $new_height = $max_height;
				}

				$src = imagecreatefromjpeg($image_path);
				$dst = imagecreatetruecolor($new_width,$new_height);
				imagecopyresized($dst, $src, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
				imagejpeg($dst,"$dest_path"."$large", 90);
				$thumb = "$dest_path"."$large";
				imagedestroy($src);
				imagedestroy($dst);
				//////////////////////////// END IMAGE SCALE ////////////////////////////////
				$status = "<p>The image \". $image. \" has been uploaded!</p>";
				$sql .= ", thumb='$thumb' ";
		}
}


		if(mysql_query($sql)) {
			header('Location: index.php');
		}
		else {
			$status ='Unable to add your images.';
		}
	}
	else {
		$status = 'Unable to upload your images.';

	$status = '<ul>';

	foreach($error_list as $error_message) {
		$status .= "<li>$error_message</li>";
	}
	$status .= '</ul>';
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Image</title>
</head>
<body>
<h2>Add Image</h2>
 <?php echo $status; ?>
      <form enctype="multipart/form-data" action="<?php echo $this_file; ?>" method="post">
          <ul>
              <li><input name="desc" type="text" onfocus="if (value=='Description') value='';" onblur="if(value=='') value='Description';" value="Description" size="55" /></li>
              <li><input name="large" type="file" size="55"  /></li>
          </ul>
          <p><input type="submit" value="Upload Image" /></p>
      </form>

</body>
</html>

 

I really appreciate the help, I need this to work for a school project thats due tomorrow

 

Jake

Link to comment
Share on other sites

You must not have GD support or you don't have JPEG support on your server.  Do me a favor please.  Create a new file and put this in it, then show me the output:

 

<?php

if (imagetypes() & IMG_JPEG) {

echo "JPEG support enabled.";

}

else {

echo "JPEG support not enabled.";

}

?>

 

Just like that.

Link to comment
Share on other sites

it output this

 

Fatal error: Call to undefined function imagetypes() in /home/2008/03/jchamberlain/public_html/wpr/final_project/admin/test.php on line 2

 

I'm testing all this on my schools server which we have to upload our projects to. It should have the GD library installed to because i'm basing the code off a script my lab instructor gave me

 

thanks for helping I really appreciate this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.