接口的写法有很多,以下列举几种
利用Jsdelivr
- 找到合适的图片后利用opencv调整图片分辨率,并输出地址(可以忽略这步)
import os
import cv2
file_path = "D:\图片路径\"
web_path = "https://cdn.jsdelivr.net/gh/替换为github存放图片的地址"
def img_resize(image_path):
image = cv2.imread(file_path+image_path)
height, width = image.shape[0], image.shape[1]
# 设置新的图片分辨率框架
width_new = 1920
height_new = 1080
# 判断图片的长宽比率
if width / height >= width_new / height_new:
img = cv2.resize(image, (width_new, int(height * width_new / width)))
else:
img = cv2.resize(image, (int(width * height_new / height), height_new))
if ".jpg" in image_path:
cv2.imwrite(file_path+image_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
elif ".png" in image_path:
cv2.imwrite(file_path+image_path, img, [cv2.IMWRITE_PNG_COMPRESSION, 0])
if __name__ == '__main__':
filelist = os.listdir(file_path)
with open('D:\输出文档路径\img.txt','w') as f:
for file in filelist:
img_resize(file)
f.write(web_path+file+'\n')
- 将图片push到github
- 以下是读取链接并重定向的php代码
- 将保存有图片地址的文件放相同目录下,访问php文件即可
<?php
//存有美图链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
die('文件不存在');
}
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
$line=trim(fgets($fs));
if($line!=''){
array_push($pics, $line);
}
}
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
//返回指定格式
$type=$_GET['type'];
switch($type){
//JSON返回
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic]));
default:
die(header("Location: $pic"));
}
?>
读取文件夹图片
- 将图片上传到某文件夹下
- 修改代码中图片路径,访问即可
<?php
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
header("Access-Control-Allow-Origin:*");
//处理请求输出数据
//将得到一个文件夹中的所有gif,jpg和png图片的数组
$rand=rand(0,1);
if($rand){
$localurl="images/*.{gif,jpg,png}";
}else{
$localurl="images/*.{gif,jpg,png}";
}
$img_array=glob($localurl,GLOB_BRACE);
//从数组中选择一个随机图片
$img=array_rand($img_array);
$imgurl=$img_array[$img];
$https=isset($_GET["https"])?$_GET["https"]:1;
if($https == "true"){
$imgurl='https://'.$_SERVER['SERVER_NAME'].'/Web/api/photo/wm/'.$imgurl;
}else{
$imgurl='http://'.$_SERVER['SERVER_NAME'].'/Web/api/photo/wm/'.$imgurl;
//例如$imgurl='https://baidu.com/Web/api/photo/wm/'.$imgurl;
}
//以下可删去
if(isset($_GET["type"])?$_GET["type"]:1=="json"){
$rTotal='0';
$gTotal='0';
$bTotal='0';
$total='0';
$imageInfo = getimagesize($img_array[$img]);
//图片类型
$imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
//对应函数
$imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
$i = $imageFun($img_array[$img]);
//测试图片,自己定义一个,注意路径
for($x=0;
$x<imagesx($i);
$x++){
for($y=0;
$y<imagesy($i);
$y++){
$rgb=imagecolorat($i,$x,$y);
$r=($rgb>>16)&0xFF;
$g=($rgb>>8)&0xFF;
$b=$rgb&0xFF;
$rTotal+=$r;
$gTotal+=$g;
$bTotal+=$b;
$total++;
}
}
$rAverage=round($rTotal/$total);
$gAverage=round($gTotal/$total);
$bAverage=round($bTotal/$total);
$arr=array('ImgUrl'=>$imgurl,'Color'=>"$rAverage,$gAverage,$bAverage");
echo json_encode($arr);
exit();
}
//在页面显示图片地址
//echo $imgurl;
header("location:$imgurl");
?>
- 使用以下程序也可
<?php
$rand=rand(0,1);
if($rand){
$localurl="images/*.{gif,jpg,png}";
}else{
$localurl="images/*.{gif,jpg,png}";
}
$img_array=glob($localurl,GLOB_BRACE);
$img=array_rand($img_array);
$imgurl=$img_array[$img];
if($https == "true"){
$imgurl='https://'.$_SERVER['SERVER_NAME'].'/Web/api/photo/wm/'.$imgurl;
}
header("location:$imgurl");
?>
读取文件图片链接
- 新建php文件写入以下代码
- 新建txt文件存放图片链接,一行一个
<?php
$type=isset($_GET['return'])?$_GET['return']:'';
if($type=="")
echo ' '
?>
<?php
$str = explode("\n", file_get_contents('md5.txt'));
$shu = count($str)-1;
$k = rand(0,$shu);
$sina_img = str_re($str[$k]);
$url = 'http://地址/origin/'.$sina_img.'.png';
$result=array("code"=>"200","imgurl"=>"$url");
switch ($type){
case 'json':
$imageInfo = getimagesize($url);
header('Content-type:text/json');
echo json_encode($result);
break;
default:
header("Location:".$result['imgurl']);
break;
}
function str_re($str){
$str = str_replace(' ', "", $str);
$str = str_replace("\n", "", $str);
$str = str_replace("\t", "", $str);
$str = str_replace("\r", "", $str);
return $str;
}
?>