欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 圖片處理

 更新時(shí)間:2020年09月16日 10:08:12   作者:Mr.Yong  
這篇文章主要介紹了PHP如何對(duì)圖片處理,幫助大家更好的利用PHP處理圖片,感興趣的朋友可以了解下

圖片處理函數(shù)功能:縮放、剪切、相框、水印、銳化、旋轉(zhuǎn)、翻轉(zhuǎn)、透明度、反色處理并保存歷史記錄的思路:當(dāng)有圖片有改動(dòng)時(shí)自動(dòng)生成一張新圖片

1、轉(zhuǎn)Base64編碼

/**
 * 獲取圖片的Base64編碼(不支持url)
 * @param $img_file 傳入本地圖片地址
 * @return string
 */
function imgToBase64($img_file) {
 $img_base64 = '';
 if (file_exists($img_file)) {
  $app_img_file = $img_file; // 圖片路徑
  $img_info = getimagesize($app_img_file); // 取得圖片的大小,類(lèi)型等
  //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
  list($width, $height, $type, $attr) = getimagesize($app_img_file);
  $fp = fopen($app_img_file, "r"); // 圖片是否可讀權(quán)限
  if ($fp) {
   $filesize = filesize($app_img_file);
   $content = fread($fp, $filesize);
   $file_content = chunk_split(base64_encode($content)); // base64編碼
   switch ($type) {   //判讀圖片類(lèi)型
    case 1: $img_type = "gif";
     break;
    case 2: $img_type = "jpg";
     break;
    case 3: $img_type = "png";
     break;
   }
   $img_base64 = 'data:image/png;base64,' . $file_content;//合成圖片的base64編碼
  }
  fclose($fp);
 }else{
  return $img_file;
 }
 return $img_base64; //返回圖片的base64
}

2、圖片旋轉(zhuǎn)

/**
 * 圖片旋轉(zhuǎn)
 * @param $src 圖片地址
 * @param $direction 1順時(shí)針90 2 逆時(shí)針90
 * @return string
 */
function imgturn($src, $direction = 1){
 $ext = pathinfo($src)['extension'];
 switch ($ext) {
  case 'gif':
   $img = imagecreatefromgif($src);
   break;
  case 'jpg':
  case 'jpeg':
   $img = imagecreatefromjpeg($src);
   break;
  case 'png':
   $img = imagecreatefrompng($src);
   break;
  default:
   die('圖片格式錯(cuò)誤!');
   break;
 }
 $width = imagesx($img);
 $height = imagesy($img);
 $img2 = imagecreatetruecolor($height, $width);
 //順時(shí)針旋轉(zhuǎn)90度
 if($direction == 1){
  for ($x = 0; $x < $width; $x++) {
   for($y=0; $y<$height; $y++) {
    imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1);
   }
  }
 }else if($direction == 2) {
  //逆時(shí)針旋轉(zhuǎn)90度
  for ($x = 0; $x < $height; $x++) {
   for($y = 0; $y < $width; $y++) {
    imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1);
   }
  }
 }
 switch ($ext) {
  case 'jpg':
  case "jpeg":
   imagejpeg($img2, $src, 100);
   break;
  case "gif":
   imagegif($img2, $src, 100);
   break;
  case "png":
   imagepng($img2, $src, 100);
   break;
  default:
   die('圖片格式錯(cuò)誤!');
   break;
 }
 imagedestroy($img);
 imagedestroy($img2);
}

3、圖片壓縮

/**
* 圖片壓縮處理
* @param string $sFile 源圖片路徑
* @param int $iWidth 自定義圖片寬度
* @param int $iHeight 自定義圖片高度
* @return string 壓縮后的圖片路徑
*/
function getThumb($sFile, $iWidth, $iHeight){
 //圖片公共路徑
 $public_path = '';
 //判斷該圖片是否存在
 if(!file_exists($public_path . $sFile)) return $sFile;
 list($width, $height, $type, $attr) = getimagesize($sFile);
 if($width < $height){
  imgturn($sFile, 2);
 }
 //判斷圖片格式(圖片文件后綴)
 $extend = explode("." , $sFile);
 $attach_fileext = strtolower($extend[count($extend) - 1]);
 if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
  return '';
 }
 //壓縮圖片文件名稱
 $sFileNameS = str_replace("." . $attach_fileext, "_" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile);
 //判斷是否已壓縮圖片,若是則返回壓縮圖片路徑
 if(file_exists($public_path . $sFileNameS)){
  return $sFileNameS;
 }
 //生成壓縮圖片,并存儲(chǔ)到原圖同路徑下
 resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight);
 if(!file_exists($public_path . $sFileNameS)){
  return $sFile;
 }
 return $sFileNameS;
}

4、生成目標(biāo)圖片

/**
 * 生成圖片
 * @param string $im 源圖片路徑
 * @param string $dest 目標(biāo)圖片路徑
 * @param int $maxwidth 生成圖片寬
 * @param int $maxheight 生成圖片高
 */
function resizeImage($im, $dest, $maxwidth, $maxheight) {
 $img = getimagesize($im);
 switch ($img[2]) {
  case 1:
   $im = @imagecreatefromgif($im);
  break;
  case 2:
   $im = @imagecreatefromjpeg($im);
  break;
  case 3:
   $im = @imagecreatefrompng($im);
  break;
 }
 $pic_width = imagesx($im);
 $pic_height = imagesy($im);
 $resizewidth_tag = false;
 $resizeheight_tag = false;
 if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  if ($maxwidth && $pic_width > $maxwidth) {
   $widthratio = $maxwidth / $pic_width;
   $resizewidth_tag = true;
  }
  if ($maxheight && $pic_height > $maxheight) {
   $heightratio = $maxheight / $pic_height;
   $resizeheight_tag = true;
  }
  if ($resizewidth_tag && $resizeheight_tag) {
   if ($widthratio < $heightratio){
    $ratio = $widthratio;
   }else{
    $ratio = $heightratio;
   }
  }
  if ($resizewidth_tag && !$resizeheight_tag){
   $ratio = $widthratio;
  }
  if ($resizeheight_tag && !$resizewidth_tag){
   $ratio = $heightratio;
  }
  $newwidth = $pic_width * $ratio;
  $newheight = $pic_height * $ratio;
  if (function_exists("imagecopyresampled")) {
   $newim = imagecreatetruecolor($newwidth, $newheight);
   imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  } else {
   $newim = imagecreate($newwidth, $newheight);
   imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  }
  imagejpeg($newim, $dest);
  imagedestroy($newim);
 } else {
  imagejpeg($im, $dest);
 }
}

以上就是PHP對(duì)圖片的處理的詳細(xì)內(nèi)容,更多關(guān)于PHP 圖片處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • PHP導(dǎo)出帶樣式的Excel示例代碼

    PHP導(dǎo)出帶樣式的Excel示例代碼

    相信大家在工作的時(shí)候有客戶會(huì)向你抱怨,軟件為他們導(dǎo)出的Excel格式太難看了,這個(gè)時(shí)候我們就需要到處自定義樣式的Excel了,那么或許這篇文章會(huì)對(duì)你有所幫助,有需要的可以參考借鑒。
    2016-08-08
  • 再談Yii Framework框架中的事件event原理與應(yīng)用

    再談Yii Framework框架中的事件event原理與應(yīng)用

    這篇文章主要介紹了再談Yii Framework框架中的事件event原理與應(yīng)用,結(jié)合實(shí)例形式分析了再談Yii框架中的事件event相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • Symfony2創(chuàng)建頁(yè)面實(shí)例詳解

    Symfony2創(chuàng)建頁(yè)面實(shí)例詳解

    這篇文章主要介紹了Symfony2創(chuàng)建頁(yè)面的方法,結(jié)合實(shí)例形式分析了Symfony頁(yè)面創(chuàng)建的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-03-03
  • Laravel 在views中加載公共頁(yè)面的實(shí)現(xiàn)代碼

    Laravel 在views中加載公共頁(yè)面的實(shí)現(xiàn)代碼

    今天小編就為大家分享一篇Laravel 在views中加載公共頁(yè)面的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • php批量修改表結(jié)構(gòu)實(shí)例

    php批量修改表結(jié)構(gòu)實(shí)例

    下面小編就為大家?guī)?lái)一篇php批量修改表結(jié)構(gòu)實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Yii CGridView用法實(shí)例詳解

    Yii CGridView用法實(shí)例詳解

    這篇文章主要介紹了Yii CGridView用法,結(jié)合實(shí)例形式分析了CGridView的功能、用法與相關(guān)屬性用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • 基于PHP實(shí)現(xiàn)用戶在線狀態(tài)檢測(cè)

    基于PHP實(shí)現(xiàn)用戶在線狀態(tài)檢測(cè)

    這篇文章主要介紹了基于PHP實(shí)現(xiàn)用戶在線狀態(tài)檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 基于php設(shè)計(jì)模式中工廠模式詳細(xì)介紹

    基于php設(shè)計(jì)模式中工廠模式詳細(xì)介紹

    本篇文章是對(duì)php設(shè)計(jì)模式中工廠模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • thinkphp跨庫(kù)操作的簡(jiǎn)單代碼實(shí)例

    thinkphp跨庫(kù)操作的簡(jiǎn)單代碼實(shí)例

    下面小編就為大家?guī)?lái)一篇thinkphp跨庫(kù)操作的簡(jiǎn)單代碼實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • thinkPHP框架中執(zhí)行事務(wù)的方法示例

    thinkPHP框架中執(zhí)行事務(wù)的方法示例

    這篇文章主要介紹了thinkPHP框架中執(zhí)行事務(wù)的方法,結(jié)合實(shí)例形式分析了thinkPHP框架中使用模型中封裝的startTran()、Commit()及Rollback()方法執(zhí)行事務(wù)與回滾操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05

最新評(píng)論