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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章