I am having the following block of code in my function:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['image']['name'])." has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
Now I want to resize the image before uploading. I am using the CodeIgniter framework. I have this code:
$config['upload_path'] = "uploads/";
$path=$config['upload_path'];
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG';
$config['max_size'] = '1024';
$config['maintain_ratio'] = TRUE;
$config['max_width'] = '1000';
$config['max_height'] = '1000';
$this->load->library('upload',$config);
But this isn’t working properly. I am looking for a good solution where image is uploaded by my specified height and width.
This is the function I use to allow our staff to upload product images, it uploads the full size image and two smaller images (I only included the code for one here the tn_ version). It takes the values it does because it’s in my controller and can be called from multiple places. $control is the name of the fileUpload control you’re using, $path is the save path, $imageName is the from the control and sizes just allows me to specify which versions to make, in my case it receives all, med and tn as options. You could make as many or as few as you need. As VDP mentioned you’re limited to under 2mb if you don’t change any settings but that’s fine for me so I just return an error if it’s over that.
I don’t use the CI image upload library at all btw. It’s just sent to the controller via a normal file upload and ajax. It uses an iframe on the main view to display errors or success.
My Controller upload function:
View HTML:
View ajax call:
Regular controller function:
config/constants.php << for the LOGO_PATH. Change these (and the name) to suit your purposes. The reasoning is if I ever change where I want to save images I change it in the constants rather than in 10 places throughout my application.