Magento – How to get product images based on image type

magento-1.9product-imagessmall-image

I have uploaded several images for a product.

Now I want to get only non-exluded images of type 'small_image'.

I have this code:

$mediaGalleryImages =  Mage::getModel('catalog/product')->load(1749)->getMediaGalleryImages('small_image');
                                        Zend_Debug::dump($mediaGalleryImages->toArray());exit;

output:

 array(2) {
  ["totalRecords"] => int(7)
  ["items"] => array(7) {
    [0] => array(11) {
      ["value_id"] => string(5) "12196"
      ["file"] => string(41) "/c/u/custom-leather-sofa-holly-hill-3.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(1) "1"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(1) "1"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(91) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill-3.jpg"
      ["id"] => string(5) "12196"
      ["path"] => string(88) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill-3.jpg"
    }
    [1] => array(11) {
      ["value_id"] => string(5) "12194"
      ["file"] => string(41) "/c/u/custom-leather-sofa-holly-hill_1.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(1) "2"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(1) "2"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(91) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_1.jpg"
      ["id"] => string(5) "12194"
      ["path"] => string(88) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_1.jpg"
    }
    [2] => array(11) {
      ["value_id"] => string(4) "9182"
      ["file"] => string(42) "/c/u/custom-leather-sofa-holly-hill_2_.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(1) "3"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(1) "3"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(92) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_2_.jpg"
      ["id"] => string(4) "9182"
      ["path"] => string(89) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_2_.jpg"
    }
    [3] => array(11) {
      ["value_id"] => string(5) "13615"
      ["file"] => string(41) "/c/u/custom-leather-sofa-holly-hill_2.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(2) "10"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(2) "10"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(91) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_2.jpg"
      ["id"] => string(5) "13615"
      ["path"] => string(88) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_2.jpg"
    }
    [4] => array(11) {
      ["value_id"] => string(5) "12195"
      ["file"] => string(41) "/c/u/custom-leather-sofa-holly-hill-2.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(2) "11"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(2) "11"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(91) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill-2.jpg"
      ["id"] => string(5) "12195"
      ["path"] => string(88) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill-2.jpg"
    }
    [5] => array(11) {
      ["value_id"] => string(4) "9183"
      ["file"] => string(42) "/c/u/custom-leather-sofa-holly-hill_1_.jpg"
      ["label"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position"] => string(2) "15"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(41) "Custom Upholstered Fabric Sofa Holly Hill"
      ["position_default"] => string(2) "15"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(92) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_1_.jpg"
      ["id"] => string(4) "9183"
      ["path"] => string(89) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_1_.jpg"
    }
    [6] => array(11) {
      ["value_id"] => string(5) "14475"
      ["file"] => string(41) "/c/u/custom-leather-sofa-holly-hill_3.jpg"
      ["label"] => string(46) "Custom Upholstered Fabric Sofa Holly Hill test"
      ["position"] => string(2) "20"
      ["disabled"] => string(1) "0"
      ["label_default"] => string(46) "Custom Upholstered Fabric Sofa Holly Hill test"
      ["position_default"] => string(2) "20"
      ["disabled_default"] => string(1) "0"
      ["url"] => string(91) "http://localhost/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_3.jpg"
      ["id"] => string(5) "14475"
      ["path"] => string(88) "/var/www/html/silvercoast/media/catalog/product/c/u/custom-leather-sofa-holly-hill_3.jpg"
    }
  }
}

I have also check this post:
How to get the image type in media image collection?

http://www.magecoders.com/blog/2013/05/programmatically-exclude-product-images-in-magento/

Best Answer

finally I found the answer:

$secondimage = "";
$smallimage = "";
$smallimageLabel = "";
$secondimageLabel = "";
$product = Mage::getModel('catalog/product')->load($product->getId());
$getSecondSmallImage = $product->getSecondsmallimage();
$getSmallImage = $product->getSmallImage();

foreach ($product->getMediaGalleryImages() as $_image) {

    $imageFile = $_image->getFile();
    if ($imageFile == $getSecondSmallImage && "" == $secondimage) {
        $secondimage = (string)Mage::helper('catalog/image')
            ->init($product, 'secondsmallimage', $imageFile)
            ->keepFrame(false)->resize(573, 282);
        $secondimageLabel = $_image->getLabel();
    }

    if ($imageFile == $getSmallImage && "" == $smallimage) {
        $smallimage = (string)Mage::helper('catalog/image')
            ->init($product, 'small_image', $imageFile)
            ->keepFrame(false)->resize(573, 282);
        $smallimageLabel = $_image->getLabel();
    }

}
Related Topic