Jump to content

Image upload and auto thumbnail help!


andrew_biggart

Recommended Posts

Im trying to write an image upload script which checks various elements of the image and then saves it and and a thumbnail into a folder.

 

At the moment I am getting a white screen which makes me think I have a syntax error so I have come here to find some fresh eyes to help me out. Can anyone see any reason why this wouldn't be working?

 

Form

<form enctype='multipart/form-data' action='upload_image.php' method='POST'>
<table class='uploads-form'>
	<tr>
		<td><input name='image' type='file' /><input type='hidden' name'MAX_FILE_SIZE' value='5000000' /></td>
	</tr>
	<tr>
		<td><input type='submit' name='upload_image' value='Upload' /></td>
	</tr>
</table>
</form>

 

php

<?php

	error_reporting(E_ALL);
        ini_set("display_errors", 1);

	$images_location = "project_files/";
	$thumbs_location = "project_thumbs/";
	$thumb_width = "100";
	$maximum_size = "5000000";

//Auto Thumbnail Function
function create_thumbnail($source,$destination, $thumb_width) {
	$size = getimagesize($source);
	$width = $size[0];
	$height = $size[1];
	$x = 0;
	$y = 0;
	if($width> $height) {
		$x = ceil(($width - $height) / 2 );
		$width = $height;
	} elseif($height> $width) {
		$y = ceil(($height - $width) / 2);
		$height = $width;
	}
	$new_image = imagecreatetruecolor($thumb_width,$thumb_width)
		or die('Cannot initialize new GD image stream');
	$extension = get_image_extension($source);
	if($extension=='jpg' || $extension=='jpeg')
		$image = imagecreatefromjpeg($source);
	if($extension=='gif')
		$image = imagecreatefromjpeg($source);
	if($extension=='png')
		$image = imagecreatefromjpeg($source);

	imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
	if($extension=='jpg' || $extension=='jpeg')
		imagejpeg($new_image,$destination);
	if($extension=='gif')
		$imagegif($new_image,$destination);
	if($extension=='png')
		imagepng($new_image,$destination);

	}

//Get Extension Function
function get_image_extension($name) {
	$name = strtolower($name);
	$i = strrpos($name,".");
	if (!$i) { return ""; }
		$l = strlen($name) - $i;
	$extension = substr($name,$i+1,$l);
	return $extension;
}

//Random Name Function
function random_name($length) {
	$characters = "abcdefghijklmnopqrstuvwxyz01234567890";
	$name = "";

	for ($i = 0; $i < $length; $i++) {
		$name .= $character[mt_rand(0, strlen($characters) - 1)];
	}
	return "image-".$name;
}



//Check And Save Image			
if(isset($_POST['upload_image'])) {

if($_FILES['image']['name'] == ""){
	echo = "Please select the image you would like to upload by clicking browse";
}
else{
	$size=filesize($_FILES['image']['tap_name']);
	$filename = stripslashes($_FILES['image']['name']);
	$extension = get_image_extension($filename);
	if($size > $maximum_size) {
		echo = "Your file size exceeds the maximum file size!";
	}
	else

	if (($extension != "jpg") &&
		($extension != "jpeg") &&
		($extension != "png") &&
		($extension != "gif")) {
		echo = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'.";
	}
	else {
		$image_random_name=random_name(15).".".$extension;
		$copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
		if (!$copy) {
			echo = "Error while uploadin your image! Please try again!";
		}
		else{
		create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width);
			echo = "Your image has been uploaded!";
		}
	}
}
}
?>

 

Link to comment
Share on other sites

And you should have the error_reporting/display_errors settings turned on in your master php.ini or in a local php.ini or in your .htaccess file (the exact method that works is dependent on how php is running in your web server) so that the fatal parse errors will be reported and displayed.

Link to comment
Share on other sites

And you should have the error_reporting/display_errors settings turned on in your master php.ini or in a local php.ini or in your .htaccess file (the exact method that works is dependent on how php is running in your web server) so that the fatal parse errors will be reported and displayed.

 

yes. if I had to "guess" if there are parse errors, i would immediately look into a different career. :-)

Link to comment
Share on other sites

Ok I have re written a new script but im having trouble with it as well. It will upload the image but once it gets to 100% and loads again I am getting the blank screen. I think the issue is when it tries to save the image.

 

My database has the following columns. image_id, file_url, thumb_url, project_name, project_text, project_type.

 

My folders are called project_files and project_thumbs.

 

This is the php :

<?php
$project=$_GET['project'];

