基于pexels 图片素材api,整理出素材资源库

举报
lxw1844912514 发表于 2022/03/27 01:46:30 2022/03/27
【摘要】 一.环境准备   php7.1+NGINX+ci框架环境,需要注册有pexels api_key 二.html页面 <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta n...

一.环境准备

 

php7.1+NGINX+ci框架环境,需要注册有pexels api_key

二.html页面


  
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. body {
  14. font-size: 16px;
  15. }
  16. a {
  17. text-decoration: none;
  18. color: #FFFFFF;
  19. }
  20. .successful_end {
  21. width: 100%;
  22. height: 3rem;
  23. background-color: #363;
  24. margin-bottom: 2px;
  25. }
  26. td, th {
  27. border: 1px dotted #99a9bf;
  28. }
  29. th {
  30. background-color: #995300;
  31. }
  32. td > a, strong {
  33. margin-left: 10px;
  34. }
  35. </style>
  36. <?php $this->load->helper('url');?>
  37. <script src="<?= base_url().'/static/js/jquery-3.2.1.min.js'?>"></script>
  38. <body>
  39. <div class="successful">
  40. <table style="text-align: center;">
  41. <tr>
  42. <th>图片ID</th>
  43. <th>宽度</th>
  44. <th>高度</th>
  45. <th>作者</th>
  46. <th>作者ID</th>
  47. <th>色调</th>
  48. <th>原始图</th>
  49. <th>2X图</th>
  50. <th>大图</th>
  51. <th>中图</th>
  52. <th>小图</th>
  53. <th>竖向图</th>
  54. <th>横向图</th>
  55. <th>极小图</th>
  56. </tr>
  57. <?php
  58. foreach ($photos as $imgs) {
  59. ?>
  60. <tr>
  61. <td><?= $imgs['id'] ?></td>
  62. <td><?= $imgs['width'] ?></td>
  63. <td><?= $imgs['height'] ?></td>
  64. <td><?= $imgs['photographer'] ?></td>
  65. <td><?= $imgs['photographer_id'] ?></td>
  66. <td>
  67. <div style="background-color: <?= $imgs['avg_color']; ?>;width: 50px;height: 35px">颜色块</div>
  68. </td>
  69. <td><img src="<?= $imgs['src']['original'] ?>" alt="" width="100%" height="auto"></td>
  70. <td><img src="<?= $imgs['src']['large2x'] ?>" alt="" width="100%" height="auto"></td>
  71. <td><img src="<?= $imgs['src']['large'] ?>" alt="" width="50%" height="auto"></td>
  72. <td><img src="<?= $imgs['src']['medium'] ?>" alt="" width="50%" height="auto"></td>
  73. <td><img src="<?= $imgs['src']['small'] ?>" alt="" width="50%" height="auto"></td>
  74. <td><img src="<?= $imgs['src']['portrait'] ?>" alt="" width="50%" height="auto"></td>
  75. <td><img src="<?= $imgs['src']['landscape'] ?>" alt="" width="50%" height="auto"></td>
  76. <td><img src="<?= $imgs['src']['tiny'] ?>" alt=""></td>
  77. </tr>
  78. <?php
  79. }
  80. ?>
  81. <tr class="successful_end">
  82. <td colspan="3">当前页码:<?= $current_page; ?></td>
  83. <td colspan="3">当前分类:
  84. <select name="style" onchange="window.open(this.options[this.selectedIndex].value,'_self')">
  85. <option value="<?= current_url() . '?style=People&per_page='.$per_page ?>"<?= ($this->input->get('style')=='People')? 'selected':''?>>People</option>
  86. <option value="<?= current_url() . '?style=Ocean&per_page='.$per_page ?>" <?= ($this->input->get('style')=='Ocean')? 'selected':''?>>Ocean</option>
  87. <option value="<?= current_url() . '?style=Tigers&per_page='.$per_page ?>"<?= ($this->input->get('style')=='Tigers')? 'selected':''?>>Tigers</option>
  88. <option value="<?= current_url() . '?style=nature&per_page='.$per_page ?>"<?= ($this->input->get('style')=='nature')? 'selected':''?>>nature</option>
  89. </select>
  90. </td>
  91. <td colspan="2">每页图片数据量:<?= $per_page; ?></td>
  92. <td colspan="4">该类图片总数:<?= $total_results; ?></td>
  93. <td colspan="4"><?= $pagenation; ?></td>
  94. <td></td>
  95. </tr>
  96. </table>
  97. </div>
  98. </body>
  99. </html>

