Jump to content
Sign in to follow this  
imgrooot

Need help with new image resize and upload script. Check out my code.

Recommended Posts

I am trying out a new script that lets me resize an image before uploading. It is based on this script. http://www.w3bees.com/2013/03/resize-image-while-upload-using-php.html

 

Basically what happens is, it resizes and makes 3 thumbnails and puts them in their relative folder.  That works.

The part that's giving me the problem is when inserting it into the database. Error says the "image_path" cannot be null. Since it's creating an array of 3 different thumbnails, should I create 2 more fields in the database tabel to account for that? If so, how would I insert the 3 different thumbnail paths into the query?  What would it look like? 

 

Below is my code.

<?php

$db_userid	= intval($row['user_id']);
$get_item_id	= intval($row['item_id']);

// settings
$max_file_size = 5242880; // 5mb
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');

// thumbnail sizes
$sizes = array(100 => 100, 150 => 150, 250 => 250);

// dir paths
$target_dir = 'images/'.$db_userid.'/items/'.$get_item_id.'/';

if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['image'])) {

	if(!empty($_FILES['image']['name'])) {
		
		
		if($_FILES['image']['size'] < $max_file_size ){
		
			// get file extension
			$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
			
			if(in_array($ext, $valid_exts)) {
			
				function resize($width, $height){
				
					global $db_userid;
					global $get_item_id;
				
				  /* Get original image x y*/
				  list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
				  
				  /* calculate new image size with ratio */
				  $ratio = max($width/$w, $height/$h);
				  $h = ceil($height / $ratio);
				  $x = ($w - $width / $ratio) / 2;
				  $w = ceil($width / $ratio);
				  
				  /* new file name */
				  $path = 'images/'.$db_userid.'/items/'.$get_item_id.'/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
				  
				  /* read binary data from image file */
				  $imgString = file_get_contents($_FILES['image']['tmp_name']);
				  
				  /* create image from string */
				  $image = imagecreatefromstring($imgString);
				  $tmp = imagecreatetruecolor($width, $height);
				  imagecopyresampled($tmp, $image,
					0, 0,
					$x, 0,
					$width, $height,
					$w, $h);
					
				  /* Save image */
				  switch ($_FILES['image']['type']) {
					case 'image/jpeg':
					  imagejpeg($tmp, $path, 100);
					  break;
					case 'image/png':
					  imagepng($tmp, $path, 0);
					  break;
					case 'image/gif':
					  imagegif($tmp, $path);
					  break;
					default:
					  exit;
					  break;
				  }
				  return $path;
				  
				  /* cleanup memory */
				  imagedestroy($image);
				  imagedestroy($tmp);
				}
				
				/* resize image */
				foreach($sizes as $w => $h) {
				
					if(!is_dir($target_dir)){
						mkdir($target_dir, 0775, true);
					}
					
					$files[] = resize($w, $h);
				}  
			  
				$insert_image = $db->prepare("INSERT INTO images(user_id, item_id, image_path, date_added) VALUES(:user_id, :item_id, :image_path, :date_added)");
				$insert_image->bindParam(':user_id', $db_userid);
				$insert_image->bindParam(':item_id', $get_item_id);
				$insert_image->bindParam(':image_path', $path);
				$insert_image->bindParam(':date_added', $item_date_added);
				if(!$insert_image->execute()) {
				
					$errors[] = 'There was a problem uploading the image!';
				
				} else {
				
					if(empty($errors)) {
						$db->commit();
						
						$success = 'Your item has been saved.';
					
					} else {
						$db->rollBack();
					}	
				
				}

			} else {
			  $errors[] = 'Unsupported file';
			}
		
		} else{
			$errors[] = 'Please upload image smaller than 5mb';
		}
	  
	} else {

		$errors[] = 'An image is required!';
	}
}
Edited by imgrooot

Share this post


Link to post
Share on other sites

That error is caused because have a variable scope issue.

 

You are trying to bind $path to the :image_path placeholder. Problem is $path is defined inside the resize() function. Functions have their own variable scope. Meaning variables defined inside them are not accessible outside of them, unless you define them as global - WHICH YOU SHOULD NOT DO

 

The resize function is returning the path for thumbnail it has created. You would need to execute the prepared statement inside the foreach loop if you want to store the path for each thumbnail being created. Each thumbnail will be inserted as a new row.

Share this post


Link to post
Share on other sites

That error is caused because have a variable scope issue.

 

You are trying to bind $path to the :image_path placeholder. Problem is $path is defined inside the resize() function. Functions have their own variable scope. Meaning variables defined inside them are not accessible outside of them, unless you define them as global - WHICH YOU SHOULD NOT DO

 

The resize function is returning the path for thumbnail it has created. You would need to execute the prepared statement inside the foreach loop if you want to store the path for each thumbnail being created. Each thumbnail will be inserted as a new row.

Oh I understand it now. I will do that.  Thanks.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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