Page 1 of 1

Rename properly?

Posted: Sun Aug 21, 2005 3:14 pm
by ianhull
Hi Guys I am trying to use this script but I am having a few little issues.

it works great and uploads and resizes all images but I would like to know the filename of the uploaded and resized image so that I can insert it into my database.

Anyone know howI can achieve this?

Thanks in advance.

Code: Select all

<?php

// script to upload multiple images from single form
// can also be used to upload one image at a time
// script will resize uploaded image and create thumbnail,
// rename thumbnail with "tn_" prefix and place in
// root/photos/ directory
// script deletes image from checkbox on form page when selected-
// and submitted


// delete checkbox is/are checked. Delete the picture(s)
if(isset($delete_pic)) 

	{
		while(list($key, $value) = each($delete_pic))

		{

			// key is the name of the Database-field
			$df = $key."(".$_position.")";
			
			
			// generate the link to the picture
			$del_p = "uploads".$HTTP_POST_VARS[$df];
			
			// delete the picture
			@unlink($del_p);
			
			


			// After the file is deleted, insert placeholder img
			// use a jpg to avoid errors from the accepted types var
			$HTTP_POST_VARS[$df] = "placeholder.jpg";
			
		}
}

else



// upload picture

// Do while one or more upload-files are present
while(list($key) = each($HTTP_POST_FILES))




if($_POST['upload'] == 'now')

	

	{

		$report_img = '';
		


		
		$img_width = $_POST['strWidth'];
		$img_height = $_POST['strHeight'];
		$img_root = "uploads";

		$fileName = explode(".", $HTTP_POST_FILES[$key]['name']);
		$name = str_replace(' ', '_', $fileName[0]);
		


		error_reporting(53); //errors may happen for no reason, so this kills them, we will use our own error reporting
		
		$accepted_types = array('image/jpeg', 'image/jpg', 'image/pjpeg');

		
		
		if(!in_array($HTTP_POST_FILES[$key]['type'], $accepted_types) || trim($HTTP_POST_FILES[$key]['tmp_name']) == "" || trim($HTTP_POST_FILES[$key]['tmp_name']) =="none")

		

		{
			echo '<center><br><br><h1><font face="Trebuchet MS, Verdana, sans-serif" color="red">
			ERROR!</font></h1><br><font face="Trebuchet MS, Verdana, sans-serif">You did not supply a valid file.
			ALL photos must be JPEGs!<br>Please use your <b>back</b> button and try again.</font></center>';


			
	}
	else
	{

		$img_orig_size = getimagesize($HTTP_POST_FILES[$key]['tmp_name']);
		$img_orig_width = $img_orig_size[0];
		$img_orig_height = $img_orig_size[1];
		$img_original = ImageCreateFromJpeg($HTTP_POST_FILES[$key]['tmp_name']);
		$image_stored = time() . '_' . $_POST[$key]  . "$name.jpg";
		ImageJPEG($img_original, "$img_root/$image_stored");
		
		

		if($img_orig_width <= $img_width || $img_orig_height <= $img_height)
	{



			echo "<b>The image that you selected did not need to be resized</b>.<br>You may want to upload a bigger picture.";


	}

	else
	{

		//the image will need to be resized, as its too big. It will be a thumbnail

		
		$mlt_w = $img_width / $img_orig_width;
		$mlt_h = $img_height / $img_orig_height;

		$mlt = $mlt_w < $mlt_h ? $mlt_w:$mlt_h;
		
		//  Calculate new dimensions
		$img_new_width =  round($img_orig_width * $mlt);
		$img_new_height =  round($img_orig_height * $mlt);
		$img_resized = ImageCreate($img_new_width, $img_new_height);
		
		imagecopyresized($img_resized, ImageCreateFromJpeg($HTTP_POST_FILES[$key]['tmp_name']), 0 , 0 , 0 , 0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);																																																									
		
		$img_name = "tn_$image_stored";
		Imagejpeg($img_resized, "$img_root/$img_name");
		ImageDestroy($img_resized);
		$mg_new_size = filesize("$img_root/$img_name");

		}

	}


{


if(isset($HTTP_POST_FILES[$key]['tmp_name']) && $HTTP_POST_FILES[$key]['tmp_name'] != "none")


{


		// if there's an old picture, delete it
		$old_key = $key."(".$_position.")";



		if(isset($HTTP_POST_VARS[$old_key]) && $HTTP_POST_VARS[$old_key] != "")

		{

		$delete_file = "../../".$HTTP_POST_VARS[$old_key];
		
		@unlink($delete_file);
		}

		
		$tf = $key."(".$_position.")";


		// the name and path to the uploaded file
		$HTTP_POST_VARS[$tf] = $upload_path.$image_stored.$HTTP_POST_FILES['name'];

		

		// delete the temp picture
		@unlink($HTTP_POST_FILES[$key]['tmp_name']);


		}
	}
}

?>

Posted: Sun Aug 21, 2005 3:22 pm
by feyd
the uploaded filename is:

Code: Select all

$image_stored = time() . '_' . $_POST[$key]  . "$name.jpg";
the resized name is: 'tn_' prepended to that name.


I'm not sure what $_POST[$key] is, but $name is the original filename with all spaces replaced with underscores.