我的cms有一个小脚本。我正在尝试在图像链接不存在时创建默认图像(default.png)。我在其他网站上有所有图像,我创建了这段代码:
#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# functions fx and fy take log-scale coordinates to 'axes' coordinates
ax = 1E-12 # [ax,bx] is range of horizontal axis
bx = 1E0
def fx(x):
return (np.log(x) - np.log(ax))/(np.log(bx) - np.log(ax))
ay = 1E-20 # [ay,by] is range of vertical axis
by = 1E-10
def fy(y):
return (np.log(y) - np.log(ay))/(np.log(by) - np.log(ay))
plot1 = plt.subplot(111)
plt.xscale('log')
plt.yscale('log')
plt.xlim(ax, bx)
plt.ylim(ay, by)
# transformed coordinates for arrow from (1E-10,1E-18) to (1E-4,1E-16)
x0 = fx(1E-10)
y0 = fy(1E-18)
x1 = fx(1E-4) - fx(1E-10)
y1 = fy(1E-16) - fy(1E-18)
plt.arrow(
x0, y0, x1, y1, # input transformed arrow coordinates
transform = plot1.transAxes, # tell matplotlib to use axes coordinates
facecolor = 'black',
length_includes_head=True
)
plt.grid(True)
plt.savefig('test.pdf')
$ provincia =城市名称。例如 milano ,如果http://www.website.com/images/milano.png不存在,我需要默认图像。如果存在,我需要这个图像。 任何人都可以帮助我吗?
答案 0 :(得分:0)
您可以使用此API:https://placehold.it/
示例:强>
<?php
$immagine_e = $_GET('http://www.website.com/images/'.$provincia'.png');
?>
<?php if ($immagine_e ==''){ ?>
<img src="https://placehold.it/350x150" class="margin-top-negative-70" alt="">
<?php } else { ?>
<img src="$immagine_e" class="margin-top-negative-70" alt="">
<?php } ?>
答案 1 :(得分:0)
这是您可以考虑的另一种解决方案。
$immagine_e = "http://www.website.com/images/$provincia.png";
$size = getimagesize($immagine_e);
if(isset($size[0]))
{
?>
<img src="$immagine_e" class="margin-top-negative-70" alt="">
<?
}else
{
?>
<img src="images/default.png" class="margin-top-negative-70" alt="">
<?
}