Welcome Guest, Not a member yet? Register   Sign In
URGENT: How to retrieve values of uploaded files after processed with image_lib?
#1

[eluser]ColinHoernig[/eluser]
Say I have a upload form that resizes the original uploaded image and also creates a thumbnail of the same image. If I upload a file that has the name "image name here.gif" it uploads as "image_name_here.gif". How would I access the newly created images in order to insert their urls (not full, just the filename ex: "image_name_here.gif") into a database.

Right now I'm doing it like this:

do_upload method
Code:
function _do_upload($field_name) {
        $config['upload_path'] = './img/products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1600';
        $config['max_height'] = '1200';

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload($field_name)) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('administration/products/add_product', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());

            $temp = $this->upload->data();

            $config['image_library'] = 'GD2';
            $config['source_image'] = './img/products/' . $temp['file_name'];
            $config['maintain_ratio'] = TRUE;
            $config['create_thumb'] = TRUE;
            $config['quality'] = '80';
            $config['height'] = '100';
            $config['width'] = '100';

            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            $this->image_lib->clear();

            $config['create_thumb'] = FALSE;
            $config['height'] = '250';
            $config['width'] = '250';
            $this->image_lib->initialize($config);
            $this->image_lib->resize();

            $data = array('upload_data' => $this->upload->data());
            return $data;
        }
    }

and part of the add product/upload method
Code:
$upload = $this->_do_upload('product_image');

        if($upload != NULL) {
            $img_url = str_replace(" ", "_", $_FILES['product_image']['name']);
            $product_image_url = str_replace("'", "", $img_url);
            $pos1 = strpos($product_image_url, ".");
            $file_type = substr($product_image_url, $pos1);
            $product_thumb_url = substr($product_image_url, 0, $pos1);
            $product_thumb_url .= "_thumb";
            $product_thumb_url .= $file_type;
I know this isn't the way to do it, because there must be a way to access the new data from the resized and thumbnailed files, rather than using the actual filename from the uploaded file (because image_lib library changes it).

The way I'm doing it now doesn't work for files with spaces in them, or files that have extra periods in them.

Say I upload a file called "image name 1.0 here.gif", input into the database would be "image_name_1_thumb.0_here.gif" which isn't what I need, it needs to be "image_name_1.0_here_thumb.gif".

Any help would be greatly appreciated.
#2

[eluser]ColinHoernig[/eluser]
Sorry to be so hasty and blunt, but I really need help with this, project is due very soon!

Thank you so much.
#3

[eluser]kgill[/eluser]
Ok first things first, don't bump you're own posts - especially when an hour hasn't even elapsed since you first asked for help. If someone is going to answer they will, bumping things is likely to have the opposite effect you're after.

The reason you're getting the thumb_ in the wrong spot is right there in the code you posted for the upload method.
Code:
$pos1 = strpos($product_image_url, ".");

You're grabbing the position of the first period you find and using that to build your filename, use strrpos to find the last occurrence of the period instead.
#4

[eluser]ColinHoernig[/eluser]
[quote author="kgill" date="1230995194"]Ok first things first, don't bump you're own posts - especially when an hour hasn't even elapsed since you first asked for help. If someone is going to answer they will, bumping things is likely to have the opposite effect you're after.

The reason you're getting the thumb_ in the wrong spot is right there in the code you posted for the upload method.
Code:
$pos1 = strpos($product_image_url, ".");

You're grabbing the position of the first period you find and using that to build your filename, use strrpos to find the last occurrence of the period instead.[/quote]

Ah, you are very much right and I am sorry. I do appreciate your help, so thank you. Although, it wasn't an hour later..it was a day later, just posted around the same time.
#5

[eluser]kgill[/eluser]
my bad, sorry about that - this is what I get for posting at 4am.




Theme © iAndrew 2016 - Forum software by © MyBB