php圖片裁剪函數

本文實例爲你們分享了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
相關文章
相關標籤/搜索