php 自动更新数据
To gain access to image metadata, you must load the file a server-side language. You can dynamically load the image in any way you please, including the methods I’ve shown in the past: the only condition is that PHP must be able to address the image file. Here, to make things clearer, I’ll use the image’s filename directly, rather than using a variable.
要访问图像元数据,您必须以服务器端语言加载文件。 您可以动态加载图像中的任何方式,您请,包括方法 ,我表示在过去的 :唯一的条件是,必须PHP能够处理图像文件。 在这里,为了使事情更清楚,我将直接使用图像的文件名,而不是使用变量。
From PHP’s perspective, IPTC metatags are secreted in the getimagesize
function. To make this information visible, we’ll create an array, fill it with getimagesize
data, and then use print_r
to make the keys of the array visible (I’ll wrap the print_r
in a <pre>
to format the array in a more presentable fashion:
从PHP的角度来看,IPTC元标记隐藏在getimagesize
函数中。 为了使此信息可见,我们将创建一个数组,用getimagesize
数据填充它,然后使用print_r
使该数组的键可见(我将print_r
包裹在<pre>
以对数组进行格式化。时髦的:
<?php $picinfo = array();
getimagesize('lighthouse_spain.jpg', $picinfo);
echo "<pre>";
print_r(array_keys($picinfo));
echo "</pre>"; ?>
The result of our print_r
will be somewhat obfuscated:
我们的print_r
的结果会有些模糊:
Array (
[0] => APP1
[1] => APP12
[2] => APP13
[3] => APP14
)
What you need to know is that the ITPC information lies within that third slot. “APP13” is an array unto itself; we could see its contents by using the itpcparse
function, and then printing the results out:
您需要知道的是,ITPC信息位于该第三个插槽内。 “ APP13”本身就是一个数组; 我们可以使用itpcparse
函数查看其内容,然后将结果打印出来:
if(isset($picinfo['APP13'])) {
$iptc = iptcparse($picinfo['APP13']);
print_r($iptc);
}
Now we’re getting somewhere. I’ll show just the relevant parts of the array:
现在我们到了某个地方。 我将仅显示数组的相关部分:
Array (
[2#105] => Array
([0] => Lighthouse at dusk in Asturias, Spain)
[2#080] => Array
([0] => René González)
[2#055] => Array
([0] => 20111016)
[2#090] => Array
([0] => Asturias)
[2#101] => Array
([0] => Spain)
)
This matches the metadata entered into the image in PhotoShop. Now we just need some PHP to extract it and put the results into variables, with a little help from the substr()
, date
, and mktime()
functions:
这与在PhotoShop中输入图像的元数据匹配。 现在,我们只需要一些PHP即可将其提取并将结果放入变量中,而substr()
, date
和mktime()
函数则提供了一些帮助:
if(isset($picinfo['APP13'])) {
$iptc = iptcparse($picinfo["APP13"]);
if (is_array($iptc)) {
$description = $iptc['2#105'][0];
$time = $iptc['2#055'][0];
$year = substr($time, 0, 4);
$month = substr($time, 4, 2);
$day = substr($time, -2);
$datetaken = date('l F jS Y', mktime(0, 0, 0, $month, $day, $year));
$city = $iptc["2#090"][0];
$country = $iptc["2#101"][0];
$creator = $iptc["2#080"][0];
}
Now we can caption any image read this way with the following information. (Here I’ve gone back to assuming we can address the image itself with a variable.)
现在,我们可以使用以下信息为任何以这种方式读取的图像加标题。 (在这里,我回到假定我们可以使用变量解决图像本身的问题。)
<figure>
<img src="<?=$image?>px;" alt="<?=$description?>" >
<figcaption>
<?=$description?>. Taken by <?=$creator?> on <?=$datetaken?>
</figcaption>
</figure>
想走得更远吗? (Want To Go Further?)
Full CMS systems such as ZenPhoto have this same functionality, but if you only need to create captions for a few dozen images while retaining greater control of your site, I’d suggest this technique is the route to go.
完整的CMS系统(例如ZenPhoto)具有相同的功能,但是如果您只需要为几十个图像创建标题,同时又可以更好地控制网站,那么我建议您采用这种方法。
翻译自: https://thenewcode.com/410/Automatic-HTML5-Image-Captioning-With-metadata-and-PHP
php 自动更新数据