Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. <?php
  2. $halfPath='./media/catalog/product';
  3. $user="username"; //username for SOAPv2 api
  4. $pass="password"; //password for above user
  5. $products = array(20,21,22); //array of product IDs to be converted
  6. ini_set('display_errors',E_ALL);
  7. error_reporting(E_ALL);
  8. define('MAGENTO_ROOT', getcwd());
  9. $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
  10. require_once $mageFilename;
  11. Mage::app();
  12. $baseurl=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
  13. $client = new SoapClient($baseurl.'api/v2_soap/?wsdl');
  14. $sessionId = $client->login($user,$pass);
  15.  
  16. $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
  17. function kariobhai($jagah)
  18. {
  19. global $halfPath;
  20. $input = imagecreatefrompng($halfPath.$jagah);
  21. $width=imagesx($input);
  22. $height=imagesy($input);
  23. $output = imagecreatetruecolor($width, $height);
  24. $white = imagecolorallocate($output, 255, 255, 255);
  25. imagefilledrectangle($output, 0, 0, $width, $height, $white);
  26. imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
  27. ob_start();
  28. imagejpeg($output);
  29. $img_content = ob_get_clean();
  30. imagedestroy($output);
  31. return base64_encode($img_content);
  32. }
  33.  
  34. foreach($products as $prodID){
  35. $items = $mediaApi->items($prodID);
  36. foreach($items as $item) {
  37. if(substr($item['file'],-3)=='png'){
  38. $direction=kariobhai($item['file']);
  39. $str = preg_replace('/.*\/(.*?)\.png$/','$1', $item['file']);
  40. $newFile = array(
  41. 'name'=>$str,
  42. 'content' => $direction,
  43. 'mime' => 'image/jpeg'
  44. );
  45. $result = $client->catalogProductAttributeMediaCreate(
  46. $sessionId,
  47. $prodID,
  48. array( 'file'=>$newFile,'label' => $item['label'], 'position' => $item['position'], 'types' => $item['type'], 'exclude' => $item['exclude'])
  49. );
  50. if($result) $client->catalogProductAttributeMediaRemove($sessionId, $prodID, $item['file']);
  51. }
  52. }
  53. }
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement