Philippians 1:18

What then? notwithstanding, every way, whether in pretence, or in truth, Christ is preached; and I there in do rejoice, yea, and will rejoice.

Paul was in prison while believers were facing persecution, and his rejoicing belongs to that setting of bonds and reviling. That very imprisonment was turned to help the spread of the gospel and the winning of people to Christ. Yet amid that advance, some were preaching Christ from envy and rivalry, hoping by their contentious proclaiming to add affliction to his imprisonment and make it more painful to bear.

He rejoiced because Christ was preached and the gospel was advancing in spite of imprisonment, persecution, and the impure motives of some preachers. His joy did not come from freedom or earthly comfort but from Christ's presence with him and from seeing the gospel go forward. For that reason it could be experienced even in suffering, and it would continue onward. So opposition to the gospel was made to magnify Christ and to lead to souls being saved.