Jump to content

Resizing and uploading an image


unistake

Recommended Posts

Hi all,

 

I have been trying to create a thumbnail but have realised I can just resize the image instead.

 

I have been trying to do this and have made the code below however do not know what else I need to do to upload the new image.

 

Could someone help?

 

Thanks

 

<?php
$filename = $_FILES['image'];
$reg = "G-ZZZZ";
$width = 200;
$height = 200;

list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} 
else {
   $height = $width/$ratio_orig;
}

$destination='aircraft/'.$reg."1.jpg";
$temp_file = $_FILES['image']['tmp_name'];
move_uploaded_file($temp_file,$destination);
?>

Link to comment
https://forums.phpfreaks.com/topic/217560-resizing-and-uploading-an-image/
Share on other sites

FYI - here is what I use for uploading, resizing, making thumb nails and adding drop shadows.

 

the form...

<table border="1" cellspacing="5" cellpadding="5">
<tr><td>
<form action="add01.php" method="post" enctype="multipart/form-data"> 
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
	Browse a File to Upload: <br><input type="file" name="imagename"><br>Enter a title for the picture<br>
		<input type="text" name="title" size="40" maxlength="80" value=""><br>Enter comments about the picture<br>
		<textarea name="comments" rows="5" cols="40"></textarea><br><br>
		<input type="submit" value="Submit" name="action"><br><br>note: <font color="#ff0080">max file size is: 2megs</font>
	</td>
</tr>
</table>

 

the processing page

<?PHP
/*		CHECK TO MAKE SURE FORM HAS BEEN SUBMITTED - IF NOT SEND BACK TO FORM*/
if($_SERVER['REQUEST_METHOD'] != "POST"){
// go back to form
}

/*		OPEN DATABASE CONNECTION */
include("db.php");

/*		GATHER AND SANITIZE FORM DATA */
$title = mysql_real_escape_string($_POST['title']); 
$comments =  mysql_real_escape_string($_POST['comments']); 

/*  EDIT THESE TO YOUR DESIRES */
$uploadDir = 'images/';								//	main picture folder
$max_height = 460;											//	largest height you allowed; 0 means any
$max_width = 380;												//	largest width you allowed; 0 means any
$max_file = 2000000;										//	set the max file size in bytes
$image_overwrite = 1;									// 0 means overwite; 1 means new name
$allowed_type01 = array(
"image/gif", 
"image/pjpeg", 
"image/jpeg", 
"image/png",  
"image/x-png",
"image/jpg");														// add or delete allowed image types
$do_thumb = 1;													// 1 make thumbnails; 0 means do NOT make 
$thumbDir = "thumbs/";						// thumbnail folder
$thumb_prefix = "";										// prefix for thumbnails
$thumb_width = 90;										// max thumb width
$thumb_height = 70;									// max thumb height
$do_shadow = 0;												// 0 to add drop shadow to main image. 1 do NOT
$flat_file = 0;																// 1 flat file for data; 0 database
$what_error = array();								//	error message array

/*		RESIZE FUNCTION */

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
$s_path = trim($s_path);
$o_path = trim($o_path);
$save = $s_path . $save;
$file = $o_path . $file;
$ext = strtolower(end(explode('.',$save)));
list($width, $height) = getimagesize($file) ; 
if(($width>$t_w) OR ($height>$t_h)) {
	$r1 = $t_w/$width;
	$r2 = $t_h/$height;
	if($r1<$r2) {
	  $size = $t_w/$width;
	}else{
	  $size = $t_h/$height;
	}
}else{
	$size=1;
}
$modwidth = $width * $size; 
$modheight = $height * $size; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
switch ($ext) {
	case 'jpg':
	case 'jpeg':
				$image = imagecreatefromjpeg($file) ; 
	break;
	case 'gif':
				$image = imagecreatefromgif($file) ; 
	break;
	case 'png':
				$image = imagecreatefrompng($file) ; 
	break;
}
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
imagejpeg($tn, $save, 100) ; 
return;
}
/* 		END OF RESIZE FUNCTION */

/*		DROP SHADOW FUNCTION */

