[英]Using PHP Sub String to Link Thumbnails
So I am making this simple gallery that fetches images from a directory. 因此,我正在制作一个简单的库,用于从目录中获取图像。 All images have names like tree_th.jpg and I would like to use sub string (I assume this is correct) to slice out the _th and link out to just tree.jpg 所有图像的名称都类似于tree_th.jpg,我想使用子字符串(我认为这是正确的)来切出_th并链接到仅tree.jpg
<?
$imagetypes = array("image/jpeg", "image/gif");
function getImages($dir) {
global $imagetypes;
$dir = "img/";
$retval = array();
if(substr($dir, -1) != "/") $dir .= "/";
$fulldir = "/$dir";
//echo $fulldir;
$d = @dir($fulldir) or die("");
while(false !== ($entry = $d->read())) {
if($entry[0] == ".") continue;
$f = escapeshellarg("$fulldir$entry");
$mimetype = trim(`file -bi $f`);
foreach($imagetypes as $valid_type) {
if(preg_match("@^{$valid_type}@", $mimetype)) {
$retval[] = array( 'file' => "$dir$entry", 'size' => getimagesize("$fulldir$entry") );
break;
}
}
}
$d->close(); return $retval;
}
$thumbs = getImages("img");
foreach($thumbs as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][1]} alt=\"\">\n";
}
?>
One way you can do this is with str_replace() function in PHP. 一种实现方法是使用PHP中的str_replace()函数。
$large_image_path = str_replace("_th","",$thumb_path);
The substring command you would have to first evaluate the position of the _th, then reassemble the string with a length and concat so I think str_replace would be easier for you. 您必须首先使用substring命令评估_th的位置,然后使用长度和concat重新组装字符串,所以我认为str_replace对您而言会更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.