Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Thumbnail Loop not resizing correctly for first picture
#1

[eluser]DaTran[/eluser]
I have an array that stores the sizes of thumbnails I want, it's able to resize correctly except for the first time.

Code:
function thumb_scan($source_path) {
      // create configs
      $config = array();
      foreach ($this->thumb_scan as $size) {
        
        $config[] = array(
          'maintain_ratio' => true,
              'master_dim' => 'width',
              'width' => $size,
              'source_image' => $source_path,
              'new_image' => $this->gallery_path . '/scan/thumbs/' . $size
          );
          
      }
      print_r($config);
    foreach ($config as $item) {
      $this->image_lib->clear();
      
      print_r($this->image_lib);
      
      $this->image_lib->initialize($item);
      if(!$this->image_lib->resize()) {
        return FALSE;
      }
    }
      return TRUE;
      
    }

The forum will only allow a limited amount of characters so I've only displayed the first 2 thumbnails As you can see before initializing it's clear and when the first thumbnail is applied the width is set but the height is set WRONG it's suppose to maintain the ratio. The next thumb for size 600 sets the height correctly. Whats up with that?

This is what is print_r();

Array
(
[0] => Array
(
[maintain_ratio] => 1
[master_dim] => width
[width] => 700
[source_image] => /Users/Alex/Sites/*/images/scan/ca5b1e4b070bf8ef21f18315d7550672.jpg
[new_image] => /Users/Alex/Sites/*/images/scan/thumbs/700
)

[1] => Array
(
[maintain_ratio] => 1
[master_dim] => width
[width] => 600
[source_image] => /Users/Alex/Sites/*/images/scan/ca5b1e4b070bf8ef21f18315d7550672.jpg
[new_image] => /Users/Alex/Sites/*4/images/scan/thumbs/600
)
CI_Image_lib Object
(
[image_library] => gd2
[library_path] =>
[dynamic_output] =>
[source_image] =>
[new_image] =>
[width] =>
[height] =>
[quality] =>
[create_thumb] =>
[thumb_marker] => _thumb
[maintain_ratio] => 1
[master_dim] => auto
[rotation_angle] =>
[x_axis] =>
[y_axis] =>
[wm_text] =>
[wm_type] => text
[wm_x_transp] => 4
[wm_y_transp] => 4
[wm_overlay_path] =>
[wm_font_path] =>
[wm_font_size] =>
[wm_vrt_alignment] =>
[wm_hor_alignment] =>
[wm_padding] =>
[wm_hor_offset] =>
[wm_vrt_offset] =>
[wm_font_color] =>
[wm_shadow_color] =>
[wm_shadow_distance] =>
[wm_opacity] =>
[source_folder] =>
[dest_folder] =>
[mime_type] =>
[orig_width] =>
[orig_height] =>
[image_type] =>
[size_str] =>
[full_src_path] =>
[full_dst_path] =>
[create_fnc] =>
[copy_fnc] =>
[error_msg] => Array
(
)

[wm_use_drop_shadow] =>
[wm_use_truetype] =>
)
CI_Image_lib Object
(
[image_library] => gd2
[library_path] =>
[dynamic_output] =>
[source_image] =>
[new_image] =>
[width] => 700
[height] => 3474
[quality] =>
[create_thumb] =>
[thumb_marker] =>
[maintain_ratio] => 1
[master_dim] => auto
[rotation_angle] =>
[x_axis] =>
[y_axis] =>
[wm_text] =>
[wm_type] => text
[wm_x_transp] => 4
[wm_y_transp] => 4
[wm_overlay_path] =>
[wm_font_path] =>
[wm_font_size] =>
[wm_vrt_alignment] =>
[wm_hor_alignment] =>
[wm_padding] =>
[wm_hor_offset] =>
[wm_vrt_offset] =>
[wm_font_color] =>
[wm_shadow_color] =>
[wm_shadow_distance] =>
[wm_opacity] =>
[source_folder] =>
[dest_folder] =>
[mime_type] => image/jpeg
[orig_width] =>
[orig_height] =>
[image_type] =>
[size_str] =>
[full_src_path] =>
[full_dst_path] =>
[create_fnc] =>
[copy_fnc] =>
[error_msg] => Array
(
)

[wm_use_drop_shadow] =>
[wm_use_truetype] =>
[dest_image] => ca5b1e4b070bf8ef21f18315d7550672.jpg
)
CI_Image_lib Object
(
[image_library] => gd2
[library_path] =>
[dynamic_output] =>
[source_image] =>
[new_image] =>
[width] => 600
[height] => 391
[quality] =>
[create_thumb] =>
[thumb_marker] =>
[maintain_ratio] => 1
[master_dim] => auto
[rotation_angle] =>
[x_axis] =>
[y_axis] =>
[wm_text] =>
[wm_type] => text
[wm_x_transp] => 4
[wm_y_transp] => 4
[wm_overlay_path] =>
[wm_font_path] =>
[wm_font_size] =>
[wm_vrt_alignment] =>
[wm_hor_alignment] =>
[wm_padding] =>
[wm_hor_offset] =>
[wm_vrt_offset] =>
[wm_font_color] =>
[wm_shadow_color] =>
[wm_shadow_distance] =>
[wm_opacity] =>
[source_folder] =>
[dest_folder] =>
[mime_type] => image/jpeg
[orig_width] =>
[orig_height] =>
[image_type] =>
[size_str] =>
[full_src_path] =>
[full_dst_path] =>
[create_fnc] =>
[copy_fnc] =>
[error_msg] => Array
(
)

[wm_use_drop_shadow] =>
[wm_use_truetype] =>
[dest_image] => ca5b1e4b070bf8ef21f18315d7550672.jpg
)
#2

[eluser]markup2go[/eluser]
Would moving $this->image_lib->clear(); to before the closing foreach's } make a difference? What about using TRUE instead of 1 for the maintain_ratio preference? What does $this->image_lib->display_errors(); show?
#3

[eluser]markup2go[/eluser]
Pulled this out of the core image library:

Code:
/*
         * Should we maintain image proportions?
         *
         * When creating thumbs or copies, the target width/height
         * might not be in correct proportion with the source
         * image's width/height.  We'll recalculate it here.
         *
         */
        if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
        {
            $this->image_reproportion();
        }
1 !== TRUE
#4

[eluser]DaTran[/eluser]
$this->image_lib->display_errors();

doesn't display an error because the thumbnails get created but the 700 thumbs don't get resized correctly. In this example it's resized to:
[width] => 700
[height] => 3474

1 is TRUE;

If you look at the code I set it as true and capitalizing doesn't make a difference I just updated that:
$config[] = array(
'maintain_ratio' => TRUE,
'master_dim' => 'width',
'width' => $size,
'source_image' => $source_path,
'new_image' => $this->gallery_path . '/scan/thumbs/' . $size
);
#5

[eluser]markup2go[/eluser]
1 may equal true but it's not identical to true...

Anyways same output?
#6

[eluser]DaTran[/eluser]
When you print_r(); it displays as 1 because you can't echo a boolean so it print a 1 or 0 for us humans.

Anyways, I found the solution.

I looked at the source code and found the clear() function and found that it doesn't clear the height or width fo the image_lib so I assuming in order for the maintain_ratio function to work you need to first have the original size. So all I did was add an extra value to the config array so that it includes the default dimensions of the original image file.

$config[] = array( 'source_image' => $source_path);


Code:
function thumb_scan($source_path) {
      // create configs
      $config = array();
      $config[] = array(
        'source_image' => $source_path
      );
      foreach ($this->thumb_scan as $size) {
        
        $config[] = array(
          'maintain_ratio' => TRUE,
              'master_dim' => 'width',
              'width' => $size,
              'source_image' => $source_path,
              'new_image' => $this->gallery_path . '/scan/thumbs/' . $size
          );
          
      }
      print_r($config);
    foreach ($config as $item) {
      $this->image_lib->clear();
      
      print_r($this->image_lib);
      $this->image_lib->initialize($item);
      echo $this->image_lib->display_errors();
      if(!$this->image_lib->resize()) {
        return FALSE;
      }
    }
      return TRUE;
      
    }
#7

[eluser]DaTran[/eluser]
Thanks btw for reminding me that there is a core code that I can look at haha. It helped alot.
#8

[eluser]markup2go[/eluser]
Whoops you are correct about the print_r, sorry. Glad you have it functional now that my head is spinning Tongue




Theme © iAndrew 2016 - Forum software by © MyBB