<?php
$maxWidth = 800;
header('Content-Type:image/png;');
$image = __DIR__.'/img2.png';
list($w,$h) = getimagesize($image); //获取图片宽高
$img = imagecreatefrompng($image);
if($w > $maxWidth){
$_w = $maxWidth;
$_h = $maxWidth*$h/$w;
$_img = imagecreatetruecolor($_w,$_h);
imagecopyresampled($_img,$img,0,0,0,0,$_w,$_h,$w,$h);
imagepng($_img);
imagedestroy($_img);
}else{
imagepng($img);
}
imagedestroy($img);
本例未输出为文件,只是简单将图片加载输出到网页上了。
如需保存为PNG图片,参考imagepng($resource,$filename);函数
生成缩略图只需修改$_w , $_h计算方式即可。
参考资料:vtuce.cn/p/111.html