Welcome Guest, Not a member yet? Register   Sign In
The problem with image_lib
#11

[eluser]dmorin[/eluser]
Why the bump? If you're having a similar issue, post it or start a new thread. My code should have solved the original question and codeDeveloper's last post is virtually unintelligible so I'm not sure what else you're looking for.
#12

[eluser]BaRzO[/eluser]
i am having same problem your code ofcorse solving the problem but if image_lib can handle this would be more better i think thats way i am bumping the topic
thanks for your solution
#13

[eluser]Chicken's Egg[/eluser]
I am sorry for bumping this threat again, but I thought that it might be a good idea to publish my solution in here.

The problems you discovered are caused by a function in the Image_lib called image_reproportion(). The Image Library in CI resizes images as you would expect. If the image is to big, it gets scaled down and if it is to small, it gets expanded.

If you don't want your images to be enlarged, you might consider the following extension to the library. Basically I just had to add step 1 to the function in order to get the behaviour I wanted. But be careful: by adding this extension as is, enlarging images is not possible anymore. If you want to have that possibility too, you will have to add a new configuration variable and create an if-statement around step 1 in the code below. That is the easiest solution as the method image_reproportion() is called by the method initialize() in the library.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_image_lib extends CI_Image_lib
{
    /**
     * Re-proportion Image Width/Height
     *
     * When creating thumbs, the desired width/height
     * can end up warping the image due to an incorrect
     * ratio between the full-sized image and the thumb.
     *
     * This function lets us re-proportion the width/height
     * if users choose to maintain the aspect ratio when resizing.
     *
     * @access  public
     * @return  void
     */

  function image_reproportion()
  {
    if(!is_numeric($this->width) OR !is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
    {
      return;
    }

    if(!is_numeric($this->orig_width) OR !is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
    {
      return;
    }

    // STEP 1: Are new measures needed?
    if($this->orig_width <= $this->width && $this->orig_height <= $this->height)
    { // Image is smaller

      $this->width  = $this->orig_width;
      $this->height = $this->orig_height;
    }

    // STEP 2: Calculate new measurements
    // &lt;!-- Original code from here --&gt;

    $new_width  = ceil($this->orig_width*$this->height/$this->orig_height);
    $new_height = ceil($this->width*$this->orig_height/$this->orig_width);

    $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));

    if ($this->master_dim != 'width' AND $this->master_dim != 'height')
    {
        $this->master_dim = ($ratio < 0) ? 'width' : 'height';
    }

    if (($this->width != $new_width) AND ($this->height != $new_height))
    {
      if ($this->master_dim == 'height')
      {
        $this->width = $new_width;
      }
      else
      {
        $this->height = $new_height;
      }
    }
  }
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB