1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Profile Image 1.0

Profile Image

  1. sachingk

    sachingk Well-Known Member

    I am no more in this project development since this project was pause many months ago. Now I see the community is still working around it & also new releases are happening. Don't know where this open source project moving ahead. I am thinking to come be back & contribute , but finding a strong reason for it.

    @valMETNG - This is a open source project and you are free to edit the source code & point out the bugs. This plugin was developed for my website long ago, but the website is no more alive.
     
    Last edited: Jul 6, 2013
  2. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    yes, the project is still going ahead and we are appreciating those who make plugins and themes
    we are updating the core and core plugins as well
     
  3. Graeme

    Graeme Member

    Hey I'm wondering if you have the same problem I have...Everything is working great, unless you delete a picture...when I press delete, the avatars elsewhere on my page (such as in the menu bar at the top, and the user navigation) don't update correctly. Instead of getting the default avatar image, it the avatars just go blank..then after deleting, if you update with a new picture, the avatar doesn't update correctly again...but if you then upload again, everything works fine..

    So this error is issue is only seen if you delete the profile image..does anyone get the same problem?
     
  4. valMETNG

    valMETNG Administrator Staff Member Admin

    I'm not seeing that problem. I just tried again to confirm: when I deleted the picture, the default gravatar one came up. (The header one didn't switch immediately, as you indicate, but after I did a page refresh, it did.) Then I reloaded the picture and it appeared normally.
     
  5. Graeme

    Graeme Member

    cool, if you want the header one to change, I just stuck this in user_navigation.php:


    PHP:
            <?php
                
    if($h->cage->post->keyExists('submit')){
                
    ?>
                
                <script>
             setTimeout(function(){
                 var source= "<?php echo BASEURL "content/images/profile_images/"$h->currentUser->name?>";
                 $("#userProfilePixBox img.avatar").attr("src", source);
                 $(".nav img.avatar").attr("src", source);
                },10)
                </script>
                
                <?php }
    it's just a bit of jquery to replace a couple of the avatars that weren't changing for me

    ..I also wrapped a bit in gravatar.php - this could be useful for you...it's just a check to see if an image has just been deleted..if it has been deleted, there is a post key 'delete' ...so if that exists, there's no point checking if any file exists in the images directory:

    PHP:
    $grav_url$default_image;

      if(!(
    $h->cage->post->keyExists('delete'))){
    $image_dir "content/images/profile_images/";        

    if (
    file_exists($image_dir."".$user_name.".jpg")) 
             
    $grav_url BASEURL."".$image_dir."".$user_name.".jpg" ;
                
    else if (
    file_exists($image_dir."".$user_name.".png")) 
             
    $grav_url BASEURL."".$image_dir."".$user_name.".png" ;
             
    else if (
    file_exists($image_dir."".$user_name.".jpeg"))  
             
    $grav_url BASEURL."".$image_dir."".$user_name.".jpeg" ;
             
    else if (
    file_exists($image_dir."".$user_name.".gif"))  
             
    $grav_url BASEURL."".$image_dir."".$user_name.".gif" ;
      }else
             
    $grav_url $default_image;
      
            return 
    $grav_url;
        }
     
    valMETNG likes this.
  6. valMETNG

    valMETNG Administrator Staff Member Admin

    Based on Alan's excellent suggestion, here's some code to compress images.

    Background: In order to improve page performance, we want to download images that are as small as necessary. For example, if a user uploads a picture that is 250K, when a page is loaded with that user's picture, it has to download a 250K file. However, the user's picture is usually only displayed at a much smaller width/height (e.g., 16x16 or 32x32). A 250K image compressed to these typical avatar sizes is usually less than 3K. So if you only need to display a picture that's less than 3K, there's no reason to download a 250K file. That should make your page speed somewhat faster (although the results aren't always amazing).

    As I've created this code specifically for my customized build, this is really only to provide direction. (You won't be able to add this directly to your build.)

    modified profile_images/templates/upload.php:
    Code:
    <?php
    
    $url = 'content/images/user/' . $h->currentUser->id . '/';
    
    //define a maxim size for the uploaded images in Kb
    define ("MAX_SIZE","100");
    //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
    function getExtension($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }
    
    
    function deletephoto($h, $ursname)
    {
       $url = 'content/images/user/' . $h->currentUser->id . '/';
       $h->deleteFile($ursname, 'image', $url, true);
    }
    //This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
    $errors=0;
    //checks if the form has been submitted
    
    if($h->cage->post->keyExists('delete'))
    {
    deletephoto($h, $h->currentUser->name);
    }
    
    
    
    
    if ($h->cage->post->keyExists('submit')) {
    
    //reads the name of the file the user submitted for uploading
    
    $image= basename($h->cage->files->sanitizeTags('/file/name'));
    
    
    //if it is not empty
    if ($image)
    {
    
    //get the original name of the file from the clients machine
    $filename = stripslashes(basename($h->cage->files->sanitizeTags('/file/name')));
    
    //get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    //if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
    {
    //print error message
    $h->messages[$h->lang('users_upload_bad_extension')] = 'red';
    $errors=1;
    }
    else
    {
    //get the size of the image in bytes
    //$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
    $size=filesize($h->cage->files->getRaw('/file/tmp_name'));
    //compare the size with the maxim size we defined and print error if bigger
    if ($size > MAX_SIZE*1024)
    {
    $h->messages[$h->lang('users_upload_too_big')] = 'red';
    $errors=1;
    return false;
    }
    //we will give an unique name, for example the time in unix time format
    $image_name=$h->currentUser->name.'.'.$extension;
    //the new name will be containing the full path where will be stored (images folder)
    
    
    deletephoto($h, $h->currentUser->name);
    /*
    if (file_exists($url.''.$h->currentUser->name.".jpg"))  unlink($url.''.$h->currentUser->name.".jpg");
    if (file_exists($url.''.$h->currentUser->name.".png"))  unlink($url.''.$h->currentUser->name.".png");
    if (file_exists($url.''.$h->currentUser->name.".jpeg"))  unlink($url.''.$h->currentUser->name.".jpeg");
    if (file_exists($url.''.$h->currentUser->name.".gif"))  unlink($url.''.$h->currentUser->name.".gif");*/
    
    
    $newname= $url.''.$image_name;
    
    //we verify if the image has been uploaded, and print error instead
    $copied = copy($h->cage->files->getRaw('/file/tmp_name'), $newname);
    if (!$copied)
    {
    $h->messages[$h->lang('users_upload_error')] = 'red';
    $errors=1;
    return false;
    }
    // create 16x16 and 32x32 images as well to speed page loads
    $h->compressImage($extension, $url . $h->currentUser->name);
    }}}
    
    
    //If no errors registred, print the success message
    if($h->cage->post->keyExists('submit') && !$errors)
    {
    $h->messages[$h->lang('users_upload_success')] = 'green';
    }
    
    ?>
    
    <?php  //Show avatars if enabled (requires an avatars plugin)
    if($h->isActive('avatar')) {
    $h->setAvatar($h->currentUser->id, 80);
    echo $h->wrapAvatar();
    }
    ?>
    
    <form method="post" enctype="multipart/form-data" action="<?php $h->url(array('page'=>'upload'))  ?>">
    <table>
    <tr><td><input type="file" name="file"></td></tr>
    <tr><td>
    <p></p>
    <button name="submit" type="submit" class="btn btn-primary" value="<?php echo $h->lang('users_upload_image'); ?>" ><?php echo $h->lang('users_upload_image'); ?></button>
    <button name="delete" type="submit" class="btn btn-danger" value="<?php echo $h->lang('users_delete_image'); ?>" ><?php echo $h->lang('users_delete_image'); ?></button>
    </td></tr>
    </table>
    </form>
    
    Three newly created function...

    This first one does the actual compression and creates files with different dimensions. I default to creating two image files, 16x16 and 32x32, because I use the 16x6 for the activity feed and the 32x32 for the post title. However, if you send in a particular width, it will create it in that dimension. It can either use the file that the user just uploaded or it can take in an existing file (more on this later):
    Code:
      public function compressImage($h, $extension, $file_name, $requested_width = false, $uploaded_file = false)
       {
         if (!$uploaded_file) { $uploaded_file = $h->cage->files->getRaw('/file/tmp_name'); }
    
         switch ($extension)
         {
           case "jpg":
           case "jpeg":
             $src = imagecreatefromjpeg($uploaded_file);
             break;
           case "png":
             $src = imagecreatefrompng($uploaded_file);
             break;
           default:
             $src = imagecreatefromgif($uploaded_file);
             break;
         }
    
         list($width, $height) = getimagesize($uploaded_file);
    
         // if width doesn't come in, we will create multiple images with different dimensions (always serving the lowest size will increase page speed)
         if (!$requested_width) { $images = array(16, 32); }
         else { $images[$requested_width] = $requested_width; }
    
         foreach ($images as $image_width)
         {
           $new_height = ($height / $width) * $image_width;
           $temp_image[$image_width] = imagecreatetruecolor($image_width, $new_height);
    
           // png image files with transparent background will turn black if we don't do this
           if ($extension == 'png')
           {
             // https://stackoverflow.com/questions/5688954/how-to-replace-black-background-with-white-when-resizing-converting-png-images-w
             $kek = imagecolorallocate($temp_image[$image_width], 255, 255, 255);
             imagefill($temp_image[$image_width], 0, 0, $kek);
           }
    
           imagecopyresampled($temp_image[$image_width], $src, 0, 0, 0, 0, $image_width, $new_height, $width, $height);
    
           imagejpeg($temp_image[$image_width], $file_name . '_' . $image_width . '.jpg', 100);
    
           imagedestroy($temp_image[$image_width]);
         }
    
         imagedestroy($src);
       }
    
    Before every upload, I delete any existing file:
    Code:
      function deleteFile($h, $file, $file_type = '', $url = 'content/communities/', $wildcard = false)
       {
         $valid_images = array('jpg', 'jpeg', 'png', 'gif');
         $partial_file_name = $url . $file;
         $deleted = false;
    
         if ($file_type == 'image')
         {
           foreach ($valid_images as $extension)
           {
             if (!$wildcard && file_exists($partial_file_name . "." . $extension))
             {
               unlink($partial_file_name . "." . $extension);
               $deleted = true;
             }
             elseif ($wildcard && file_exists($partial_file_name . "." . $extension))
             {
               unlink($partial_file_name . "." . $extension);
    
               // from http://us3.php.net/manual/en/function.unlink.php#109971
               array_map('unlink', glob($partial_file_name . "*.jpg"));
               $deleted = true;
             }
           }
         }
         elseif (file_exists($partial_file_name . $file_type))
         {
           unlink($partial_file_name . $file_type);
           $deleted = true;
         }
    
         return $deleted;
       }
    
    If you already have existing users and want to add this functionality, you need an easy way to grab their existing profile image and create the additional thumbnails:
    Code:
      public function addResizedImages($h)
       {
         $sql = "SELECT user_id, user_username FROM " . TABLE_USERS;
         $users = $h->db->get_results($h->db->prepare($sql));
    
         $valid_images = array('jpg', 'jpeg', 'png', 'gif');
         $url = BASE . 'content/images/user/';
    
         foreach ($users as $user)
         {
           foreach ($valid_images as $extension)
           {
             $file_name = $user->user_id . '/' . $user->user_username;
             $check = $url . $file_name . "." . $extension;
             echo '<br />Checking ' . $check;
    
             if (file_exists($check))
             {
               echo '<br />-->File exists';
               $h->compressImage($extension, $url . $file_name, false, $check);
               echo '<br />-->Created compressed images at ' . $check;
             }
           }
         }
    
         exit;
       }
    
    Hope that all isn't too confusing. Don't hesitate to ask if you have questions about integrating into your build.
     
    shibuya246 likes this.
  7. aliali

    aliali New Member

    Attached Files:

  8. valMETNG

    valMETNG Administrator Staff Member Admin

    Is the error that it's showing the same profile image for different users? If so, I've never seen this problem before - have you modified any code? If not, you might want to first try uninstalling the plugin and then reinstalling.
     
    shibuya246 likes this.
  9. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    valMETNG likes this.
  10. robin007

    robin007 Well-Known Member

    when i upload my profile image on ipadrank

    Parse error: syntax error, unexpected '.', expecting ',' or ';' in/home/ipadrank/public_html/content/plugins/profile_image/libs/profile_image_functions.php on line33
     
  11. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    i see it. will check
     
  12. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    fixed on ipadrank. will release new version of plugin shortly.
    need to change the browser cache on the image name first as it still reads the old image unless a browser refresh is done
    will check this and then release new version
     
  13. robin007

    robin007 Well-Known Member

    I upload a image.
    it show success message.

    after upload image. It show old image.
     

    Attached Files:

  14. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    as i said above that is a browser caching problem and needs to be addressed also
    for the moment to test on ipadrank, click F5 or browser refresh and see your image change
    it is saved on the server but the name is the same as before and therefore the browser does not pull in the new image until you request the refresh
     
  15. robin007

    robin007 Well-Known Member

    I am many time try it but not work.
     
  16. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    is that on ipadrank ?

    what was your file type?
     
  17. robin007

    robin007 Well-Known Member

    yes.

    jpg.
     
  18. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    i made an edit to the plugin
    please check again now on ipdarank

    thanks for finding this
     
    robin007 likes this.
  19. robin007

    robin007 Well-Known Member

    upload done.


    But that time profile page sidebar gone.
     

    Attached Files:

    Last edited: Jan 6, 2015
  20. shibuya246

    shibuya246 Hotaru Developer Staff Member Admin

    it only does that just on the final step of uploading avatar
     

Share This Page