// Check project is selected
if($project != '') {

//Check a form has been submitted				
if(isset($_POST['upload_image'])) { 

// Fetch the image array sent by preupload.php  
$photos_uploaded = $_FILES['image'];  
// Fetch the image caption array  
$photo_captions = $_POST['caption'];
// File type array
$photo_types = array(    
 'image/pjpeg' => 'jpg',   
 'image/jpeg' => 'jpg',   
 'image/gif' => 'gif',   
 'image/bmp' => 'bmp',   
 'image/x-png' => 'png'   
);
while($counter <= count($photos_uploaded)) {   
	if($photos_uploaded['size'][$counter] > 0) {   
		if(!array_key_exists($photos_uploaded['type'][$counter], $photo_types)) {   
	 	$result_final .= 'File ' . ($counter + 1) .   
	   	' is not a photo<br />';   
  		} else { 

		// Save the image details to db
		$sql="INSERT INTO project_images (file_url, project_name, project_text, project_type) VALUES ('$photos_uploaded', '$project', '$photo_captions', 'image')";
		$result=mysql_query($sql);

		$new_id = mysql_insert_id(image_id); // New Id generated

		// Get the filetype of the uploaded file   
		$filetype = $photos_uploaded['type'][$counter];    
		// Get the extension for the new name   
		$extension = $known_photo_types[$filetype];    
		// Generate a new name   
		$filename = "$new_id.$extension";    
		  
		// Update information
		$sql2="UPDATE project_images SET file_url = '$filename' WHERE image_id='$new_id'";
		$result2=mysql_query($sql2);   

		move_uploaded_file($photos_uploaded['tmp_name'][$counter],    
		$images_dir . '/project_files/' . $filename);

		//Auto thumbnail start
		$size = GetImageSize($images_dir . "/project_files/" . $filename);    
   
		// Wide Image    
			if($size[0] > $size[1])    
			{     
			$thumbnail_width = 100;     
			$thumbnail_height = (int)(100 * $size[1] / $size[0]);     
			}     
			   
			// Tall Image    
			else    
			{    
			 $thumbnail_width = (int)(100 * $size[0] / $size[1]);    
			 $thumbnail_height = 100;    
			}

			$thumbnail_width = <Preset Width>     
			$thumbnail_height = <Preset Width> * <height_dimension_of_original_image> / <width_dimension_of_original_image>

			$thumbnail_width = <Preset Height> * <width_dimension_of_original_image> /  <height_dimension_of_original_image>    
			$thumbnail_height = <Preset Height>

			$gd_function_suffix = array(      
			 'image/pjpeg' => 'JPEG',     
			 'image/jpeg' => 'JPEG',     
			 'image/gif' => 'GIF',     
			 'image/bmp' => 'WBMP',     
			 'image/x-png' => 'PNG'     
			);

			$function_suffix = $gd_function_suffix[$filetype];     
			$function_to_read = 'ImageCreateFrom' . $function_suffix;     
			$function_to_write = 'Image' . $function_suffix;     

			// Read the source file     
			$source_handle = $function_to_read($images_dir . '/project_files/' . $filename );      

			if ($source_handle) {     
			 // Let's create a blank image for the thumbnail     
			 $destination_handle = ImageCreate($thumbnail_width, $thumbnail_height);     

			 // Now we resize it     
			 ImageCopyResized($destination_handle, $source_handle,     
			   0, 0, 0, 0, $thumbnail_width, $thumbnail_height,     
			   $size[0], $size[1]);     
			}     

			// Let's save the thumbnail     

			$function_to_write($destination_handle, $images_dir . '/project_thumbs/' . $filename);     
			ImageDestroy($destination_handle);

			// Update information
			$sql3="UPDATE project_thumbs SET thumb_url = '$filename' WHERE image_id='$new_id'";
			$result3=mysql_query($sql3); 

			if($result3){
			echo"Your image has been uploaded successfully!";
			}

			else {
			echo "Your image has not been uploaded please try again!";
			}	

		}   
	}   
}				

}// End check form submit
}
else {
echo"Please select a project!";
}
?>

 

Link to comment
Share on other sites

Sorry my bad.

<?php

	error_reporting(E_ALL);
    ini_set("display_errors", 1);

	$images_location = "project_files/";
	$thumbs_location = "project_thumbs/";
	$thumb_width = "100";
	$maximum_size = "5000000";

//Auto Thumbnail Function
function create_thumbnail($source,$destination, $thumb_width) {
	$size = getimagesize($source);
	$width = $size[0];
	$height = $size[1];
	$x = 0;
	$y = 0;
	if($width> $height) {
		$x = ceil(($width - $height) / 2 );
		$width = $height;
	} elseif($height> $width) {
		$y = ceil(($height - $width) / 2);
		$height = $width;
	}
	$new_image = imagecreatetruecolor($thumb_width,$thumb_width)
		or die('Cannot initialize new GD image stream');
	$extension = get_image_extension($source);
	if($extension=='jpg' || $extension=='jpeg')
		$image = imagecreatefromjpeg($source);
	if($extension=='gif')
		$image = imagecreatefromjpeg($source);
	if($extension=='png')
		$image = imagecreatefromjpeg($source);

	imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
	if($extension=='jpg' || $extension=='jpeg')
		imagejpeg($new_image,$destination);
	if($extension=='gif')
		$imagegif($new_image,$destination);
	if($extension=='png')
		imagepng($new_image,$destination);

	}

//Get Extension Function
function get_image_extension($name) {
	$name = strtolower($name);
	$i = strrpos($name,".");
	if (!$i) { return ""; }
		$l = strlen($name) - $i;
	$extension = substr($name,$i+1,$l);
	return $extension; 
}

//Random Name Function
function random_name($length) {
	$characters = "abcdefghijklmnopqrstuvwxyz01234567890";
	$name = "";

	for ($i = 0; $i < $length; $i++) {
		$name .= $character[mt_rand(0, strlen($characters) - 1)];
	}
	return "image-".$name;
}



//Check And Save Image			
if(isset($_POST['upload_image'])) {

if($_FILES['image']['name'] == ""){
	echo = "Please select the image you would like to upload by clicking browse";
}
else{
	$size=filesize($_FILES['image']['tap_name']);
	$filename = stripslashes($_FILES['image']['name']);
	$extension = get_image_extension($filename);
	if($size > $maximum_size) {
		$result = "Your file size exceeds the maximum file size!";
	}
	else

	if (($extension != "jpg") &&
		($extension != "jpeg") &&
		($extension != "png") &&
		($extension != "gif")) {
		$result = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'.";
	}
	else {
		$image_random_name=random_name(15).".".$extension;
		$copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
		if (!$copy) {
			$result = "Error while uploadin your image! Please try again!";
		}
		else{
		create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width);
			$result = "Your image has been uploaded!";
		}
	}
}
}
?>

 

I am also trying out another tutorial as well which isnt going to well so if this looks redundant let me know and I will post the latest script. Thanks for your help.

 

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.