如何在PHP中获取图像的标题或其他属性
我正在为一个网站构建一个定制衬衫生成器 我设置的是第一步,他们将从12件不同的衬衫中选择一种衬衫颜色,然后进入下一步,他们将能够选择领带。我需要能够将一个变量传递到下一个页面,告诉它只提取带有领带的图像,例如红色衬衫 我这样做的想法是在图像alt或title标记处设置该属性,然后从当前显示的图像中获取该属性 我需要知道的是怎么做 我试着用这个代码作为开始如何在PHP中获取图像的标题或其他属性,php,javascript,magento,Php,Javascript,Magento,我正在为一个网站构建一个定制衬衫生成器 我设置的是第一步,他们将从12件不同的衬衫中选择一种衬衫颜色,然后进入下一步,他们将能够选择领带。我需要能够将一个变量传递到下一个页面,告诉它只提取带有领带的图像,例如红色衬衫 我这样做的想法是在图像alt或title标记处设置该属性,然后从当前显示的图像中获取该属性 我需要知道的是怎么做 我试着用这个代码作为开始 <?php $url=$this->helper('core/url')->getCurrentUrl();;
<?php
$url=$this->helper('core/url')->getCurrentUrl();;
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$divs = $doc->getElementByID('loadarea');
foreach ($divs as $div) {
echo "Found the loadarea div <br />";
}
?>
但它不起作用,也导致我的页面加载非常缓慢
以防万一,这里是缩略图的代码
<?php
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $prod) {
$product = Mage::getModel('catalog/product')->load($prod->getId());
$pro_title = $product->getName();
$img = $product->getImageUrl();
echo "<a href='".$img."' title='".$pro_title."' rel='enlargeimage' rev='targetdiv:loadarea,enabletitle:no,trigger:click,preload:none,fx:fade'><img src='".$img."' width='100px'/></a>";
}?>
我希望这个问题的措辞正确,不要过于本地化。如果是这样,我将改写它 我认为$someVar=get_字段(“图像文件”)应该返回图像中所有标记数据的数组。然后可以从数组中访问数据(即$someVar['alt'])
这里有一个更简单的解决方案,不需要将图像制作成对象
$html=file_get_contents("URL OF YOUR SITE");
$doc = new DOMDocument();
@$doc -> loadHTML($html);
// Add a class to your big image to identify it (ex. selected)
$tags = getElementsByClassName($doc, 'selected');
foreach ($tags as $tag) {
echo $tag->getAttribute('alt');
}
// Not my function but its very useful. I'll track down where I got it
// and add source later.
function getElementsByClassName(DOMDocument $DOMDocument, $ClassName) {
$Elements = $DOMDocument -> getElementsByTagName("*");
$Matched = array();
foreach($Elements as $node)
{
if( ! $node -> hasAttributes())
continue;
$classAttribute = $node -> attributes -> getNamedItem('class');
if( ! $classAttribute)
continue;
$classes = explode(' ', $classAttribute -> nodeValue);
if(in_array($ClassName, $classes))
$Matched[] = $node;
}
return $Matched;
}
我最后做的是,当点击缩略图图像时,一个隐藏的文本框会获取该衬衫的产品id,然后当你转到下一页时,它会自动提取该产品的产品图像。我已经为我的整个表单插入了代码,以防将来有人需要这样做:) 快乐编码
<form id="GoTie_Builder" method="POST" action="/gotie/mixandmatch/tie">
<script>
function changeInput(pro_id)
{
var my_form = document.getElementById('GoTie_Builder');
my_form.shirt_color.value = pro_id;
}
function changePattern(pattern)
{
var my_div = document.getElementById('shirt_zoom');
my_div.innerHTML = '<img src="http://www.tuxedojunction.com/Content/Products/Vests/LegacyBlueVelvet_s_1.jpg" />';
}
</script>
<div id="shirt_zoom" style="width:300px; height:100px;">
<img src="http://www.tuxedojunction.com/Content/Products/Vests/LegacyBlueVelvet_s_1.jpg" />
</div>
<div class="Builder_thumbnails" style="float:left;">
<?php
$cat_id = 8;
$products = Mage::getModel('catalog/category')->load($cat_id)->getProductCollection();
echo '<input id="shirt_color" type="text" name="shirt_color" value="0">';
foreach($products as $prod) {
$product = Mage::getModel('catalog/product')->load($prod->getId());
$pattern = $this->helper('catalog/image')->init($product, 'thumbnail');
//echo "<img onclick='changeInput($pro_id)' class='product_thumbnail' src='".$pattern."' alt='".$pro_id."' width='100px'/>";
$pro_id = $product->getId();
$pro_title = $product->getName();
$img = $product->getImageUrl();
$input_id = "shirt_color";
echo "<a href='".$img."' title='".$pro_title."' rel='enlargeimage' rev='targetdiv:loadarea,enabletitle:no,trigger:click,preload:none,fx:fade'><img onclick='changeInput($pro_id)' class='product_thumbnail' src='".$this->helper('catalog/image')->init($product, 'thumbnail')."' alt='".$pro_id."' width='100px' height='100px'/></a>";
}?>
</div>
<div id="loadarea" style="width:300px;top: 0px;right: 0px;float: right;position: relative;"><img src="http://cdn4.blackenterprise.com/wp-content/blogs.dir/1/files/2011/07/White-Shirt-620x480.jpg" width="500px;"/>
</div>
<input type="submit" value="Choose a Tie" />
</form>
功能更改输入(pro_id)
{
var my_form=document.getElementById('GoTie_Builder');
my_form.shirt_color.value=pro_id;
}
功能更改模式(模式)
{
var my_div=document.getElementById('shirt_zoom');
my_div.innerHTML='';
}
但是,如果它只获取活动大照片的alt,我该如何处理?你看过这个网站了吗?你知道我指的是什么吗?是的,对不起,我的错。我将用一个更简单的解决方案编辑我的原始帖子。每次大图片被改变时,这会自动更新吗?这很好,可能会奏效,但我走了另一条路。我将很快发布答案。