ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Howto: resize avatars/profile photos during upload

February 10, 2012 5:54am

Subscribe [2]
  • #1 / Feb 10, 2012 5:54am

    Yuri

    315 posts

    Originally posted on my site

    If you are like me, you’ve always wanted to have members avatars and photos resized automatically upon upload, without asking people to do that manually.

    ExpressionEngine 2.4 and prior do not resize member’s photos and avatars “out of the box”. You have to ask people upload files of certain dimension and them probably use some plugin to resize the image in the template (which leads to unnecessary server load).

    Fortunately, EE 2 offers complete integration of CodeIgniter libraries, and so you can add automatic resize for profile photos and avatars with just few lines of code.
    Unfortunately, you need to make changes to one of core files (but that’s just one file and few lines, so you can easily keep that changes).

    So, open /system/expressionengine/libraries/Member.php in your code editor.

    In the end of upload_member_images function (around line 378) find the lines that say

    $config['max_width']  = $max_width;
    $config['max_height']  = $max_height;

    and comment them out

    //$config['max_width']  = $max_width;
    //$config['max_height']  = $max_height;

    Then, find these lines below:

    // Do we need to resize?
    $width = $file_info['image_width'];
    $height = $file_info['image_height'];

    and BELOW them add

    //hack start
      if ($width > $max_width || $height > $max_height)
      {
       $config = array(
        'library_path'  => $this->EE->config->item('image_library_path'),
        'image_library'  => $this->EE->config->item('image_resize_protocol'),
        'source_image'  => $upload_path.$new_filename,
        'height'   => $max_height,
        'width'    => $max_width,
        'maintain_ratio' => TRUE
       );
     
       $this->EE->load->library('image_lib', $config);
       $this->EE->image_lib->resize();
       
       $image_data = $this->EE->image_lib->get_image_properties($upload_path.$new_filename, TRUE);
       $width = $image_data['width'];
       $height = $image_data['height'];
      }
      //hack end

    That’s all. Now all avatars, photos (and signatures) that people upload will be automatically resized to the dimensions that you specified as max width and max height in EE membership preferences.

  • #2 / Feb 10, 2012 7:25am

    Bhashkar Yadav

    727 posts

    thanks for sharing it 😊

  • #3 / Mar 05, 2012 11:22am

    Jim F

    53 posts

    Thanks for sharing. This is a real lifesaver. I’m working profile photos into a site redesign and was just running up against this problem.

    This really should be part of core functionality.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases