Home  >  Article  >  Backend Development  >  How to implement php image scaling example code

How to implement php image scaling example code

怪我咯
怪我咯Original
2017-07-10 11:51:421392browse

This article mainly introduces the method of generating uploading picturesthumbnails according to the specified size by scaling in php. Friends who need it can refer to the following

The source code given in the article can realize the function of generating thumbnails according to the proportional scaling of pictures. It is a very practical technique.

Create a new file index.php, you need to have a picture in the statistics directory as pic.jpg (the name of the picture can be changed according to the source code)

The source code is as follows:

<?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);
?>

After running with a browser, there will be pic1.jpg in the directory at the same level as index.php. This picture is a scaled picture. The path can be changed in the source code and placed in your own project or You can also write a method.


The following introduces the method of image compression in PHP. Readers can refer to or improve it according to the specific application and directly enter the code, imgzip($src,$newwid,$newhei)functionWhat is brought in are the original image, the width required for scaling, and the length of scaling.

<?php 
//压缩图片 缩略图
$src= "xiezheng.jpg";
$newwid=640;
$newhei= 480;
function imgzip($src,$newwid,$newhei){
 $imgInfo = getimagesize($src);
 
 $imgType = image_type_to_extension($imgInfo[2], false);
 
 $fun = "imagecreatefrom{$imgType}";
//声明图片 打开图片 在内存中
 $image = $fun($src);
//方便配置长度宽度、高度,设置框为变量wid,高度为hei
 $wid=$imgInfo[0];
 $hei=$imgInfo[1];
//判断长度和宽度,以方便等比缩放,规格按照500, 320
 if($wid>$hei){
 $wid=$newwid;
 $hei=$newwid/($wid/$hei);
 }else {
 $wid = $newhei * ($wid / $hei);
 $hei = $newhei;
 }
//在内存中建立一张图片
 $images2 = imagecreatetruecolor($newwid, $newhei); //建立一个500*320的图片
 
//将原图复制到新建图片中
//imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
 
 imagecopyresampled($images2, $image, 0, 0, 0, 0, $wid,$hei, $imgInfo[0],$imgInfo[1]);
//销毁原始图片
 imagedestroy($image);
//直接输出图片文件
 
 header("Content-type: ".$imgInfo[&#39;mime&#39;]);
 imagejpeg($images2);
//保存图片 到新文件
 imagejpeg($images2, &#39;new.jpg&#39;, 100); //10代码输出图片的质量 0-100 100质量最高
//销毁
 imagedestroy($images2);
}
imgzip($src,$newwid,$newhei);
?>

The above is the detailed content of How to implement php image scaling example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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