php图片裁剪函数

本文实例为大家分享了php图片裁剪函数的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
  * 图片裁剪工具
  * 将指定文件裁剪成正方形
  * 以中心为起始向四周裁剪
  * @param $src_path string 源文件地址
  * @param $des_path string 保存文件地址
  * @param $des_w double 目标图片宽度
  * */
function img_cut_square( $src_path , $des_path , $des_w =100){
   $img_info = getimagesize ( $src_path ); //获取原图像尺寸信息
   $img_width = $img_info [0]; //原图宽度
   $img_height = $img_info [1]; //原图高度
   $img_type = $img_info [2]; //图片类型 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式
   if ( $img_type != 2 && $img_type != 3) return ;
 
   /*计算缩放尺寸*/
   if ( $img_height > $img_width ){
     $scale_width = $des_w ; //缩放宽度
     $scale_height = round ( $des_w / $img_width * $img_height ); //缩放高度
 
     $src_y = round (( $scale_height - $des_w )/2);
     $src_x = 0;
   } else {
     $scale_height = $des_w ;
     $scale_width = round ( $des_w / $img_height * $img_width );
 
     $src_y = 0;
     $src_x = round (( $scale_width - $des_w )/2);
   }
 
   $dst_ims = imagecreatetruecolor( $scale_width , $scale_height ); //创建真彩画布
   $white = imagecolorallocate( $dst_ims , 255, 255, 255);
   imagefill( $dst_ims , 0, 0, $white );
   if ( $img_type == 2){
     $src_im = @imagecreatefromjpeg( $src_path ); //读取原图像
   } else if ( $img_type == 3){
     $src_im = @imagecreatefrompng( $src_path ); //读取原图像
   }
 
   imagecopyresized( $dst_ims , $src_im , 0, 0 ,0, 0 , $scale_width , $scale_height , $img_width , $img_height ); //缩放图片到指定尺寸
 
 
   $dst_im = imagecreatetruecolor( $des_w , $des_w );
//  $white = imagecolorallocate($dst_im, 255, 255, 255);
//  imagefill($dst_im, 0, 0, $white);
   imagecopy( $dst_im , $dst_ims , 0, 0, $src_x , $src_y , $des_w , $des_w ); //开始裁剪图片为正方形
// imagecopyresampled($dst_im, $src_im, $src_x, $src_y, 0, 0, $real_width, $real_width,$img_width,$img_height);
   if ( $img_type == 2) {
     imagejpeg( $dst_im , $des_path ); //保存到文件
   } else if ( $img_type == 3){
     imagepng( $dst_im , $des_path );
   }
//  imagejpeg($dst_im);//输出到浏览器
   imagedestroy( $dst_im );
   imagedestroy( $dst_ims );
   imagedestroy( $src_im );
}

 

 
DEDE织梦实战+迅雷看看前端开发项目+HDPHP视频 后盾网三套经典PHP开发实战课程 attach_img
ThinkPHP5基础视频教程 attach_img
Thinkphp5底层源码分析
独立实现ThinkPHP开发整站 前端+后台 attach_img
Thinkphp实战微博项目 attach_img
Thinkphp5.0一套博客程序开发实战
Laravel5实战开发在线图书商城项目 attach_img
Laravel框架从入门到精通【第二季】 attachment
特战旅Laravel实战开发商城前台
Laravel5.2官方文档详细讲解 attach_img
Laravel + AngularJS 实战开发知乎 attach_img
laravel5.2博客项目实战开发 attach_img
Laravel P2P实战教程 attachment
NHW的PHP+ laravel attach_img
PHP四大框架之Laravel 框架 attach_img
laravel实战微信商城及基础应用三套视频+源码 attach_img
Laravel5.4电商实战项目-联想商城 attach_img
基于laravel框架的app软件开发
云知梦教育,laravel电商项目实战视频教程(共24节课程) attach_img
Laravel开发短网址系统(附带源码) attach_img

转载于:https://www.cnblogs.com/janera/p/10016744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值