三.控制器


  
  1. <?php
  2. class Image extends Base_Controller
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->library('pagination');
  8. $this->load->helper('url');
  9. $this->redis = $this->connectredis();
  10. }
  11. public function getImageLists()
  12. {
  13. $per_page = $this->input->get('per_page') ?? 1;
  14. $page = $this->input->get('page') ?? 1;
  15. $style = $this->input->get('style') ?? 'people';
  16. $image_key = 'images_page_' . $page . '_style_' . $style;
  17. $imageList = $this->redis->get($image_key);
  18. if (!$imageList) {
  19. $ch = curl_init();
  20. //https://api.pexels.com/v1/search/?page=2&per_page=15&query=people
  21. $url = 'https://api.pexels.com/v1/search?query=' . $style . '&page=' . $page . '&per_page=' . $per_page;
  22. curl_setopt($ch, CURLOPT_URL, $url);
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  25. $headers = array();
  26. $headers[] = 'Authorization:api_key'; //todo:此处换为你的api_key
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  28. $result = curl_exec($ch);
  29. if (curl_errno($ch)) {
  30. echo 'Error:' . curl_error($ch);
  31. }
  32. curl_close($ch);
  33. $imageList = $result;
  34. $this->redis->set($image_key, $result,3600);
  35. }
  36. //echo $result;//输出json
  37. //处理json数据
  38. extract(json_decode($imageList, true));
  39. //分页
  40. $config['base_url'] = base_url().'/v1/image/getImageLists';
  41. $config['total_rows'] = $total_results;
  42. $config['per_page'] = $per_page;
  43. // $config['uri_segment'] = 2;
  44. $config['num_links'] = 2;//当前页码的前面和后面的“数字”链接的数量
  45. $config['enable_query_strings']=true;//链接将自动地被重写成查询字符串格式
  46. $config['page_query_string'] = TRUE;
  47. $config['reuse_query_string'] = true;
  48. $config['query_string_segment']='page';
  49. $config['cur_page'] = ' 1';
  50. $config['first_link'] = ' 第一页';
  51. $config['last_link'] = '最后一页 ';
  52. $this->pagination->initialize($config);
  53. $pagenation=$this->pagination->create_links();
  54. $this->load->view('image/image',
  55. array('current_page' => ceil($page/$per_page)+1,
  56. 'per_page' => $per_page,
  57. "photos" => $photos,
  58. 'next_page' => $next_page,
  59. 'total_results' => $total_results,
  60. 'pagenation'=>$pagenation
  61. )
  62. );
  63. }
  64. }

四.效果图

五.pexels 文档: https://www.pexels.com/zh-cn/api/documentation/?

每当你提交一个 API 请求时,请确保一定要显示到 Pexels 的醒目链接。你可以使用文本链接(如“照片由 Pexels 提供”)或带有我们徽标的链接。

在可能的情况下,请始终注明我们的摄影作者(如“Pexels 上由 John Doe 拍摄的照片”,并附带可转至 Pexels 照片页的链接)。

你不得拷贝或复制 Pexels 的核心功能(包括提供 Pexels 内容作为壁纸应用)。

请勿滥用该 API。默认情况下,API 的使用率上限为每小时 200 个请求和每月 20000 个请求你可以联系我们,申请提高此限制,但请提供示例,或做好准备提供演示文件,确保清晰展示你使用该 API 时已注明归属。如果你符合我们的 API 条款,可以免费获得无限次请求。

如有滥用 Pexels API 的行为(包括但不限于试图规避使用率限制),将导致你的 API 使用权限被终止。

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/116102789

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。