Magento – How to get magento product image url for a thumbnail in an array

blocksimagemagento-1.7module

I have a data module that gets information for a product and its attributes associated with it like color, sizes etc. In here there is an image that needs to be taken and put into an array in particular the thumbnail images.

Here is the code that I have put together/grabbed:

if (!in_array($size_name, $available_sizes)) {

    $available_sizes[] = array(
        'id'                    => $asc_product->getId(), // Product ID
        'color'                 => $this->getColorName($asc_product), // Color name used for reference?
        'size'                  => $size_name, // Ring size
        'product_type'          => $attribute_set_name,
        'product_image'         => $asc_product->getImageUrl(),
        'product_image'         => $imageThumbnail,
        'product_image_label'   => 'labelle' // Main product image
    );
}

Inside the array I need to get the thumbnail of that product. This is using 'product_image' => $asc_product->getImageUrl(),

Currently I use this in most of my product pages to get the image url for each image size:

$imageThumbnail = Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getThumbnail() );

Does anyone know how I can get the correct image size the in the array at the top?

====== Added the data helper file content here =======

<?php


class Mawi_Products_Helper_Data extends Mage_Core_Helper_Abstract {


    // --------------------------------------------------------------------

    /**
     * Tests to see if a product has a comparison image
     */
    function hasComparisonImage($_product)
    {
        return ((strlen($_product->getComparisonImage()) > 0) && ($_product->getComparisonImage() != "no_selection")) ? true : false;
    }


    // --------------------------------------------------------------------

    /**
     * Gets all ring sizes
     */
    function getAllRingSizes()
    {   
        $all_ring_sizes     = array();

        $get_ring_sizes  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'ring_size_uk'); 

        foreach ($get_ring_sizes->getSource()->getAllOptions(true, true) as $ring_size) {

            if (strlen($ring_size['label']) > 0) {

                $all_ring_sizes[] = $ring_size['label'];

            }

        }

        return (count($all_ring_sizes) > 0) ? $all_ring_sizes : false;

    }

    // --------------------------------------------------------------------

    /**
     * Gets all bracelet sizes
     */
    function getAllBraceletSizes()
    {
        $get_bracelet_sizes  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'bracelet_size_uk'); 

        foreach ($get_bracelet_sizes->getSource()->getAllOptions(true, true) as $bracelet_size) {

            if (strlen($bracelet_size['label']) > 0) {

                // Remove the "One Size" bracelet size
                if ($bracelet_size['label'] !== 'One Size') {

                  $all_bracelet_sizes[] = $bracelet_size['label'];

                }

            }

        }

        return (count($all_bracelet_sizes) > 0) ? $all_bracelet_sizes : false;

    }

    // --------------------------------------------------------------------

    /**
     * Gets all sizes for the product type
     */
    function getAllSizes($_product)
    {

        // Attr set name 
        $attribute_set_name         = $this->getAttributeSetName($_product);

        switch ($attribute_set_name) {
          case 'rings':
            $all_sizes    = $this->getAllRingSizes();
            break;

          case 'bracelets':
            $all_sizes    = $this->getAllBraceletSizes();
            break;

          default:
            $all_sizes    = false;
            break;
        }

        return $all_sizes;

    }



    // --------------------------------------------------------------------

    /**
     * Gets available sizes for a given product
     */
    function getAvailableSizes($available_sizes, $asc_product, $attribute_set_name)
    {   

        if ($attribute_set_name == 'bracelets') {

            $attr          = $asc_product->getResource()->getAttribute("bracelet_size_uk");
            $size_name     = $attr->getSource()->getOptionText($asc_product->getBraceletSizeUk());


        } else if ($attribute_set_name == 'rings') {

            $attr          = $asc_product->getResource()->getAttribute("ring_size_uk");
            $size_name     = $attr->getSource()->getOptionText($asc_product->getRingSizeUk());

        }


        if (!in_array($size_name, $available_sizes)) {

            $available_sizes[] = array(
                'id'                      => $asc_product->getId(), // Product ID
                'color'                   => $this->getColorName($asc_product), // Color name used for reference?
                'size'                    => $size_name, // Ring size
                'product_type'            => $attribute_set_name,
                'product_image'           => $asc_product->getImageUrl(),
                'product_image_label'     => 'labelle' // Main product image
            );

        }

        return $available_sizes;
    }



    // --------------------------------------------------------------------

    /**
     * Gets the available colours for a product
     */
    function getAvailableColors($available_colors, $asc_product)
    {

        $color_name         = $this->getColorName($asc_product);

        if (!in_array($color_name, $available_colors)) {

            $available_colors[] = $color_name;

        }

        return (count($available_colors) > 0) ? $available_colors : false;

    }



    // --------------------------------------------------------------------

    /**
     * Gets a colour name string for a particular simple product
     */
    function getColorName($asc_product)
    {

        $color_attr         = $asc_product->getResource()->getAttribute("color");
        $color_name         = $color_attr->getSource()->getOptionText($asc_product->getColor());

        return $color_name;

    }

    // --------------------------------------------------------------------

    /**
     * Gets an attribute set name for a product
     */
    function getAttributeSetName($_product)
    {

        $attribute_set_model    = Mage::getModel("eav/entity_attribute_set");

        $attribute_set_model->load($_product->getAttributeSetId());

        $attribute_set_name     = strtolower($attribute_set_model->getAttributeSetName());

        return $attribute_set_name;

    }










}

======= end =======

I am struggling to think of what it is, even though it is a simple idea.

Cheers

Best Answer

Do you mean resizing it in the array? In that case you can use the Magento catalog image helper.

'product_image' => (string)Mage::helper('catalog/image')->init($asc_product, 'thumbnail')->resize(100);

This would return you the url to the resized image.

Related Topic