Home > Backend Development > PHP Tutorial > 如何实现php图片等比例缩放_PHP

如何实现php图片等比例缩放_PHP

WBOY
Release: 2016-05-30 08:46:15
Original
987 people have browsed it

通过文章给出的源代码可实现针对图片的等比缩放生成缩略图的功能,非常实用的技巧哦。

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

源代码如下:

<&#63;php
$filename="pic.jpg";
$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);
//copy部分图像并调整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为
imagejpeg($new, "pic1.jpg");
imagedestroy($new);
imagedestroy($img);
&#63;>
Copy after login

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

以上就是本文的全部内容,希望对大家的学习有所帮助。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template