CodeIgniter Forums
How to create multiple image copies with image resize(height and width) in single image upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to create multiple image copies with image resize(height and width) in single image upload (/showthread.php?tid=48926)



How to create multiple image copies with image resize(height and width) in single image upload - El Forum - 02-02-2012

[eluser]Was[/eluser]
Hello friend,

i want to create multiple image copies with image resize including height and width (in proportion) in a single image upload, can any one guide me how to do this in CI ?


Thanks,
Was



How to create multiple image copies with image resize(height and width) in single image upload - El Forum - 02-02-2012

[eluser]Bruno França[/eluser]
You can make auxiliar functions in your controller that initialize the image manipulation functions every time that it's called...
See this:
Code:
class MyClass extends CI_Controller
{
  /* Your code */

  /* Image watermark */
  function _img_watermark($imagem)
  {
   $config_wtm = array();
   $config_wtm['image_library']  = 'gd2';
   $config_wtm['source_image']  = $imagem;
   $config_wtm['wm_type']    = 'overlay';
   $config_wtm['wm_vrt_alignment'] = 'bottom';
   $config_wtm['wm_hor_alignment'] = 'right';
   $config_wtm['wm_hor_offset']  = 20;
   $config_wtm['wm_vrt_offset']  = 20;
   $config_wtm['wm_x_transp']   = 1;
   $config_wtm['wm_y_transp']   = 1;
   $config_wtm['wm_overlay_path']  = $this->config->item('arquivos_pasta') . $this->config->item('marcadagua');;
   $this->image_lib->initialize($config_wtm);
   if ( ! $this->image_lib->watermark())
   {
    return $this->_errors = $this->image_lib->display_errors('<div class="message error"><p>', '</p></div>');
   }
   $this->image_lib->clear();
   return TRUE;
  }

  /* Image Thumb */
  function _img_thumb($imagem)
  {
   $config_thumb['image_library']  = 'gd2';
   $config_thumb['source_image']  = $imagem;
   $config_thumb['maintain_ratio'] = true;
   $config_thumb['width']    = $this->config->item('thumb_largura');
   $config_thumb['height']   = $this->config->item('thumb_altura');
   $this->image_lib->initialize($config_thumb);
   if ( ! $this->image_lib->resize())
   {
    return $this->_errors = $this->image_lib->display_errors('<div class="message error"><p>', '</p></div>');
   }
   $this->image_lib->clear();
   return TRUE;
  }
}

And... In your controller action, you can use that functions many times as you want. For example:

Code:
/* After image upload */
if ($this->_img_resize($uploaded_image1)) log_message('debug', ' -- Redimensionada');
if ($this->_img_resize($uploaded_image2)) log_message('debug', ' -- Redimensionada');
if ($this->_img_watermark($uploaded_image1)) log_message('debug', ' -- Marca dagua inserida');

I hope this helps.



How to create multiple image copies with image resize(height and width) in single image upload - El Forum - 02-02-2012

[eluser]Was[/eluser]
Thanks Bruno for the reply,

got what i need,but is there any way to do without calling the function again and again means in one time call function ?

Was


How to create multiple image copies with image resize(height and width) in single image upload - El Forum - 02-02-2012

[eluser]Bruno França[/eluser]
If your image copies has the same size, you can use just a loop with the imagem library... you not need the functions above.

If your images has diferent sizes, I think that you need to initialize the image library always that you process an image... In this case, the use of the functions is more efficient.


How to create multiple image copies with image resize(height and width) in single image upload - El Forum - 02-02-2012

[eluser]Was[/eluser]
Thanks Bruno,

got it and i have all the different sizes to resize.

Was