首页 电脑 电脑学堂 查看内容

php实现图片等比例缩放代码

2015-7-24 11:21 1525 0

摘要: 新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称) 源代码如下: ?123456789101112131415?php$filename="q.jpg";$per=0.3;list($width, $height)=getimagesize($filename);$ ...
关键词: 图片 filename height imagedestroy 新图 源代码 width new jpg 图像

新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)

源代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$filename="q.webp";
$per=0.3;
list($width, $height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w, $n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为
imagejpeg($new, "q1.webp");
imagedestroy($new);
imagedestroy($img);
?>

使用浏览器运行过后,在index.php同级的目录下会有个q1.jpg,这个图片就是等比例缩放后的图片,路径可以自己在源代码里面更改,放在自己的项目当中去或写个方法也行

声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部