PHP入門之文件、時間、GD圖形圖像

1,文件

1,讀取文件內容

1)函數方式
  1. $content = file_get_contents('./test.txt'); // 將整個文件全部讀取到一個字符串中
  2. $content = file_get_contents('./test.txt', null, null, 100, 500); // 通過參數控制讀取內容的開始點以及長度
2)讀取行數
  1. /*
  2. * 提供類似於C語言操作文件的方法,使用fopen,fgets,fread等方法,
  3. * fgets可以從文件指針中讀取一行,freads可以讀取指定長度的字符串。
  4. */
  5. $fp = fopen('./text.txt', 'rb');
  6. while (! feof($fp))
  7. {
  8. echo fgets($fp); // 讀取一行
  9. }
  10. fclose($fp);
3)讀取字符數
  1. $fp = fopen('./text.txt', 'rb');
  2. $contents = '';
  3. while (! feof($fp))
  4. {
  5. $contents .= fread($fp, 4096); // 一次讀取4096個字符
  6. }
  7. fclose($fp);

2,讀取文件信息
  1. $filename = './test.txt';
  2. /* ----------------------------對文件,存在與否,進行校驗---------------------------- */
  3. file_exists($filename); // 校驗文件或者文件夾是否存在
  4. is_file($filename); // 校驗一個文件是否存在
  5. is_writeable($filename); // 文件 是否 可寫
  6. is_readable($filename); // 文件 是否 可讀
  7. /* ----------------------------對文件,頭部信息,進行校驗---------------------------- */
  8. fileowner($filename); // 獲得文件的所有者
  9. filectime($filename); // 獲取文件的創建時間
  10. filemtime($filename); // 獲取文件的修改時間
  11. fileatime($filename); // 獲取文件的訪問時間
  12. /* ----------------------------文件大小---------------------------- */
  13. filesize($filename); // 獲取文件大小
  14. /**
  15. * 獲取文件大小
  16. * @param unknown $size
  17. * @param string $format
  18. * @return string
  19. */
  20. function getsize($size, $format = 'kb')
  21. {
  22. $p = 0;
  23. if ($format == 'kb')
  24. {
  25. $p = 1;
  26. } elseif ($format == 'mb')
  27. {
  28. $p = 2;
  29. } elseif ($format == 'gb')
  30. {
  31. $p = 3;
  32. }
  33. $size /= pow(1024, $p);
  34. return number_format($size, 3);
  35. }

2,時間

1,獲取日期和時間戳

  1. $time = time(); // unit:s 時間戳
  2. //設置默認的時區
  3. date_default_timezone_set('Asia/Shanghai')."\n";
  4. echo date("Y-m-d")."\n";//2014-03-30

2,日期和時間戳相互轉換
  1. // 時間戳 ==>> 日期
  2. echo date("Y-m-d",'1396193923')."\n";//2014-03-30,1396193923表示2014-03-30的unix時間戳
  3. // 日期 ==>> 時間戳
  4. // 1398700800,這個數字表示從1970年1月1日 00:00:00 到2014年4月29號經歷了1398700800秒
  5. echo strtotime('2014-04-29')."\n";
  6. // 1398700801,這個數字表示從1970年1月1日 00:00:00 到2014-04-29 00:00:01時經歷了1398700801秒
  7. echo strtotime('2014-04-29 00:00:01')."\n";
  8. // 獲取時間戳
  9. echo strtotime("now")."\n";//相當於將英文單詞now直接等於現在的日期和時間,並把這個日期時間轉化爲unix時間戳。這個效果跟echo time();一樣。
  10. echo strtotime("+1 seconds")."\n";//相當於將現在的日期和時間加上了1秒,並把這個日期時間轉化爲unix時間戳。這個效果跟echo time()+1;一樣。
  11. echo strtotime("+1 day")."\n";//相當於將現在的日期和時間加上了1天。
  12. echo strtotime("+1 week")."\n";//相當於將現在的日期和時間加上了1周。
  13. echo strtotime("+1 week 3 days 7 hours 5 seconds")."\n";//相當於將現在的日期和時間加上了1周3天7小時5秒。

3,格式化 格林威治(GMT)標準時間
  1. // 輸出爲:2014-05-01 15:15:22
  2. echo date('Y-m-d H:i:s', time())."\n";
  3. // 輸出爲:2014-05-01 07:15:22
  4. echo gmdate('Y-m-d H:i:s', time())."\n"; // 因爲格林威治時間是現在中國時區的時間減去8個小時,所以相對於現在時間要少8個小時

3,GD圖形圖像

1)簡介

   PHP的GD庫是用來處理圖形的擴展庫,通過GD庫提供的一系列API,可以對圖像進行處理或者直接生成新的圖片。
   PHP通過GD庫,可以對JPG、PNG、GIF、SWF等圖片進行處理。GD庫常用在圖片加水印,驗證碼生成等方面。

2,簡單使用
  1. $img = imagecreatetruecolor(100, 100); // 創建一個畫布
  2. $black = imagecolorallocate($img, 0x00, 0x00, 0x00);
  3. $red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
  4. $blue = imagecolorallocate($img, 0x00, 0x00, 0xFF);
  5. $green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
  6. $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
  7. imagefill($img, 0, 0, $red); // 填充背景
  8. imageline($img, 0, 0, 100, 100, $blue); // 繪製線條
  9. imagestring($img, 5, 0, 0, "Hello world", $green); // 繪製字體
  10. // 生成隨機的驗證碼
  11. $code = '';
  12. for ($i = 0; $i < 4; $i ++)
  13. {
  14. $code .= rand(0, 9);
  15. }
  16. imagestring($img, 5, 10, 10, $code, $black);
  17. // 加入噪點干擾
  18. for ($i = 0; $i < 50; $i ++)
  19. {
  20. imagesetpixel($img, rand(0, 100), rand(0, 100), $black);
  21. imagesetpixel($img, rand(0, 100), rand(0, 100), $green);
  22. }
  23. header("content-type:image/png");
  24. /*
  25. * 使用imagejpeg將圖片保存成jpeg格式,imagegif將圖片保存成gif格式,
  26. * 需要說明的是,imagejpeg會對圖片進行壓縮,因此還可以設置一個質量參數。
  27. */
  28. imagepng($img);
  29. // imagepng($img, 'img.png'); 保存成文件
  30. // imagejpeg($img, 'img.png', 80); 壓縮質量
  31. imagedestroy($img);

3,加入水印
  1. // 最大的問題在於:有些網站的圖片,採用這種方式,會被禁掉;對於我這種小白,很難找修改方案
  2. // 準備一些素材圖片
  3. $img_url = 'http://imgstore.cdn.sogou.com/app/a/100540002/667592.jpg';
  4. $img_content = file_get_contents($img_url);
  5. $img_file_name = 'tmp.jpg';
  6. file_put_contents($img_file_name, $img_content);
  7. $logo_url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png';
  8. $logo_file_name = 'logo.png';
  9. file_put_contents($logo_file_name, file_get_contents($logo_url));
  10. // 水印操作
  11. $result_img = imagecreatefromjpeg($img_file_name);
  12. $result_logo = imagecreatefrompng($logo_file_name);
  13. $result_logo_size = getimagesize($logo_file_name);
  14. imagecopy($result_img, $result_logo, 15, 15, 0, 0, $result_logo_size[0], $result_logo_size[1]);
  15. header("content-type: image/jpeg");
  16. imagejpeg($result_img);


有技術上的問題,或者想法,歡迎來交流
QQ聯繫:[email protected]  // 備註 CSDN
github:https://github.com/yline



發佈了96 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章