Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Intermittent Image Manipulation library issue
#1

[eluser]Bondie[/eluser]
Quote:Your server does not support the GD function required to process this type of image.

I'm using the Image Manipulation Library to resize user uploaded images which is then input into a database called images, to give you a quick outline on how its setup:

Code:
CREATE TABLE IF NOT EXISTS `images` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `alias` varchar(40) NOT NULL,
  `file_location` text NOT NULL,
  `original_width` int(11) NOT NULL,
  `original_height` int(11) NOT NULL,
  `resized_versions` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

The alias is the ORIGINAL file name when uploaded, the resized_versions is a json array of resized image values for example:

Code:
{"75_75":{"width":75,"height":75,"file_location":"GBPIMtcC4PjlsTXK.JPG"},"60_60":{"width":60,"height":60,"file_location":"iPKKYSDtFMcC48oQ.JPG"}}
75_75 = width_height

okay, now you know how the database is working this is how I'm using the Image Manipulation library...

For example when I call the function and this can be called anywhere in a page between 1 and 50 times...
Code:
$this->mimage->display_image('no-image', 75, 75, 'image', array
                                        ('title' => 'no image available'));

// This is just an example, and its not always no-image called...

The code for MImage model:

#2

[eluser]Bondie[/eluser]
Code:
<?php
class MImage extends CI_Model
{
    public $upload_prefix = './../';
    public $upload_dir = 'uploads/images/';

    function get_image($alias = '', $width = '', $height = '', $attributes = array())
    {
        $get_image = $this->db->select('id, alias, file_location, original_height, original_width, resized_versions')->
            from('images')->where('alias', $alias)->get();

        if ($get_image->num_rows() == 1) {
            $img = $get_image->row();
            if (file_exists('./../' . $this->upload_dir . $img->file_location)) {
                $alias = 'no-image';
                $get_image = $this->db->select('id, alias, file_location, original_height, original_width, resized_versions')->
                    from('images')->where('alias', $alias)->get();
                $img = $get_image->row();
            }
        } else {
            $alias = 'no-image';
            $get_image = $this->db->select('id, alias, file_location, original_height, original_width, resized_versions')->
                from('images')->where('alias', $alias)->get();
            $img = $get_image->row();
        }

        if (($height == '' && $width == '') || ($height == $img->original_height && $width ==
            $img->original_width)) {
            $return = explode('./', $img->file_location);
            return base_url($return[1]);
        } else {
            $resized_images = (array )json_decode($img->resized_versions);
            if (isset($resized_images[$width . '_' . $height])) {
                return base_url($this->upload_dir . $resized_images[$width . '_' . $height]->
                    file_location);
            } else {
                // load the string helper to generate us a random string for the image name
                $this->load->helper('string');

                // get the extension of the image from the original file
                $img_ext = explode('.', $img->file_location);

                // get the new randomly generated name for the resized image
                $new_image_rand = random_string('alnum', 16) . '.' . $img_ext[1];

                $this->load->helper('path');

                $config['dynamic_output'] = false;
                $config['quality'] = 100;

                $config['new_image'] = set_realpath($this->upload_dir) . $new_image_rand;
                $config['source_image'] = set_realpath($this->upload_dir) . $img->file_location;

                $resized_array = array($width . '_' . $height => array('width' => $width,
                    'height' => $height, 'file_location' => $new_image_rand));
                if ($height != '') {
                    $config['height'] = $height;
                }
                if ($width != '') {
                    $config['width'] = $width;
                }
                if ($height == '' || $width == '') {
                    $config['maintain_ratio'] = true;
                } else {
                    $config['maintain_ratio'] = false;
                }

                // load the config into the image manipulation library
                $this->load->library('image_lib', $config);


                // run the resize script and get the result
                if (!$this->image_lib->resize()) {

                    // if there are errors print them out for testing purposes
                    print $this->image_lib->display_errors();

                } else {
                    // if its uploaded and manipulated okay then we add it into the database
                    if (file_exists($config['new_image'])) {
                        $this->db->where('alias', $alias)->update('images', array('resized_versions' =>
                            json_encode(array_merge($resized_images, $resized_array))));

                        return base_url('uploads/images/' . $new_image_rand);
                    } else {
                        print 'Error in image manipulation';
                    }

                }
                $this->image_lib->clear();
                unset($config);
            }
        }

    }

    function display_image($alias = '', $width = '', $height = '', $alt = 'image', $attributes =
        array())
    {
        $rtn = '<img src="' . $this-&gt;get_image($alias, $width, $height) . '" width="' .
            $width . '" height="' . $height . '" alt="' . $alt . '">_parse_attributes($attributes);
        $rtn .= '/>';
        return $rtn;
    }


