$uid = $request_user->ID;
  
    $cropped_full = USER_AVATAR_UPLOAD_PATH.“{$uid}/”.time().“-bpfull.jpg”;
    $cropped_thumb = USER_AVATAR_UPLOAD_PATH.“{$uid}/”.time().“-bpthumb.jpg”;
    
    // Create image from file
        switch(strtolower($_FILES[‘file’][‘type’]))
        {
            case ‘image/jpeg’:
                $fn = $_FILES[‘file’][‘tmp_name’];
                $height_width = $this->get_image_ratio_height_width($fn);
                
                $width = $height_width[‘width’];
                $height = $height_width[‘height’];
                $size = $height_width[‘size’];
                $src = imagecreatefromstring(file_get_contents($fn));
                $dst = imagecreatetruecolor($width,$height);
                imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
                imagedestroy($src);
                
                imagejpeg($dst,$cropped_full); // adjust format as needed
                imagedestroy($dst);
                $url=user_avatar_fetch_avatar( array( ‘html’=>false, ‘item_id’ => $uid ) );
        return $this->validation->response(“sucess”, $data[‘avatar_url’] = $url, 200);
                
                break;
            case ‘image/png’:
                $fn = $_FILES[‘file’][‘tmp_name’];
                $height_width = $this->get_image_ratio_height_width($fn);
                
                $width = $height_width[‘width’];
                $height = $height_width[‘height’];
                $size = $height_width[‘size’];
                
                $src = imagecreatefromstring(file_get_contents($fn));
                $dst = imagecreatetruecolor($width,$height);
                imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
                imagedestroy($src);
                
                imagepng($dst,$cropped_full); // adjust format as needed
                imagedestroy($dst);
                
                $url=user_avatar_fetch_avatar( array( ‘html’=>false, ‘item_id’ => $uid ) );
        return $this->validation->response(“sucess”, $data[‘avatar_url’] = $url, 200);
        
                break;
            case ‘image/gif’:
                $fn = $_FILES[‘file’][‘tmp_name’];
                $height_width = $this->get_image_ratio_height_width($fn);
                
                $width = $height_width[‘width’];
                $height = $height_width[‘height’];
                $size = $height_width[‘size’];
                
                $src = imagecreatefromstring(file_get_contents($fn));
                $dst = imagecreatetruecolor($width,$height);
                imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
                imagedestroy($src);
                // delete the previous files
             user_avatar_delete_files($uid);
                
                imagegif($dst,$cropped_full); // adjust format as needed
                imagedestroy($dst);
                
                $url=user_avatar_fetch_avatar( array( ‘html’=>false, ‘item_id’ => $uid ) );
        return $this->validation->response(“sucess”, $data[‘avatar_url’] = $url, 200);
                
                break;
            default:
                $msg = ‘Unsupported file type: ‘.$_FILES[‘file’][‘type’];
                return $this->validation->response($msg, null, 400);
        }
private function get_image_ratio_height_width($fn) {
        $size = getimagesize($fn);
        $ratio = $size[0]/$size[1]; // width/height
        if( $ratio > 1) {
            $width = 200;
            $height = 200/$ratio;
        }
        else {
            $width = 200*$ratio;
            $height = 200;
        }
        
        return array(‘height’=>$height, ‘width’=>$width, ‘size’=>$size);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *