当前位置:网站首页>Based on pexels image material API, sort out the material resource library
Based on pexels image material API, sort out the material resource library
2022-07-25 20:41:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
One . Environmental preparation
php7.1+NGINX+ci Framework environment , Need to register with pexels api_key
Two .html page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
body {
font-size: 16px;
}
a {
text-decoration: none;
color: #FFFFFF;
}
.successful_end {
width: 100%;
height: 3rem;
background-color: #363;
margin-bottom: 2px;
}
td, th {
border: 1px dotted #99a9bf;
}
th {
background-color: #995300;
}
td > a, strong {
margin-left: 10px;
}
</style>
<?php $this->load->helper('url');?>
<script src="<?= base_url().'/static/js/jquery-3.2.1.min.js'?>"></script>
<body>
<div class="successful">
<table style="text-align: center;">
<tr>
<th> picture ID</th>
<th> Width </th>
<th> Height </th>
<th> author </th>
<th> author ID</th>
<th> tonal </th>
<th> The original picture </th>
<th>2X chart </th>
<th> Big picture </th>
<th> Chinese </th>
<th> Little picture </th>
<th> Vertical drawing </th>
<th> Horizontal view </th>
<th> Minimal graph </th>
</tr>
<?php
foreach ($photos as $imgs) {
?>
<tr>
<td><?= $imgs['id'] ?></td>
<td><?= $imgs['width'] ?></td>
<td><?= $imgs['height'] ?></td>
<td><?= $imgs['photographer'] ?></td>
<td><?= $imgs['photographer_id'] ?></td>
<td>
<div style="background-color: <?= $imgs['avg_color']; ?>;width: 50px;height: 35px"> Color block </div>
</td>
<td><img src="<?= $imgs['src']['original'] ?>" alt="" width="100%" height="auto"></td>
<td><img src="<?= $imgs['src']['large2x'] ?>" alt="" width="100%" height="auto"></td>
<td><img src="<?= $imgs['src']['large'] ?>" alt="" width="50%" height="auto"></td>
<td><img src="<?= $imgs['src']['medium'] ?>" alt="" width="50%" height="auto"></td>
<td><img src="<?= $imgs['src']['small'] ?>" alt="" width="50%" height="auto"></td>
<td><img src="<?= $imgs['src']['portrait'] ?>" alt="" width="50%" height="auto"></td>
<td><img src="<?= $imgs['src']['landscape'] ?>" alt="" width="50%" height="auto"></td>
<td><img src="<?= $imgs['src']['tiny'] ?>" alt=""></td>
</tr>
<?php
}
?>
<tr class="successful_end">
<td colspan="3"> The current page number :<?= $current_page; ?></td>
<td colspan="3"> Current classification :
<select name="style" onchange="window.open(this.options[this.selectedIndex].value,'_self')">
<option value="<?= current_url() . '?style=People&per_page='.$per_page ?>"<?= ($this->input->get('style')=='People')? 'selected':''?>>People</option>
<option value="<?= current_url() . '?style=Ocean&per_page='.$per_page ?>" <?= ($this->input->get('style')=='Ocean')? 'selected':''?>>Ocean</option>
<option value="<?= current_url() . '?style=Tigers&per_page='.$per_page ?>"<?= ($this->input->get('style')=='Tigers')? 'selected':''?>>Tigers</option>
<option value="<?= current_url() . '?style=nature&per_page='.$per_page ?>"<?= ($this->input->get('style')=='nature')? 'selected':''?>>nature</option>
</select>
</td>
<td colspan="2"> The amount of image data per page :<?= $per_page; ?></td>
<td colspan="4"> The total number of such pictures :<?= $total_results; ?></td>
<td colspan="4"><?= $pagenation; ?></td>
<td></td>
</tr>
</table>
</div>
</body>
</html>3、 ... and . controller
<?php
class Image extends Base_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('pagination');
$this->load->helper('url');
$this->redis = $this->connectredis();
}
public function getImageLists()
{
$per_page = $this->input->get('per_page') ?? 1;
$page = $this->input->get('page') ?? 1;
$style = $this->input->get('style') ?? 'people';
$image_key = 'images_page_' . $page . '_style_' . $style;
$imageList = $this->redis->get($image_key);
if (!$imageList) {
$ch = curl_init();
//https://api.pexels.com/v1/search/?page=2&per_page=15&query=people
$url = 'https://api.pexels.com/v1/search?query=' . $style . '&page=' . $page . '&per_page=' . $per_page;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization:api_key'; //todo: Change here for your api_key
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$imageList = $result;
$this->redis->set($image_key, $result,3600);
}
//echo $result;// Output json
// Handle json data
extract(json_decode($imageList, true));
// Pagination
$config['base_url'] = base_url().'/v1/image/getImageLists';
$config['total_rows'] = $total_results;
$config['per_page'] = $per_page;
// $config['uri_segment'] = 2;
$config['num_links'] = 2;// Before and after the current page number “ Numbers ” The number of links
$config['enable_query_strings']=true;// Links will automatically be rewritten into query string format
$config['page_query_string'] = TRUE;
$config['reuse_query_string'] = true;
$config['query_string_segment']='page';
$config['cur_page'] = ' 1';
$config['first_link'] = ' first page ';
$config['last_link'] = ' The last page ';
$this->pagination->initialize($config);
$pagenation=$this->pagination->create_links();
$this->load->view('image/image',
array('current_page' => ceil($page/$per_page)+1,
'per_page' => $per_page,
"photos" => $photos,
'next_page' => $next_page,
'total_results' => $total_results,
'pagenation'=>$pagenation
)
);
}
}Four . design sketch
5、 ... and .pexels file : https://www.pexels.com/zh-cn/api/documentation/?
Whenever you submit a API When asked , Please make sure to show To Pexels Eye catching links . You can use text links ( Such as “ Photos by Pexels Provide ”) Or a link with our logo . Where possible , Please always indicate our photographer ( Such as “Pexels Upper slave John Doe Photos taken ”, And it can be transferred to Pexels Links to photo pages ). You may not copy or duplicate Pexels Core functions ( Including provision Pexels Content as wallpaper Application ). Do not abuse this API. By default ,API The upper limit of usage is per hour 200 Requests and monthly 20000 A request . In case of abuse Pexels API act ( Including but not limited to trying to circumvent usage restrictions ), Will lead to your API Permission is terminated .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111545.html Link to the original text :https://javaforall.cn
边栏推荐
- Has baozi ever played in the multi merchant system?
- Implementation of simple registration and login
- 移动web布局方法
- leetcode-6125:相等行列对
- Leetcode-6129: number of all 0 subarrays
- leetcode-919:完全二叉树插入器
- 【单细胞高级绘图】07.KEGG富集结果展示
- Behind every piece of information you collect, you can't live without TA
- Web crawler principle analysis "suggestions collection"
- [technical dry goods] how to ensure the idempotency of the interface?
猜你喜欢

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

【高等数学】【5】定积分及应用

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)

Working principle of radar water level gauge and precautions for installation and maintenance

Recommended books | essentials of industrial digital transformation: methods and Practice

Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative

Leetcode customs clearance: hash table six, this is really a little simple
![[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance](/img/2d/c23a367c9e8e2806ffd5384de273d2.png)
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance
随机推荐
Behind every piece of information you collect, you can't live without TA
Formatdatetime explanation [easy to understand]
Force deduction ----- calculate the money of the force deduction bank
Leetcode-6131: the shortest dice sequence impossible to get
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
[tensorrt] trtexec tool to engine
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
Leetcode customs clearance: hash table six, this is really a little simple
leetcode-6130:设计数字容器系统
Leetcode-6127: number of high-quality pairs
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
[matlab] download originality documents based on oil monkey script and MATLAB
第六章 修改规范(SPEC)类
“链”接无限可能:数字资产链,精彩马上来!
Mobile web layout method
Yolov7 training error indexerror: list index out of range
[technical dry goods] how to ensure the idempotency of the interface?
leetcode-6131:不可能得到的最短骰子序列
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)