php圖片裁剪函數(shù)
更新時間:2018年10月31日 10:27:27 作者:可我不愛聰明
這篇文章主要為大家詳細(xì)介紹了php圖片裁剪函數(shù),圖片裁剪工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php圖片裁剪函數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
/* * 圖片裁剪工具 * 將指定文件裁剪成正方形 * 以中心為起始向四周裁剪 * @param $src_path string 源文件地址 * @param $des_path string 保存文件地址 * @param $des_w double 目標(biāo)圖片寬度 * */ 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);//創(chuàng)建真彩畫布 $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); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- PHP的mysqli_set_charset()函數(shù)講解
- PHP的mysqli_select_db()函數(shù)講解
- PHP的mysqli_rollback()函數(shù)講解
- php中的explode()函數(shù)實例介紹
- PHP內(nèi)置函數(shù)生成隨機數(shù)實例
- PHP parse_ini_file函數(shù)的應(yīng)用與擴展操作示例
- PHP實現(xiàn)函數(shù)內(nèi)修改外部變量值的方法示例
- php使用array_chunk函數(shù)將一個數(shù)組分割成多個數(shù)組
- PHP格式化顯示時間date()函數(shù)代碼
- PHP的mysqli_sqlstate()函數(shù)講解
相關(guān)文章
深入理解 PHP7 中全新的 zval 容器和引用計數(shù)機制
這篇文章主要介紹了 PHP7 中全新的 zval 容器和引用計數(shù)機制的相關(guān)知識, 主要側(cè)重于解釋新 zval 容器中的引用計數(shù)機制。需要的朋友可以參考下2018-10-10Laravel 5框架學(xué)習(xí)之Eloquent 關(guān)系
Eloquent是Laravel的原始ActiveRecord是實現(xiàn)的,建立在Laravel的Fluent Query Builder之上的,所以Eloquent類和Fluent類是一樣的,能實現(xiàn)復(fù)雜的SQL語句和非常直觀表達(dá)出的表與表之間的關(guān)系2015-04-04ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作,結(jié)合完整實例形式分析了thinkPHP使用PDO方式連接數(shù)據(jù)庫的相關(guān)配置、控制器及模板調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-03-03