Skip to content

Instantly share code, notes, and snippets.

@harshvardhanmalpani
Last active January 5, 2017 18:13
Show Gist options
  • Save harshvardhanmalpani/5b5d63cc83060bf1b1ff2ce25dcc26fc to your computer and use it in GitHub Desktop.
Save harshvardhanmalpani/5b5d63cc83060bf1b1ff2ce25dcc26fc to your computer and use it in GitHub Desktop.
Converts all magento images from PNG to JPG
<?php
$halfPath='./media/catalog/product';
$user="username"; //username for SOAPv2 api
$pass="password"; //password for above user
$products = array(20,21,22); //array of product IDs to be converted
ini_set('display_errors',E_ALL);
error_reporting(E_ALL);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::app();
$baseurl=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$client = new SoapClient($baseurl.'api/v2_soap/?wsdl');
$sessionId = $client->login($user,$pass);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
function kariobhai($jagah)
{
global $halfPath;
$input = imagecreatefrompng($halfPath.$jagah);
$width=imagesx($input);
$height=imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
ob_start();
imagejpeg($output);
$img_content = ob_get_clean();
imagedestroy($output);
return base64_encode($img_content);
}
foreach($products as $prodID){
$items = $mediaApi->items($prodID);
foreach($items as $item) {
if(substr($item['file'],-3)=='png'){
$direction=kariobhai($item['file']);
$str = preg_replace('/.*\/(.*?)\.png$/','$1', $item['file']);
$newFile = array(
'name'=>$str,
'content' => $direction,
'mime' => 'image/jpeg'
);
$result = $client->catalogProductAttributeMediaCreate(
$sessionId,
$prodID,
array( 'file'=>$newFile,'label' => $item['label'], 'position' => $item['position'], 'types' => $item['type'], 'exclude' => $item['exclude'])
);
if($result) $client->catalogProductAttributeMediaRemove($sessionId, $prodID, $item['file']);
}
}
}
?>
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_id IN (85,86,87)
AND mgv.position = 1;
--DOUBLE check the attribute ids; Go into your attributes panel, find the image/small image/thumbnail attributes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment