佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > php圖片裁剪函數(shù)

php圖片裁剪函數(shù)

熱門標(biāo)簽:團購網(wǎng)站 銀行業(yè)務(wù) 電子圍欄 阿里云 科大訊飛語音識別系統(tǒng) Mysql連接數(shù)設(shè)置 Linux服務(wù)器 服務(wù)器配置

本文實例為大家分享了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ù)講解

標(biāo)簽:江蘇 萍鄉(xiāng) 大理 蚌埠 棗莊 廣元 衡水 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php圖片裁剪函數(shù)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    南汇区| 西乌| 马边| 彭水| 绿春县| 惠水县| 黄大仙区| 普定县| 海门市| 玛曲县| 东安县| 邳州市| 阿勒泰市| 盈江县| 镶黄旗| 连平县| 习水县| 玉环县| 留坝县| 喀喇| 济宁市| 拉萨市| 綦江县| 出国| 崇州市| 远安县| 邵东县| 井研县| 贞丰县| 天门市| 临海市| 阿尔山市| 调兵山市| 青冈县| 化德县| 宣汉县| 肇源县| 田东县| 马边| 阿巴嘎旗| 石狮市|