Q:

Adding images to product gallery -- Can't get media type to stick

I'm trying to add base, small, and thumbnail images to all of my products programmatically. The images are importing fine and being placed into /media/catalog/product as expected, but only 'Base Image' is selected when I view the product's images in the Magento backend.
I'm looping through each product in the catalog and running:
$mediaArray = array('thumbnail', 'small_image', 'image');   
$file = Mage::getBaseDir('media') . DS . 'import' .  trim($imageName);
try {
    $product->addImageToMediaGallery($file, $mediaArray, false, false);
    $product->save();
} catch (Exception $e) {
    echo $e->getMessage();
}

All of my image names start with a slash, and the images themselves are uploaded to /media/import. I'm attempting to mimic what the Mage product importer would do.
Any insight on why the small and thumbnail flags aren't being set would be greatly appreciated!

A:

Here is my own import code I used, that worked:
        // Add three image sizes to media gallery
        $mediaArray = array(
            'thumbnail'   => $oscProduct['products_image'],
            'small_image' => $oscProduct['products_mediumimage'],
            'image'       => $oscProduct['products_largeimage'],
        );

        // Remove unset images, add image to gallery if exists
        $importDir = Mage::getBaseDir('media') . DS . 'import/';

        foreach ( $mediaArray as $imageType => $fileName ) {
            $filePath = $importDir . $fileName;
            if ( file_exists($filePath) ) {
                try {
                    $product->addImageToMediaGallery($filePath, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";
            }
        }

Note $imageType is not an array, as your counterpart is.
Edit: You only want one image, and to set it as each type. After you save the product with $product->save(), try something like the following, assuming you have set the image type already:
$product->setThumbnail($product->getImage());
$product->setSmallImage($product->getImage());
$product->save();

