I tried to upload two images using a form. To do that I wrote a image function.
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
And I access that function this way,
// allocate image name to data set
if(!empty($_FILES["thumb_image"]['name'])){
// thumb upload
$thumb_image_new_name = $this->imageUploadResize($_FILES["thumb_image"]['name'],'thumb_image',THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, '_thumb');
$data['thumb_image'] = $thumb_image_new_name;
}else{
$data['thumb_image'] = "";
}
// allocate image name to data set
if(!empty($_FILES["banner_image"]['name'])){
// banner upload
$banner_image_new_name = $this->imageUploadResize($_FILES["banner_image"]['name'],'banner_image',BANNER_IMAGE_WIDTH, BANNER_IMAGE_HEIGHT, '_banner');
$data['banner_image'] = $banner_image_new_name;
}else{
$data['banner_image'] = "";
}
When I upload one image (thumb_image or banner_image) above function is worked properly.
But when I upload both images thumb_image uploaded properly but banner_image did not upload properly. As a example assume I am going to upload Hydrangeas.jpg and Desert.jpg. Then it is working this way,
Then file uploaded this way (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Hydrangeas1.jpg - image name also wrong
But expected output is (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Desert.jpg
1493025280_Desert_banner.jpg
Can someone please help me, thank you..
A different approach that i use for file uploads is below.
I rearrange the $_FILES array because the original has some difficulties with keys and objects.
$files = array();
foreach ($_FILES['files']['name'] as $num_key => $dummy) {
foreach ($_FILES['files'] as $txt_key => $dummy) {
$files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key];
}
}
In new array, loop through each image and do what you need:
foreach ($files as $file) {
if ($file['error'] == 0) {
//Your code here...
//Call your function imageUploadResize
}
}
Finally found the solution, In codeigniter when upload image, we have to call,
$this->load->library('upload', $config);
Then we have to initialized the configurations.
$this->upload->initialize($config);
And when resize image we have to call,
$this->load->library('image_lib', $config);
Then have to initialized the configurations.
$this->image_lib->initialize($config);
Therefore completed function is,
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
Related
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
my controller:
public function addgalleryProcess()
{
$height = $this->input->POST('height');
$width = $this->input->POST('width');
$config['upload_path'] = './assets/images/gallery';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/addgallery', $error);
}
else
{
$upload_data = $this->upload->data();
//resize:
$config1['image_library'] = 'gd2';
$config1['source_image'] = $upload_data['full_path'];
$config1['maintain_ratio'] = TRUE;
$config1['create_thumb'] = TRUE;
$config1['width'] = $width;
$config1['height'] = $height;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
$this->adminModel->galleryimages($upload_data);
$this->load->view('admin/homeView');
}
my model:
public function galleryimages($image_data = array())
{
$data = array(
'image' => $image_data['file_name'],
);
$this->db->insert('gallery', $data);
}
Here image is uploaded properly and working, but resize is not working. I have to display the resized image with specified width and height. I am new to this.
Thanking in advance.
Try this:
For image resize I suggest to use "TimThumb" TimThumb – PHP Image Resizer
https://www.binarymoon.co.uk/projects/timthumb/
I have use timthumb in my project like
<img src="<?php echo base_url('assets/common/timthumb') . '/timthumb.php?src=./assets/myuploads/myimagename.jpg&w=245&h=164'; ?>" alt="">
I have put timthumb.php in "assets" folder and I uploaded my images in "assets/myuploads" folder
Not to be to distracting, if you want to resize the image before upoading through the networks, you can javascript it to a canvas and then send the canvas image in toDataURL. This would help prevent someone from uploading something massive and your network and server struggle with it.
This also depends if your client in a HTML client.
The following Article can help with this,:
https://stackoverflow.com/a/10334170/811827
I'm trying to upload an image and everything is working fine, except the width and heigh of the resize. the strange part is, if I add px on the $config_img['width'] and $config_img['height'] I got an error saing:
"A non well formed numeric value encountered
Filename: libraries/Image_lib.php"
What is obvious because the px on the value, but this way the image do resize.
Here is my upload image function:
function upload_foto_acompanhe_sua_obra($field) {
$dir = realpath('assets/uploads/acompanhe_sua_obra');
$config['upload_path'] = $dir;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = '500000';
$config['max_width'] = '10024';
$config['max_height'] = '7068';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = $field;
if ($this->upload->do_upload($field_name)) {
$dados = $this->upload->data();
$this->load->library('image_lib');
$this->image_lib->clear();
$size = getimagesize($dados['full_path']);
$config_img['image_library'] = 'GD2';
$config_img['source_image'] = $dados['full_path'];
$config_img['create_thumb'] = FALSE;
$config_img['maintain_ratio'] = TRUE;
$config_img['encrypt_name'] = TRUE;
$config_img['width'] = '1092px';
$config_img['height'] = '295px';
$this->image_lib->initialize($config_img);
$this->image_lib->resize();
// Returns the photo name
return $dados['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
return $error;
}
}
That's it, any help will be appreciated! Thanks!
I am trying to make a simple app with Codeigniter where i need to store thumbnail image path in MySql database. The images along with the thumbnail versions upload correctly in the upload folder and I can store the image path in my database while uploading but i can not store the thumbnail image path in the database. I tried this in the controller:
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data'] ['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
)
But it only stores "./uploads/" in the thumbnail_path column in my table, not the actual thumbnail path. Where i expect it to store something like "./uploads/Lighthouse_thumb.jpg". I can not figure out how to get the path of the thumbnail images to store them in the database. Here is my full upload controller:
class Upload extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form');
}
function index() {
$this->load->view('upload_form', array('error'=>''));
}
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$image_data = $this->upload->data();
$imgpath = './uploads/' . $image_data['file_name'];
$data = array('upload_data'=>$this->upload->data());
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
);
$this->load->model('postimage_path');
$this->postimage_path->insert($newRow);
$this->load->view('upload_success', $data);
}
}
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
$thumbnail_path = './uploads/'. $thumbnail;
So thumbnail_path contains only "./uploads/" : that tells us that $thumbnail is false (or null or empty string etc.), meaning that the return value of the call to resize is wrong - it seems you expect it to return the file name.
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
resize returns nothing! And you assign that nothing to $thumbnail_path. That's your problem.
You seem to have simply copied the code from the CodeIgniter docs. From said docs (emphasis mine):
The above code tells the image_resize function to look for an image called mypic.jpg located in the source_image folder, then create a thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called mypic_thumb.jpg
So there you have it! If your filename is $data['upload_data']['file_name'], your thumbnail will be $data['upload_data']['file_name'] . "_thumb". Have a look at your file system, the files should be there for you to see.
So to fix your problem, this should do the trick:
$pathinfo = pathinfo($data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];
Well i have the following problem:
I'm using CodeIgniter 2.1.3 and want to upload an image and create two scaled images. one that is resized to the size of around 50x50 and one that is resized to around 200x200.
Uploading is doing fine.
But when i want to resize the following error occures:
Unable to save the image. Please make sure the image and file directory are writable.
I've set my permission on the server to 777, but it still doesn't compute
Here is the code of the classes:
public function uploadimage()
{
$head = array('title' => 'Upload Image');
$error = array('error' => '');
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
public function changeimage()
{
$config = array();
$config['upload_path'] = 'img/Upload';
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
$config['max_size'] = '100';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['overwrite'] = TRUE;
$config['file_name'] = md5($this->session->userdata('username')).".png";
$head = array('title' => 'Upload Image');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
else
{
$uploaddata = $this->upload->data();
//Create Thumb
$config2 = array();
$config2['image_library'] = 'gd';
$config2['source_image'] = $uploaddata['full_path'];
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 50;
$config2['height'] = 50;
$config2['new_image'] = '/~3612546/DateSite/img/ProfileThumbs/'.$uploaddata['file_name'];
$this->image_lib->initialize($config2);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
//Create resized original image so images < 200x200 get resized to get as close to 200x200
$config3 = array();
$config3['image_library'] = 'gd';
$config3['source_image'] =$uploaddata['full_path'];
$config3['maintain_ratio'] = TRUE;
$config3['width'] = 200;
$config3['height'] = 200;
$config3['new_image'] = '/~3612546/DateSite/img/ProfileImages/'.$uploaddata['file_name'];
$this->image_lib->initialize($config3);
if ( ! $this->image_lib->resize())
{
echo $uploaddata['full_path'];
echo $this->image_lib->display_errors();
}
}
}
i really don't think tilde is good for upload urls:
/~3612546/DateSite/img/ProfileImages/
then, all your urls are upper/lowercase, put them all to lowercase like this:
/~3612546/dateSite/img/profileimages/
in the end post what error do you receive now that you chmod 777 your upload folder, do you added 777 only to dir or dir+all his content? (it's important dir+all his content to 777)