    /**
     * Used from URL helper
     **/
    function _parse_attributes($attributes, $javascript = false)
    {
        if (is_string($attributes)) {
            return ($attributes != '') ? ' ' . $attributes : '';
        }

        $att = '';
        foreach ($attributes as $key => $val) {
            if ($javascript == true) {
                $att .= $key . '=' . $val . ',';
            } else {
                $att .= ' ' . $key . '="' . $val . '"';
            }
        }

        if ($javascript == true and $att != '') {
            $att = substr($att, 0, -1);
        }

        return $att;
    }
}

I can load the page the first time (empty rezized_images array in the database column) and it'll resize one image, then the rest of the images on the page will resize and get a random name generated, but it doesn't upload? :S refresh the page and then the next image works okay but the rest aren't, basically every time I refresh, the next image appears... but I get a tonne of errors.

I keep getting two errors messages, the one at the top of the post quoted, and my custom one...

Quote:Error in image manipulation

It's been driving me crazy for hours, if anyone can shed any light I'd more than appreciate it!
#3

[eluser]John_Betong_002[/eluser]

Try this:

Code:
$result =NULL; // default

// run the resize script and get the result
  if (!$this->image_lib->resize())
  {
    // if there are errors print them out for testing purposes
    print $this->image_lib->display_errors();
  }
  else
  {
    // if its uploaded and manipulated okay then we add it into the database
    if (file_exists($config['new_image']))
    {
      $this->db->where('alias', $alias)->update('images', array('resized_versions' =>
          json_encode(array_merge($resized_images, $resized_array))));
          
     # NEW POSITION
     $this->image_lib->clear();
     unset($config);
    
    # Maybe best to only have one return value
    $result =  base_url('uploads/images/' . $new_image_rand);
     # return base_url('uploads/images/' . $new_image_rand);
    }
    else
    {
      print 'Error in image manipulation';
    }

  }
  # MOVED to NEW POSITION because not called if (file_exists(...))
  # $this->image_lib->clear();
  # unset($config);
  
  return $result;
}
&nbsp;
&nbsp;
#4

[eluser]Bondie[/eluser]
Thank you for your reply John, unfortunately that didn't seem to fix the problem, and I'm getting the following error about 21 times whilst loading 7 images, but its still only doing the first image each time on the page... :\

Quote:Your server does not support the GD function required to process this type of image.

This errors obviously a lie because it's already done one image on that page *sigh*

Here's the updated source code of mimage model:
Code:
&lt;?php
class MImage extends CI_Model
{
    public $upload_prefix = './../';
    public $upload_dir = 'uploads/images/';
    public $default_alias = 'no-image';

    function get_image($alias = '', $width = '', $height = '', $attributes = array())
    {
        // load the helpers used
        $this->load->helper(array('string','path'));

        $result = null; // default
        
        // Get the specified image from the database
        $get_image = $this->db->select('id, alias, file_location, original_height, original_width, resized_versions')->
            from('images')->where('alias', $alias)->get();

        // If the image exists in the db get the row
        if ($get_image->num_rows() == 1) {
            
            // Get image row
            $img = $get_image->row();
            
        // If the image doesn't exist in the db then set the alias to 'no-image'
        } elseif ($get_image->num_rows() == 0 || !file_exists($this->upload_prefix . $this->upload_dir . $img->file_location)) {
            
            // We may need to update the database again further down so to keep things simple
            // Just keep using the alias variable
            $alias = $this->default_alias;
            
            $get_n_image = $this->db->select('id, alias, file_location, original_height, original_width, resized_versions')->
                from('images')->where('alias', $alias)->get();
            $img = $get_n_image->row();
        }

        // If the height / width isn't sepcified or the height = the original uploaded height
        // and the width = the original width then just return the original uploaded image
        if (($height == '' && $width == '') || ($height == $img->original_height && $width ==
            $img->original_width)) {
              
            // return file location
            $result = base_url($this->upload_dir . $img->file_location);
            
        // Else if the image height or width isn't the same as the original then we need to
        // get it from the database or manipulate and upload then add to the database...
        } else {
            
            // Get the resized array from the database
            $resized_images = (array )json_decode($img->resized_versions);
            
            // Check to see if the image size is alredy listed int the array
            if (isset($resized_images[$width . '_' . $height])) {
                
                // Perfect, its already been manipulated so we'll return the location
                $result = base_url($this->upload_dir . $resized_images[$width . '_' . $height]->
                    file_location);
                    
            } else {

                // get the extension of the image from the original file
                $img_ext = explode('.', $img->file_location);

                // get the new randomly generated name for the resized image
                $new_image_rand = random_string('alnum', 16) . '.' . $img_ext[1];

                // Set the image configuration for the manipulation class            
                $config['dynamic_output'] = false;
                $config['quality'] = 100;
                $config['new_image'] = set_realpath($this->upload_dir) . $new_image_rand;
                $config['source_image'] = set_realpath($this->upload_dir) . $img->file_location;
                $config['height'] = $height;
                $config['width'] = $width;
                $config['maintain_ratio'] = false;
                $config['master_dim'] = 'auto';

                // load the config into the image manipulation library
                $this->load->library('image_lib', $config);
                
                // Set the new array to upload to the database
                $resized_array = array($width . '_' . $height => array('width' => $width,
                    'height' => $height, 'file_location' => $new_image_rand));

                // run the resize script and get the result
                if (!$this->image_lib->resize()) {
                    
                    // if there are errors print them out for testing purposes
                    print $this->image_lib->display_errors();
                    
                } else {
                    
                    // if its uploaded and manipulated okay then we add it into the database
                    if (file_exists($config['new_image'])) {
                        
                        // Update the database
                        $this->db->where('alias', $alias)->update('images', array('resized_versions' =>
                            json_encode(array_merge($resized_images, $resized_array))));

                        # NEW POSITION
                        $this->image_lib->clear();
                        unset($config);

                        # Maybe best to only have one return value
                        $result = base_url('uploads/images/' . $new_image_rand);
                        # return base_url('uploads/images/' . $new_image_rand);
                        
                    } else {
                        
                        // If its not created the file, scream and print an error message for testing purposes
                        print 'Error in image manipulation';
                    }

                }
            }
        }

    }
} // removed the last two functions, not enough characters remaining, nothing changed...
#5

