CodeIgniter Forums

Full Version: Thumbs creation with Image Library - Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Soares[/eluser]
Hello guys!

I found myself recently with a problem that I couldn't find a solution. Actually, my code was working and suddenly it has stopped.
Well.. The problem is:

I have an image upload form. After doing the upload (which is working okay) I got to create two thumbs - one with 500px and another with 100px. I don't know what I've changed, but now it just not works for certain images. For example, if I upload and image of 250 KB, the thumbs creation works perfectly. On the other hand, if I upload an image of 2MB, the thumbs are not created (although the upload still works).

What might be causing it?

Here is the thumb creation code:

Code:
/**
*    criarThumbs()
*    Após o upload ter sido efetuado, essa função é responsável por criar os thumbnails (big e mini). Arquivos
*    do tipo .bmp não são suportados.
*
**/
    function criarThumbs($info)
    {
        $this->load->library('image_lib');
        $config['image_library']     = 'gd2';
        $config['source_image']     = $info['full_path'];
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']     = TRUE;

        // mini_thumb
        $config['new_image']     = $info['file_path'] . '_mini_thumbs/'; // sem dizer o nome do arquivo. É automático.
        $config['width']         = 150;
        $config['height']         = 120;
        $config['quality']         = 100;

        $this->image_lib->initialize($config);
        log_message('debug', '[painel_acoes] Classe inicializada');

        echo "\nCriando mini_thumb: ";
        if ( ! $this->image_lib->resize())
        {
            echo "Erro:".$this->image_lib->display_errors();
        }
        else
        {
            echo "OK";
        }

        $this->image_lib->clear();

        // big_thumb
        $config['new_image']     = $info['file_path'] . '/_big_thumbs/';
        
        $width = 600;
        $height = 500;
        $config['width']           = $width;
        $config['height']          = $height;
        $config['quality']         = 100;
        
        $this->image_lib->initialize($config); // inicia novamente, com as novas configurações do big_thumb.
        echo "\nCriando big_thumb: ";
        if ( ! $this->image_lib->resize())
        {
            echo "Erro:".$this->image_lib->display_errors();
        }
        else
        {
            echo "OK";
        }
    }


The variable $info is an array containing the following information:
Code:
Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

Anybody has a tip of what might be happening?

Thanks in advance!

El Forum

[eluser]jedd[/eluser]
What is your memory_limit currently set to in your php.ini file, and/or what happens if you bump that number up a goodly way (say around 100mb - and restart apache, of course).

El Forum

[eluser]Soares[/eluser]
That solved part of the problem. Thanks a lot!
The memory_limit was set to 5MB. I changed it to 100MB and it works perfectly in my local server.

However, when I upload my files to the webserver, a new problem appears. The code creates only the first thumbnail. I really don't know why, since in the local server the exact same code is perfectly working. I suppose it has something to do with the php configuration, but I sincerely don't know what. The webserver memory_limit is also set to 100MB now.

Any thoughts?
And thanks again for solving the first problem!

El Forum

[eluser]jedd[/eluser]
What is your max_execution_time set to? It should be just above the memory setting you just played with.

It sounds like you've got errors turned off - you might want to enable them again so you can see when things go wrong. Check display_errors (and also the earlier error_reporting) settings.

El Forum

[eluser]Soares[/eluser]
My max_execution_time is set to 30. What do you mean? This should be also set to 100?

The error_reporting is still set to E_ALL.
What kind of errors it could present? My big question is why the whole thing works on my local server and does not work on the webserver.
It might have something to do with the server configuration. Am I thinking wrong?

El Forum

[eluser]Xeoncross[/eluser]
Quote:I really don’t know why, since in the local server the exact same code is perfectly working. I suppose it has something to do with the php configuration, but I sincerely don’t know what.

This is a problem that everyone seems to face when they are getting started. To prevent PHP from using too many resources the OS (linux/windows) limits the amount of memory that PHP can use and how long it can run before it kills it.

max_execution_time = the time that php has to run before it is killed. 30 = 30 seconds and then the OS kills it weither it is finished or not.

memory_limit = the amount of memory that the OS will let that php thread have before it kills it for taking too much RAM.

Good defaults would be more like
Code:
memory_limit = 10mb;
max_execution_time = 15;

If your server can't make two small thumbnails in 15 seconds then there is something seriously wrong. Also, you should limit file uploads to images less than 2-5MB.

El Forum

[eluser]Soares[/eluser]
@Xeoncross

I see your point. Making some tests here, I noticed that it has no effects in my thumbnail script. I set the configurations to this two modes:

Code:
memory_limit = 500M
max_execution_time = 100
and
Code:
memory_limit = 10M
max_execution_time = 15

None of this has worked.
I'm really lost here.
In my local server the script worked with both mentioned configurations.

I was thinking.. The problem appears to be in the part that re-initialize the class, since the first thumb (mini) is successfully created.
If I strip off the first part of the code, the other thumb (big) is created successfully.
So, my thought is that multiple thumbs cannot be created. Which is really weird, since in the local server it works!

...
I have no clue about what's happening.

El Forum

[eluser]jedd[/eluser]
What I'd do next ...

.. compare the two php.ini files using a tool like (on KDE) kdiff3 - but I'm sure such side-by-side comparison tools exist for whatever platform you're on.

This will very quickly give you an understanding of what's different between the two PHP configurations.

The fact that one works and one doesn't suggests a memory or time constraint, hence the questions above - these are the kinds of things defined in the php configuration files. There might be some apache feature that is biting you - but changes are not so good that the apache configurations on the two boxes are so easily compared.

El Forum

[eluser]Xeoncross[/eluser]
You haven't yet stated what platform your using - what is your localhost and what is your webserver? Also, until you get this problem worked out you should still keep your settings insanely high like jedd stated.

Code:
memory_limit = 500M
max_execution_time = 200

Also, if you are using AJAX to do this then log the output when you are done. If this is a regular HTTP request then is the final page saying success (or whatever your doing) showing? Do you have error_reporting and display_errors on? Have you checked the PHP error log?

El Forum

[eluser]jedd[/eluser]
Ah yes, I should probably have thrown some more heuristics in to the mix.

If the page responds quick (couple of seconds) and your second image hasn't been thumbnailed, then it's not a time-out issue (you say it's set to 15s, yes?). It suggests it's more a memory or size (file-size of upload) limit.

Have you changed the order of the two files you're throwing up - to see if it's the second *file*, or the *second* file, that's failing (if you see what I mean).

Have you tried sending the same file (that works) twice, using a different name for the second instance of it? And/or trying with two very small files.

As I alluded to, but wasn't explicit about, having display_errors turned on is helpful, and it's good that you have error_reporting = E_ALL already.
Pages: 1 2