濮阳杆衣贸易有限公司

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

php圖片裁剪函數(shù)

熱門標簽:神龍斗士電話機器人 宿州正規(guī)外呼系統(tǒng)軟件 萍鄉(xiāng)商鋪地圖標注 電信外呼系統(tǒng)多少錢一個月 企業(yè)400電話辦理多少費用 合肥企業(yè)外呼系統(tǒng)線路 太原400電話申請流程 桂陽公司如何做地圖標注 代理打電話機器人

本文實例為大家分享了php圖片裁剪函數(shù)的具體代碼,供大家參考,具體內容如下

/*
 * 圖片裁剪工具
 * 將指定文件裁剪成正方形
 * 以中心為起始向四周裁剪
 * @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);//創(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);
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP的mysqli_set_charset()函數(shù)講解
  • PHP的mysqli_select_db()函數(shù)講解
  • PHP的mysqli_rollback()函數(shù)講解
  • php中的explode()函數(shù)實例介紹
  • PHP內置函數(shù)生成隨機數(shù)實例
  • PHP parse_ini_file函數(shù)的應用與擴展操作示例
  • PHP實現(xiàn)函數(shù)內修改外部變量值的方法示例
  • php使用array_chunk函數(shù)將一個數(shù)組分割成多個數(shù)組
  • PHP格式化顯示時間date()函數(shù)代碼
  • PHP的mysqli_sqlstate()函數(shù)講解

標簽:辛集 白銀 衡陽 綏化 太原 廊坊 鄂州 崇左

巨人網絡通訊聲明:本文標題《php圖片裁剪函數(shù)》,本文關鍵詞  php,圖片,裁剪,函數(shù),php,圖片,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php圖片裁剪函數(shù)》相關的同類信息!
  • 本頁收集關于php圖片裁剪函數(shù)的相關信息資訊供網民參考!
  • 推薦文章
    永州市| 息烽县| 佛坪县| 彝良县| 镇康县| 驻马店市| 横峰县| 石台县| 佳木斯市| 科尔| 常宁市| 垣曲县| 墨竹工卡县| 连平县| 宕昌县| 榆树市| 遵义县| 台北县| 宝山区| 长寿区| 巴楚县| 长海县| 连南| 东乡| 山阳县| 门头沟区| 禄劝| 伊金霍洛旗| 星座| 贡山| 宁津县| 南京市| 鄂托克前旗| 屯留县| 丹江口市| 托克托县| 射洪县| 嵩明县| 迭部县| 铁力市| 上栗县|