如何在PHP中获取图像的标题或其他属性

时间:2013-05-01 19:03:52

标签: php javascript magento

我正在为网站http://mytempsite.net/gotie/mixandmatch

构建自定义衬衫制作工具

我设置的是第一步,他们将从12种不同的衬衫中选择衬衫颜色,然后继续下一步,他们将能够选择领带。我需要能够将变量传递给下一页,告诉它只能拉上带有绑带的图像,例如红色衬衫。

我这样做的想法是在图像alt或title标签处设置该属性,然后从正在显示的当前图像中获取该属性。

我需要知道的是怎么做?

我尝试使用此代码作为开始

<?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>";

}?>

我希望我正确地说出这个问题,不要过于本地化。如果是这样,我会改写它。

2 个答案:

答案 0 :(得分:0)

我相信$ someVar = get_field('image file')应返回图像中所有标记数据的数组。然后,您可以从数组中访问数据(即$ 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;
}

答案 1 :(得分:0)

我最终做的是当点击缩略图图像时,隐藏的文本框获取该衬衫的product_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>