Welcome Guest, Not a member yet? Register   Sign In
uploading and using images
#31

[eluser]textnotspeech[/eluser]
The class has a max height and max width setting. I don't mind helping, but you have to actually read the code that I'm sending or else you won't learn how to do these things for yourself. I'm not mad, but you gotta see it from my perspective it seems like your not even reading the code I posted. If you didn't understand it, that's one thing, but you didn't seem to notice those features. If you find a way that it works with CI's upload class I'd be interested in seeing the code. Don't take this the wrong way, I don't want you to think that I'm angry.
#32

[eluser]M4rc0[/eluser]
No no not at all, it's okay Smile

I'm afraid you are getting me wrong too.
To me it seems like you are not reading my posts carefully either, or maybe i'm the one not expressing myself clearly.
My posts above your replies are always explaining what you are mentioning, as i don't want to be repetitive, i'd rather beeing on second plan here and posting again only when i get a conclusion of my problem.

For example, again when i mean validation of max height and max width, it would be adapting your code to use the validation error string that i'm displaying in the form in case of errors
Quote:It’d be a lot of trouble to add stuff like valid extensions, max height, max width, max size and set them all with $this->validation->error_string = ‘MSG’;

I'm not mad either, i'm actually happy to be using your lib that is great (i really like it).

I did read the whole code and i tweaked as well. And when i asked for opinion it's more teorically then pratically, no coding, just what step should i take next since i reached a point that i'll have to re-think what to do (imagination can't get the renamed file name, not imagination's fault but CI's), so i need a different aproach.

I apologize if you got me wrong and for the trouble.

I will post the changes later.
#33

[eluser]textnotspeech[/eluser]
It's all good. I think I'm just misunderstanding some of your posts. I have other classes that I use for my own error handling and notifying and that is why there's none in the imagination class. It may not be the best way to go but it keeps things portable and prevents redundancy. Just so you know, if the image is larger than the max values, it will resize the image to the max values. I should have taken some time to shape it up for more general use but I figured I'd just post it and let you adapt it. I'm always looking for better ways to do things so if you have any suggestions, let me know. :cheese:
#34

[eluser]M4rc0[/eluser]
Ok so here i am Smile

Less talk more code this time, since i have lots to show.

I choose Imagination to upload (temp place) and rename and upload (correct place). I'm not using CI upload helper since that would be one extra unnecessary upload. So i had to add a function to rename the picture (random hash) and to display a validation error in case of invalid file type.

I'm not caring about max width or max height since imagination takes care of resizing it according to it's ratio (rocks). Size validation can be important but that can wait Smile
So i only care about file type now, since i was successfully adding with .zip files (or any other extension).

Here's my controller code:
Code:
function add()
{
if($this->validation->run() == FALSE OR $this->resize_img($_FILES['cover']['name'], $_FILES['cover']['tmp_name'],'public/images/covers/') == FALSE)
         {
            $this->template->write_view('content','movie/add');
            $this->template->render();
         }
         else
         {
            $this->success();
           }        
}

function resize_img($name,$path,$dest=FALSE)
    {        
        $this->load->library('imagination');    
        if($image->valid == TRUE){
            $image->resize(400,400);
            return $image->new_name; //to add on the db
        }else{ return FALSE; } //means invalid file type
    }

Now the imagination changes:
I have on the top of the variables a "public $new_name;"

Added the function encrypt_name:
Code:
public function encrypt_name()
        {
            mt_srand();
            $this->new_name = md5(uniqid(mt_rand())).'.'.$this->type;

            return $this->new_name;            
        }

And on your resize function, there's an if in the beginning, i just added the else code:
Code:
if($this->valid)
{ //(...)
}else{$CI->validation->error_string = "Invalid file type.";}

So it works! Not perfectly but it works.

If i choose a file other then a valid image type, it shows the form again but without the "Invalid file type" error.
But at least it doesn't go Big Grin

My trouble is with $CI variable that i see you get with "$CI =& get_instance();"
I have never worked with that before, so i tought it would set on my controller (CI instance?) the validation error but it's not setting it. Maybe i have to use the return of the resize function for this. What you think?
#35

[eluser]M4rc0[/eluser]
Yep, just as i tought.

I had to change.

In the controller:
Code:
function resize_img($name,$path,$dest=FALSE)
    {        
        $this->load->library('imagination');
        $image = new Imagination($name,$path,$dest);
        if($image->valid == TRUE){
            $image->resize(400,400);            
            $this->cover_name = $image->new_name;
            return TRUE;//
        }else{
                        $this->validation->error_string = "<div class='error'>Invalid file type.</div>"; return FALSE;
               }
    }

The controller now have a private variable called "cover_name" that contains the new cover name.

The problem i was having is that i had to return two values: TRUE or FALSE if it worked resizing, and if TRUE the name of the new generated image so i save on database. The only way i found to do this is a private variable called cover_name on the controller (cover is the image). Is there a better way to do this? It feels this variable is not necessary..

So i removed the else on the imagination resize function and now added this on the add function:
Code:
if($this->validation->run() == FALSE OR $this->resize_img($_FILES['cover']['name'], $_FILES['cover']['tmp_name'],'public/images/covers/') == FALSE)
         {
            $this->template->write_view('content','movie/add');
            $this->template->render();
         }
         else
         {
            $img_link = 'public/images/covers/'.$this->cover_name;
            $this->Movie_model->insert_movie($img_link); //on the model saves all the fileds to db plus the img link got by parameter    
            $this->success();
           }
#36

[eluser]textnotspeech[/eluser]
The $CI->validation is referring to my validation library and not CI's. I've built my own validator that works a little different way than CI's. I am found of doing things my way, and that's why I rewrite a lot of stuff CI does already. This is probably not the best way to go about doing things as I suppose I could just extend CI but that's what I love about CI, it let's you do it your way very easily. If the validation library is already loaded, the
Code:
$CI =& get_instance();
will allow you to access that library inside the imagination class. If the library is not loaded you would need to do
Code:
$CI->load->library('validation');
. Hope that helps!
#37

[eluser]M4rc0[/eluser]
Quote:"but that’s what I love about CI, it let’s you do it your way very easily"

Couldn't agree more. Actually i like you validator too :cheese:
I think i'll use in the future. Right now i'm not doing the things how it should be either, i'm not checking for file size so far.

I just found a small problem after that renaming function.
I'm adding a thumb like this:
Code:
$image->resize(200,210);
$image->resize(120,140,'_thumb');


The problem is that it's generating a new name for the thumb, and not getting the name from the original image. It's not prepending either (because of the function too). So i have two resized images but with different names.

How can i get the name of the image before and just add the _thumb?
Without this rename function, how was it working? Was it keeping the original name and prepending the thumb?
like original.jpg original_thumb.jpg

And the multisize function is generating like 5 different images together.

Halp Smile
#38

[eluser]textnotspeech[/eluser]
Once you rename the file, that's it. Before you name it, you can access $this->image_name. maybe you could make an $orig_name private variable out of that in the constructor. That would leave all the functionality but would save the name to persist throughout changes to the image.
#39

[eluser]M4rc0[/eluser]
Good idea, i remembered i added a variable called $new_name, so i used.

In the resize function i did this:
Code:
if($this->new_name){
                    $save_path = $this->tmp_dir.$prepend.$this->new_name;
                }else{
                    $save_path = $this->tmp_dir.$prepend.$this->encrypt_name();
                }

Now it is getting the name, but it's overwriting it Tongue
It's not prepending the name "_thumb". Almost there, any idea?


You don't have to reply this fast if you are busy man haha
This is just for fun i don't want to trouble you Smile
#40

[eluser]textnotspeech[/eluser]
To be honest, I'm kinda lost at this point. I'm sure it's something simple you're overlooking. If you want to send me the modified class, I'd be glad to take a look at it.




Theme © iAndrew 2016 - Forum software by © MyBB