function DropS($src, $temp_name) {
$ds_offest = 7;
$ds_steps = 10;
$ds_spread = 1;
$background = array("r" => 255, "g" => 255, "b" => 255);  
if(isset($src) AND file_exists($src)) {  
	list($o_width, $o_height) = getimagesize($src); 
	$width = $o_width + $ds_offest; 
	$height = $o_height + $ds_offest; 
	$image = imagecreatetruecolor($width, $height);
	$step_offset = array("r" => ($background["r"] / $ds_steps), "g" => ($background["g"] / $ds_steps), "b" => ($background["b"] / $ds_steps));
	$current_color = $background; 
	for ($i = 0; 	$i <= $ds_steps; $i++) { 
		$colors[$i] = imagecolorallocate($image, round($current_color["r"]), round($current_color["g"]), round($current_color["b"]));
		$current_color["r"] -= $step_offset["r"]; 
		$current_color["g"] -= $step_offset["g"];
		$current_color["b"] -= $step_offset["b"]; 
	}  
	imagefilledrectangle($image, 0,0, $width, $height, $colors[0]);
	for ($i = 0; $i < count($colors); $i++) { 
		imagefilledrectangle($image, $ds_offest, $ds_offest, $width, $height, $colors[$i]); 
		$width -= $ds_spread; 
		$height -= $ds_spread; 
	}  
	$original_image = imagecreatefromjpeg($src); 
	imagecopymerge($image, $original_image, 0,0, 0,0, $o_width, $o_height, 100);
	imagejpeg($image, $temp_name, 100);  
	imagedestroy($image); imagedestroy($original_image); 
} 
}
/*		END OF DROP SHADOW FUNCTION */


/*  GET BASIC INFO ABOUT UPLOADED FILE */
$original_name = str_replace ( " ", "_", $_FILES['imagename']['name'] ); 
$original_tmp = $_FILES['imagename']['tmp_name']; 
$original_size = $_FILES['imagename']['size']; 
$original_type = $_FILES['imagename']['type']; 
/*  do some basic error trapping and cleanup */
if($original_size>$max_file) {
//too large set error message and go back to form
}
if( $original_size<1) {
//no file was uploaded set error message and go back to form
}
if(!in_array($original_type, $allowed_type01)) {
// wrong file type set error message and return to form
}

/*  check to see if file already exists in the folder   if it exists AND rename option is , create new name */
$does_it = FALSE;
$original_name = strtolower($original_name);
if(file_exists($uploadDir . $original_name)) {
if($image_overwrite == 1) {
	//rename
	while(!$does_it) {
		$add_this_name = rand(1,200);
		$original_name = $add_this_name . $original_name;
		if(!file_exists($uploadDir . $original_name)) {
			$does_it = TRUE;
		}
	}
}
}

/*  ATTEMPT TO MOVE IMAGE TO PROPER LOCATION */
$uploadFile = $uploadDir . $original_name; 
if (move_uploaded_file($_FILES['imagename']['tmp_name'], $uploadFile) ) { 
// continue
} else { 
// send to error page
} 

/*   CHECK TO SEE IF IMAGE NEEDS RESIZING */
list($original_width, $original_height) = getimagesize($uploadDir . $original_name);
/* line 220 */
if($max_height<$original_height OR $max_width<$original_width) {
// dimensions are too large - resize
Resize_Image($original_name,$original_name,$max_width,$max_height,$uploadDir,$uploadDir);
}

/*  CHECK TO SEE IF THUMBNAILS IS TRUE - IF YES, MAKE AND STORE THUMB */
$thumb_name = $thumb_prefix . $original_name;

if($do_thumb == 1) {
// make a thumb
Resize_Image($thumb_name,$original_name,$thumb_width,$thumb_height,$thumbDir,$uploadDir);
}

/* CHECK TO SEE IF NEED TO ADD DROP SHADOW */
if($do_shadow <1) {
$src = $uploadDir . $original_name;
DropS($src, $src);
}

/* STORE DATA IN DATABASE */
$when = date("Y-m-d H:i:s");
$image = $original_name;
$title =  mysql_real_escape_string($title); 
$comments =  mysql_real_escape_string($comments); 
$sql1 = "INSERT INTO album (image,title,when2,comments) VALUES('$image', '$title', '$when', '$comments')";
$result2 = mysql_query($sql1);
$sid = mysql_insert_id();

/* RETURN TO FORM TO ADD ANOTHER IMAGE */
?>
<meta http-equiv="refresh" content="0;URL=add00.php"> 
<?PHP
exit();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.