HomePage Forums Traveler Questions about the weight of the photos to download

This topic contains 1 reply, has 2 voices, and was last updated by  Bryan 3 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9849

    In the partner part, when registering a hotel, it is request to download the photos for the gallery. Is there a way to limit the size of images to download. We risk having images of 2.5Mo and more
    I would like to limit the size to 350ko automatically for example

    #9899

    Hello

    You can add this code to functions.php file of theme to limit the file size upload:

    add_filter( 'wp_handle_upload_prefilter', '_limit_image_size' );
    
        function _limit_image_size( $file )
        {
         $current_user = wp_get_current_user();
      $level = $current_user->roles;
      $level = array_shift($level);
            if ( $level == 'partner' ) {
                $size = (float)$file['size'];
                $size = $size / 1024;
                $type = $file['type'];
                $is_image = strpos( $type, 'image' );
                $limit = 150;
    
                if ( ( $size > $limit ) && ( $is_image !== false ) ) {
                    $file['error'] = 'Image files must be smaller than ' . $limit . 'KB';
                }
            }
    
            return $file;
        }
    
        function tm_change_upload_size( $size )
        {
         $current_user = wp_get_current_user();
      $level = $current_user->roles;
      $level = array_shift($level);
            if ( $level == 'partner' ) {
                return 1024 * 150;
            }
    
            return 1000 * 2048;
        }
    
        add_filter( 'upload_size_limit', 'tm_change_upload_size' );

    Change this $limit = 150 to the size you want (in KB)

    Thank you.

Viewing 2 posts - 1 through 2 (of 2 total)

The forum ‘Traveler’ is closed to new topics and replies.