Uploaded image, resize and save function issues

Hi all,

I’m trying to create a function that neatly takes an image upload from my form and resizes as needed and then saves to a new permanent location. I referenced another thread in here by nbewley(imagecopyresized() question)

I can’t seem to get the path correctly defined as imagejpeg($dst, $upload_filename) , shows error “No such file or directory”. I can’t figure out what I’m doing wrong. Unless my use of the image functions here are the real issue. I haven’t done this before.

Here’s my function:


function img_process($imagedetails, $src_image, $upload_dir){
		
 	//$imagedetails - result of getimagesize($src_image) 	- this function called if image is jpg
	//$src_image = $_FILES['userfile']['tmp_name'];		- from form upload
	//$upload_dir	 var_dump shows "/home/tacres01/public_www/w1_uploads/"
	
	
	$width = $imagedetails[0];
	$height = $imagedetails[1];
	
	//This will be used to determine if the file needs to be made smaller
	$max_dim = 600;
	
	if ($width > $max_dim || $height > $max_dim){
			
		//New dimensions are required and determined here
		$ratio = $width/$height;
		if ($ratio > 1){
			$dst_width = $max_dim;
			$dst_height = $max_dim/$ratio;
		} else {
			$dst_height = $max_dim;
			$dst_width = $max_dim*$ratio;
		}
	} else {
		$dst_width = $width;
		$dst_height = $height;
	}
	
	
	//load old image and map to new
	$src = imagecreatefromjpeg($src_image);
	$dst = imagecreatetruecolor($dst_width, $dst_height);
	imagecopyresized($dst,$src,0,0,0,0,$dst_width,$dst_height,$width,$height);
	
	//generate unique name for new file
	$now = time();
	while(file_exists($upload_filename = $upload_dir . $now . '-' . $_FILES['userfile']['name'])) {
		$now++;
	}
	
	//var_dump($upload_dir);
	//var_dump($upload_filename);
	
	//save new image
	if(imagejpeg($dst, $upload_filename, 80)) {
		echo 'Image Saved';
		return true;
	} else {
		echo 'Not Saved';
		return false;
	}
	
}

And for further information I’ll show you guys how I’m trying to define the correct path in my config.inc.php file:


$config['app_dir'] = dirname(dirname(__FILE__)); //                             "/home/tacres01/public_www/w1fma"
$config['upload_dir'] = dirname($config['app_dir']) . '/w1_uploads/'; //      "/home/tacres01/public_www/w1_uploads/"

The dir /w1_uploads/ does exist and has the correct permissions for writing set. It is one level above what I’ve defined as the ‘app_dir’

I really hope I’m just being very stupid and I’m missing something obvious.

Any help from you smart people will be very much appreciated.

Ta.

Did you inspect the output of :


 var_dump($_FILES);

If the ‘error’ array is anything other than 0, then that is where to start looking.

I have checked this within the function and everything looks good. Error 0. So I know that within $src_image I have the details of the [‘tmp_name’] from the file uploaded by the form.

Is it correct of me to reference the file upload using this [‘tmp_name’] for use with ‘imagejpeg’, and for the second parameter to be $upload_filename (the location and name of the new file I want to create) ?

Hey there. Please can you run the command:

print_r($_FILES);

Somewhere within the function, and paste the results into this thread?

Pete :slight_smile:

Here is the result of print_r($_FILES)

Array ( [userfile] => Array ( [name] => landscape-medium.jpg [type] => image/jpeg [tmp_name] => /var/tmp/phpnVaOBj [error] => 0 [size] => 19837 ) )

I’m confused by the error I’m receiving here, as it states the file that I’m trying to write as a new file couldn’t be opened. Obviously my intention isn’t to ‘open’ this location but rather to create it.

Warning: imagejpeg() [function.imagejpeg]: Unable to open ‘/home/tacres01/public_www/w1_uploads/1348823198-landscape-medium.jpg’ for writing: No such file or directory in /home/tacres01/public_www/w1fma/includes/functions_inc.php on line 54

This issue has now been resolved. Many thanks to anyone who tried to provide some insight for me.

It turns out the issue is the way in which the University server runs. Even though I’m able to create and set full permissions on a folder above the /public_www/ level, some kind of odd wizardry occurs that makes a dir at this level essentially invisible. It is perhaps a safe mode restriction of some sort. Anyway, by using an alternative location for the uploaded files, the code now works nicely.

Onwards and upwards…