Created
July 30, 2014 20:26
-
-
Save brankoajzele/38fa394d0b276aa5dab8 to your computer and use it in GitHub Desktop.
magento-csv-product-generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Magento CSV products generator */ | |
if(php_sapi_name() != 'cli' && !empty($_SERVER['REMOTE_ADDR'])) { | |
exit('Only shell execution execution is allowed!'); | |
} | |
require_once 'faker/autoload.php'; | |
$_total = (empty($argv[1])) ? 10 : $argv[1]; /* Total products to add to CSV */ | |
$_category = (empty($argv[2])) ? 'Default Category' : $argv[2]; | |
$file = sprintf('magento-products-%s.csv', time()); | |
$fp = fopen($file, 'wb'); | |
fputcsv($fp, array('name', 'description', 'short_description', 'sku', '_type', 'weight', '_product_websites', '_store', 'status', 'visibility', 'price', 'tax_class_id', '_attribute_set', 'qty', 'is_in_stock', '_category', 'image', 'thumbnail', 'small_image', 'color', 'manufacture_country', 'author')); | |
$faker = Faker\Factory::create(); /* each call to $faker->someProperty yields a different (random) result */ | |
$color = array('Blue', 'Brown', 'Green', 'Orange', 'Purple', 'Red', 'Yellow'); | |
$country = array('Australia', 'Belgium', 'Croatia', 'Cyprus', 'Estonia', 'Ireland', 'Switzerland', 'United Kingdom'); | |
$author = array(); | |
for ($i=1; $i<=50; $i++) { | |
$author[] = $faker->name; | |
} | |
$_category = array('Phone', 'Phone/Samsung', 'Tablet', 'Tablet/Apple', 'Tablet/LG'); | |
for ($i=1; $i<=$_total; $i++) { | |
$sku = $faker->ean13(); | |
$short_description = $faker->realText(200); | |
imageCenterString(800, 600, sprintf('SKU: %s, SHORT_DESCRIPTION: %s', $sku, $short_description), 5, sprintf('C:/Bitnami/wampstack-5.4.30-0/apps/magento1901/htdocs/media/import/%s.jpg', $sku)); | |
fputcsv($fp, array( | |
//rtrim($faker->sentence(rand(2, 5), true), '.'), /* name */ | |
//rtrim(rtrim($faker->realText(40), '.'), ','), /* name */ | |
$faker->catchPhrase, | |
$faker->realText(400), /* description */ | |
$short_description, /* short_description */ | |
$sku, /* sku */ | |
'simple', /* _type */ | |
$faker->randomFloat(4, 0.1, 1), /* weight */ | |
'base', /* _product_websites */ | |
'default', /* _store */ | |
1, /* status => Enabled */ | |
4, /* visibility => Catalog, Search */ | |
$faker->randomFloat(4, 20, 500), /* price */ | |
2, /* tax_class_id => Read from Magento admin */ | |
'Default', /* _attribute_set */ | |
$faker->numberBetween(50, 1500), /* qty */ | |
1, /* is_in_stock */ | |
$_category[array_rand($_category)], /* _category */ | |
$sku.'.jpg', /* image */ | |
$sku.'.jpg', /* thumbnail */ | |
$sku.'.jpg', /* small_image */ | |
$color[array_rand($color)], /* color */ | |
$country[array_rand($country)], /* manufacture_country */ | |
$author[array_rand($author)], /* author */ | |
)); | |
} | |
fclose($fp); | |
////////////////////////// | |
function imageCenterString( $imgw, $imgh, $image_text = '', $text_size=5, $path) | |
{ | |
$im = imagecreate( $imgw, $imgh ); | |
//BG color | |
imagecolorallocate($im, 51, 153, 204); /* pastel blue */ | |
$textcolor = imagecolorallocate($im, 0, 0, 0); | |
$t_h = $t_w = $t_x = $t_y = 0; | |
$base_w =9; $base_h = 16; | |
$m = 0.88; | |
switch ( $text_size ) | |
{ | |
case 1: $t_w = $base_w*pow(($m*.98),4); | |
$t_h = $base_h*pow(($m*.98),4); | |
break; | |
case 2: $t_w = $base_w*pow($m,3); | |
$t_h = $base_h*pow($m,3); | |
break; | |
case 3: $t_w = $base_w*pow($m,2); | |
$t_h = $base_h*pow($m,2); | |
break; | |
case 4: $t_w = $base_w*$m; | |
$t_h = $base_h*$m; | |
break; | |
case 5: $t_w = $base_w; | |
$t_h = $base_h; | |
break; | |
default: | |
if ( $text_size >= 5 ) // set to 5 | |
{ $t_w = $base_w; $t_h = $base_h; } | |
if ( $text_size < 5 ) // set to 1 | |
{ | |
$t_w = $base_w*pow(($m*.98),4); | |
$t_h = $base_h*pow(($m*.98),4); | |
} | |
break; | |
} | |
$text_array = array(); | |
$max = floor($imgw/$t_w); | |
for( $i=0; strlen($image_text) > 0; $i += $max) | |
{ | |
array_push($text_array, substr($image_text,0,$max)); | |
if ( strlen($image_text) >= $max ) | |
{ $image_text = substr($image_text,$max); } | |
else | |
{ $image_text = ''; } | |
} | |
$t_y = ($imgh/2) - ($t_h*count($text_array)/2); | |
foreach ( $text_array as $text ) | |
{ | |
$t_x = ($imgw/2)-($t_w*strlen($text)/2); | |
imagestring($im, $text_size, $t_x, $t_y, | |
$text, $textcolor); | |
$t_y += $t_h; | |
} | |
imagejpeg($im, $path, 100); | |
} | |
////////////////////////// | |
exit('All done...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment