Welcome Guest, Not a member yet? Register   Sign In
Preventing resize of smaller images
#1

[eluser]Unknown[/eluser]
Hi, I`m using Image_lib to upload resize images, but now I stopped and don`t know what to do .

I`m using these variables:
Code:
$config['width'] = 800;
$config['height'] = 800;

And the problem is, that if I try to upload an image 200x200 or other size, it will be expanded to 800px. How to allow resizing only to bigger images?

Thanks in advance Smile
#2

[eluser]ok3x[/eluser]
well, i use this snippet:
Code:
$data = $this->upload->data();

$doResize = ($data['image_width'] < MIN_WIDTH || $data['image_height'] < MIN_HEIGHT) ? false : true;

if ($doResize && $this->image_lib->resize())
{
  // image uloaded and resized
}

if you're using file uploading class to upload your images, you can also (after succesfull upload) utilize it's method "data()", which gives you upload data (see user guide). this is an array, where you can find some usefull info (see user guide again). then you can run this one-line code $doResize.. to determine whether to resize your image or not. if FALSE, the resizing won't happen at all: because of the first condition argument, the second function may not be initialized.
#3

[eluser]OwanH[/eluser]
The snippet posted by ok3x will prevent images with both dimensions less than 800x800 from being resized but it will also prevent images with only one dimension less than the target resized dimension from being resized as well. What you need is to be able to resize images that are, for example, 1000x760, 600x900, and so on. Here's a snippet of code from an app I built previously that had this exact check in place. Note that this code assumes it is being executed within a controller and I'm using your config['width'] and config['height'] variables:

Code:
... (File uploaded and Image manipulation library loaded prior to the below snippet)

$data = $this->upload->data();
$target_width = $this->config->item('width');
$target_height = $this->config->item('height');

// File uploaded and validated: resize and optimize.
if ($data['image_width'] > $target_width || $data['image_height'] > $target_height)
{
  // Uploaded image has at least one dimension that exceeds the target dimensions.
  if ($data['image_width'] > $target_width && $data['image_height'] > $target_height)
    $master_dim = 'auto';
  elseif ($data['image_width'] > $target_width)
    $master_dim = 'width';
  else
    $master_dim = 'height';

  $config = array('source_image' => $data['full_path'], 'maintain_ratio' => TRUE,
                  'width' => $target_width, 'height' => $target_height,
                  'master_dim' => $master_dim);

  $this->image_lib->clear();                    // reset image lib properties
  $this->image_lib->initialize($config);        // initialize image lib preferences
                        
  if ($this->image_lib->resize())
  {
    // Image resized
    ...
  }
  else {
    // Image resize failed
    ...
  }
}

Note that the master_dim preference (see the CodeIgniter Image Manipulation Library doc. for a list of all available preferences) is being manually set to one of the three available values. If the image uploaded has both dimensions exceeding the target dimensions then it is set to 'auto', the library's default value. If however only one dimension (either the width or height) exceeds the target dimensions then I'm telling the library to use the x-axis as the master axis if the image's width exceeds the target dim. but to use the y-axis as the master axis if the image's height exceeds the target dim.

Hope this helps. All the best with your CI coding Smile
#4

[eluser]Michael Wales[/eluser]
I usually find that you don't actually need to define your resize by both height and width. Usually you simply want to say, "I don't want this any wider than 800px" or "No taller than 400px"

If this is the case, just define that one "must have" size and the image library will take care of the rest. The following example assumes the image was uploaded with the File Upload class:

Code:
$uploaded_img = $this->upload->data();

$config['source_image'] = $uploaded_img['full_path'];
$config['width'] = 800;
$this->load->library('image_lib', $config);

if ($uploaded_img['image_width'] > $config['width']) {
  $this->image_lib->resize();
}

I don't have an environment to test in - as I said, I believe this would work. If not, it's really simple to dynamically generate the height (just a basic proportion from elementary school math):
Code:
$config['height'] = ($uploaded_img['image_height'] * $config['width']) / $uploaded_img['image_width'];




Theme © iAndrew 2016 - Forum software by © MyBB