The purpose of study is to optimize the preparation of porous coal gangue-based geopolymer and quantitatively analyze its pore structure to establish the relationship between pore structure and mechanical properties. Porous coal gangue-based geopolymers were prepared by using coal gangue as the raw material, Na2SiO3 and NaOH as activators, H2O2 as the foaming agent and carboxymethylcellulose sodium (CMC) as the surfactant. Then response surface methodology (RSM) was used to study effects of liquid-to-solid ratio, CMC content, H2O2 content and curing temperature on compressive strength. Finally, pore number, porosity, pore size distribution and pore structure parameters were analyzed by self-developed image analysis algorithm. RSM results demonstrate that H2O2 content has the greatest effect on mechanical strength, followed by liquid-to-solid ratio, curing temperature and CMC content. Quantitative analysis of pore structure indicate that with the increase in H2O2 content, porosity could increase and pore size and pore shape could become more regular, but mechanical properties sharply deteriorate.