二款php 图片等比例缩放

原创
2016-06-13 09:48:30 1003浏览

php 图片等比例缩放代码是一款根据用户上传的图片来指定比例大小的图片,原理很简单就是算出图片大小进等比例就行了,第二款生成小图是固定图片大小,但是如何图片小于设定的图片就填白,这是一个好方法哦。

php教程 图片等比例缩放代码是一款根据用户上传的图片来指定比例大小的图片,原理很简单就是算出图片大小进等比例就行了,第二款生成小图是固定图片大小,但是如何图片小于设定的图片就填白,这是一个好方法哦。

header("content-type:image/jpeg");
$filename = hsdir.'/mljntc2p.jpg';
$im = imagecreatefromjpeg($filename);
$h=imagesy($im);//获得目标图片高度
$new_img_width = 257;
$new_img_height = 522;

$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagefilledrectangle($newim,0,$h,$new_img_width,$new_img_height,$white);
//填充 目标图片高度作为起驶y坐标 以指定截取宽高为结束坐标
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

代码二

header("content-type:image/jpeg");
$filename = 'myface.jpg';
$im = imagecreatefromjpeg($filename);
$new_img_width = 80;
$new_img_height = 150;
$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。