Skip to content

Instantly share code, notes, and snippets.

@amitjambusaria
Created December 15, 2016 20:53
Show Gist options
  • Save amitjambusaria/3e4939357ca9e54f89f00a402fc8d2f6 to your computer and use it in GitHub Desktop.
Save amitjambusaria/3e4939357ca9e54f89f00a402fc8d2f6 to your computer and use it in GitHub Desktop.
Php CSV parser
<?php
require_once INCLUDE_PATH . '/admin/app_includes/global_admin_header.php';
use WF\Admin\Models\Color_Thief\Color_Thief_Model;
use WF\Shared\PDO;
ini_set('max_execution_time', 3000); //3000 seconds = 50 minutes
echo 'yay ';
$start = microtime(true) . ' ';
// Get image urls and other information from csv
$csv_data = [];
if (($handle = fopen("accent_pillows_1227_2.csv", "r")) !== FALSE) {
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$components = explode('/', $data[6]);
$ma_id = $components[4];
$ire_id = $data[1];
$url = 'http://img.wfrcdn.com/lf/49/hash/' . $ma_id . '/' . $ire_id . '/1/' . $ire_id . '.jpg';
$row = [
'pr_sku' => $data[0],
'ire_id' => intval($data[1]),
'pi_id' => !empty($data[2]) ? intval($data[2]) : 0,
'class_id' => intval($data[3]),
'image_url' => $url
];
$csv_data[] = $row;
}
fclose($handle);
}
$call_time_array = [];
// Compute dominant color and get the palette
foreach ($csv_data as &$row) {
if (!data_exists($row['ire_id'])) {
$call = microtime(true);
$colors = Color_Thief_Model::getPalette($row['image_url'], 14, 10);
$call_end = microtime(true);
$call_time = $call_end - $call;
echo $call_time . ' ';
$call_time_array[] = $call_time;
if (!empty($colors)) {
$row['dominant_color'] = !empty($colors[0]) ? rgb2hex($colors[0]) : '';
// Grab palette colors
for ($i = 1; $i < 11; $i++) {
$row['palette_' . $i] = !empty($colors[$i]) ? rgb2hex($colors[$i]) : '';
}
insert_to_table($row);
}
}
}
echo ' Actual time for palette ---> ' ;
echo array_sum($call_time_array) / count($call_time_array);
echo " Overall time -----> " ;
echo microtime(true) - $start;
// At this point we have everything. Lets start populating the db
function insert_to_table($row) {
$sql = '
INSERT INTO csn_solr.dbo.tblColorThiefColorData
(IreID, PrSKU, PiID, ClID, ImageUrl, DominantColor, Palette1, Palette2, Palette3, Palette4, Palette5, Palette6, Palette7, Palette8, Palette9, Palette10)
VALUES (:ire_id, :pr_sku, :pi_id, :class_id, :image_url, :dominant_color, :palette_1, :palette_2, :palette_3, :palette_4, :palette_5, :palette_6, :palette_7, :palette_8, :palette_9, :palette_10);
';
$statement = PDO::new_statement('SOLR', $sql);
$statement->bindValue(':ire_id', $row['ire_id'], PDO::PARAM_INT);
$statement->bindValue(':pr_sku', $row['pr_sku'], PDO::PARAM_STR);
$statement->bindValue(':pi_id', $row['pi_id'], PDO::PARAM_INT);
$statement->bindValue(':class_id', $row['class_id'], PDO::PARAM_INT);
$statement->bindValue(':image_url', $row['image_url'], PDO::PARAM_STR);
$statement->bindValue(':dominant_color', $row['dominant_color'], PDO::PARAM_STR);
$statement->bindValue(':palette_1', $row['palette_1'], PDO::PARAM_STR);
$statement->bindValue(':palette_2', $row['palette_2'], PDO::PARAM_STR);
$statement->bindValue(':palette_3', $row['palette_3'], PDO::PARAM_STR);
$statement->bindValue(':palette_4', $row['palette_4'], PDO::PARAM_STR);
$statement->bindValue(':palette_5', $row['palette_5'], PDO::PARAM_STR);
$statement->bindValue(':palette_6', $row['palette_6'], PDO::PARAM_STR);
$statement->bindValue(':palette_7', $row['palette_7'], PDO::PARAM_STR);
$statement->bindValue(':palette_8', $row['palette_8'], PDO::PARAM_STR);
$statement->bindValue(':palette_9', $row['palette_9'], PDO::PARAM_STR);
$statement->bindValue(':palette_10', $row['palette_10'], PDO::PARAM_STR);
$statement->execute();
}
function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
return $hex;
}
function data_exists($ire_id) {
$sql = 'SELECT TOP 1 * FROM csn_solr.dbo.tblColorThiefColorData WITH (NOLOCK)
WHERE IreID = :ire_id';
$statement = PDO::new_statement('SOLR', $sql);
$statement->bindValue(':ire_id', $ire_id, PDO::PARAM_INT);
$results = $statement->execute() ? $statement->fetchAll() : [];
return !empty($results) && !empty($results[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment