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

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

大家好,又见面了,我是全栈君。

一.环境准备

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

二.html页面

<!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>图片ID</th>
            <th>宽度</th>
            <th>高度</th>
            <th>作者</th>
            <th>作者ID</th>
            <th>色调</th>
            <th>原始图</th>
            <th>2X图</th>
            <th>大图</th>
            <th>中图</th>
            <th>小图</th>
            <th>竖向图</th>
            <th>横向图</th>
            <th>极小图</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">颜色块</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">当前页码:<?= $current_page; ?></td>
            <td colspan="3">当前分类:
                <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">每页图片数据量:<?= $per_page; ?></td>
            <td colspan="4">该类图片总数:<?= $total_results; ?></td>
            <td colspan="4"><?= $pagenation; ?></td>
            <td></td>
        </tr>
    </table>

</div>
</body>

</html>

三.控制器

<?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:此处换为你的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;//输出json

        //处理json数据
        extract(json_decode($imageList, true));

        //分页
        $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;//当前页码的前面和后面的“数字”链接的数量
        $config['enable_query_strings']=true;//链接将自动地被重写成查询字符串格式
        $config['page_query_string'] = TRUE;
        $config['reuse_query_string'] = true;
        $config['query_string_segment']='page';
        $config['cur_page'] = ' 1';
        $config['first_link'] = ' 第一页';
        $config['last_link'] = '最后一页 ';

        $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
            )
        );
    }

}

四.效果图

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

五.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 使用权限被终止。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111545.html原文链接:https://javaforall.net

(0)
上一篇 2022年2月18日 下午11:00
下一篇 2022年2月19日 上午6:00


相关推荐

  • 定时备份数据库的存储过程.sql

    定时备份数据库的存储过程.sql

    2021年4月25日
    149
  • 3极管的原理_二极管讲解

    3极管的原理_二极管讲解我们把晶体管比作两个二极管时候,是指基极-射极二极管和基极-集电极二极管。NPN型二极管与PNP型二极管对三极管放大作用的理解,切记一点:能量不会无缘无故的产生,所以,三极管一定不会产生能量。但三极管厉害的地方在于:它可以通过小电流控制大电流。放大的原理就在于:通过小的交流输入,控制大的静态直流。假设三极管是个大坝,这个大坝奇怪的地方是,有两个阀门,一个大阀门,一个小阀门。小阀门可以用…

    2022年10月21日
    6
  • Hibernate二级缓存问题[通俗易懂]

    Hibernate二级缓存问题[通俗易懂]相关概念和定义1、缓存的意义把一些不常修改,但是又经常用的数据存放到内存中,这样能减少与数据库的交互,提升程序的性能2、Hibernate中提供了两级缓存:第一级别的缓存是Session级别的缓存(比如说在调用get方法的时候,如果已经查询过一次了,第二次就不会查了,而是直接返回session缓存中已经存在的那个对象给你,不过这个只对当前Session有效,一旦又开一个新的Sess…

    2022年5月23日
    40
  • 阿里字体图标之Symbol用法

    阿里字体图标之Symbol用法第一步 下载阿里字体图标 Symbol 文件包并解压 第二步 第 1 步 引入项目下面生成的 symbol 代码 scriptsrc iconfont js 第 2 步 加入通用 CSS 代码 引入一次就行 style iconfont width 1em height 1em vertical align 0 15em fill currentColor overflow h style scriptsrc

    2025年11月1日
    11
  • commons-lang里面StringUtils方法说明以及案例

    commons-lang里面StringUtils方法说明以及案例下面总结了StringUtil里面的常用的方法:1.publicstaticbooleanisBlank(Stringstr)在校验一个String类型的变量是否为空时,通常存在3中情况是否为null 是否为"" 是否为空字符串(引号中间有空格)如:""。 制表符、换行符、换页符和回车 StringUtils的…

    2022年6月5日
    29
  • SpringBoot源码分析

    SpringBoot源码分析一.SpringApplication.run(Xxx.class,args)源码分析@SpringBootApplicationpublicclassIndexApplication{publicstaticvoidmain(String[]args){SpringApplication.run(IndexApplication.clas…

    2022年5月18日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号