php 图片等比例缩放
php 图片等比例缩放代码是一款根据用户上传的图片来指定比例大小的图片,原理很简单就是算出图片大小进等比例就行了,第二款生成小图是固定图片大小,但是如何图片小于设定的图片就填白,这是一个好方法.
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);
- ?>
- 代码二:
- <?php
- 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);//开源代码phpfensi.com
- imagedestroy($newim);
- imagedestroy($im);
- ?>