protected function preparePagination(&$offset, &$limit): ?string
    {
        $total   = count($this->images);
        $perPage = $this->get('perPage');

        // Paginate the result of not randomly sorted (see #8033)
        if ($perPage > 0 && $this->get('sortBy') != 'random') {
            // Get the current page
            $parameter = 'page_g' . $this->get('id');
            $page      = (Input::get($parameter) !== null) ? Input::get($parameter) : 1;

            // Do not index or cache the page if the page number is outside the range
            if ($page < 1 || $page > max(ceil($total / $perPage), 1)) {
                throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
            }

            // Set limit and offset
            $offset = (($page - 1) * $perPage);
            $limit  = min(($perPage + $offset), $total);

            $pagination = new Pagination(
                $total,
                $perPage,
                Config::get('maxPaginationLinks'),
                $parameter
            );

            return $pagination->generate("\n  ");
        }

        return null;
    }