[eluser]Bondie[/eluser]
Other information, I'm using the 2.1.0 version of CI, I'm just in the process of updating Image_lib to the latest version on Github, sorry for bump, not enough space on the last post to add.
#6

[eluser]John_Betong_002[/eluser]
It could be a memory problem. What size are your images. Maybe check for the available memory.

Have you checked phpinfo() to see the name of the GD Library?

Vague memories that I had a similar problem a long time ago and after creating the thumb-nail I put a sleep()/delay() for about a tenth of a second and it seemed to cure the problem.

If you send the image links I will try and create thumbnails using your code.
&nbsp;
Time for my beauty sleep...
&nbsp;
&nbsp;
#7

[eluser]Bondie[/eluser]
http://everythingswindon.co.uk/uploads/images/

The images are the sensibly named ones, any generated are the random strings.

Quote:GD Support enabled
GD Version bundled (2.0.34 compatible)
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 6b
PNG Support enabled
libPNG Version 1.2.10
WBMP Support enabled
XPM Support enabled
XBM Support enabled

Profiler shows:
Quote:MEMORY USAGE
3,152,360 bytes

Update:
Tried increasing memory_limit and upload_size (or whatever the variable is) still the same, tried using sleep() even setting it to 30 seconds still getting the same problems.

Update 2:
Updated to the latest version of image_lib on github(or whatever that site is) and now I'm only getting my custom error messages...
Quote:Error in image manipulation
which suggests its not uploading the image for some reason or other.

Update 3:
From the log...
Quote:ERROR - 2012-01-22 18:28:23 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at /home/jamiebon/es/application/models/mimage.php:110) /home/jamiebon/es/system/core/Output.php 391
ERROR - 2012-01-22 18:28:23 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at /home/jamiebon/es/application/models/mimage.php:110) /home/jamiebon/es/system/core/Output.php 391
ERROR - 2012-01-22 18:28:23 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at /home/jamiebon/es/application/models/mimage.php:110) /home/jamiebon/es/system/core/Output.php 391
#8

[eluser]John_Betong_002[/eluser]
Hi Bondie,

You will *NOT* be delighted to know that I downloaded your images and created a brand new controller to pick the images from the path and to create thumbnails. Unfortunately I had exactly the same problem of only creating a single thumbnail and also generating errors.

The path to the solution:
I reverted back to using a "MarcoMonteiro THUMB Helper" and it worked every time???

http://ellislab.com/forums/viewthread/204185/
&nbsp;

I spotted the difference between the helper and both your code and mine. The following missing line makes all the difference - don't you love computers Smile
Code:
$this->load->library('image_lib', $config);
  $this->image_lib->initialize( $config ); // MISSING LINE
&nbsp;
Try inserting the line and if it solves your problem then please create a Debug Report Error and hopefully mark this thread as [SOLVED]
&nbsp;
&nbsp;
#9

[eluser]Bondie[/eluser]
Spot on pal!

Thank you for the help, much appreciated!
#10

[eluser]CroNiX[/eluser]
Yes, there is a bug in the image library and it wont load with the config. You have to set (initialize) the config manually.




Theme © iAndrew 2016 - Forum software by © MyBB