Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active January 3, 2016 05:29
Show Gist options
  • Select an option

  • Save mklooss/8415988 to your computer and use it in GitHub Desktop.

Select an option

Save mklooss/8415988 to your computer and use it in GitHub Desktop.
Magento Copy Active Images
<?php
define('DS', DIRECTORY_SEPARATOR);
$xml = simplexml_load_file('app/etc/local.xml');
$mysql_user = (string)$xml->global->resources->default_setup->connection->username;
$mysql_password = (string)$xml->global->resources->default_setup->connection->password;
$mysql_dbname = (string)$xml->global->resources->default_setup->connection->dbname;
$mysql_host = (string)$xml->global->resources->default_setup->connection->host;
$prefix = (string)$xml->global->resources->db->table_prefix;
$mysqli = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_dbname);
$type = null;
$res = $mysqli->query("SELECT entity_type_id FROM {$prefix}eav_entity_type WHERE entity_type_code='catalog_product'");
while ($row = $res->fetch_object())
{
$type = (int)$row->entity_type_id;
}
// copy images from media_gallery
$res = $mysqli->query("SELECT value FROM {$prefix}catalog_product_entity_media_gallery ORDER BY value_id ASC");
while ($row = $res->fetch_object())
{
$source = 'media'.DS.'catalog'.DS.'product'.(string)$row->value;
$target = 'media'.DS.'catalog'.DS.'new_product'.(string)$row->value;
if(!file_exists($target))
{
$dir = dirname($target);
if(!is_dir($dir))
{
mkdir($dir ,0755 , true);
}
copy($source, $target);
echo $row->value."\n";
}
}
// copy images from attributes
$attrIds = array();
$res = $mysqli->query("SELECT attribute_id FROM `{$prefix}eav_attribute` WHERE `entity_type_id` = {$type} AND frontend_input = 'media_image'");
while ($row = $res->fetch_object())
{
$attrIds[] = (int)$row->attribute_id;
}
$res = $mysqli->query("SELECT value FROM `{$prefix}catalog_product_entity_varchar` WHERE `entity_type_id` = {$type} AND attribute_id IN (".implode(',', $attrIds).")");
while ($row = $res->fetch_object())
{
if($row->value == 'no_selection' || $row->value == '')
{
continue;
}
$source = 'media'.DS.'catalog'.DS.'product'.(string)$row->value;
$target = 'media'.DS.'catalog'.DS.'new_product'.(string)$row->value;
if(!file_exists($target))
{
$dir = dirname($target);
if(!is_dir($dir))
{
mkdir($dir ,0755 , true);
}
copy($source, $target);
echo $row->value."\n";
}
}
// copy placeholder
$source_dir = 'media'.DS.'catalog'.DS.'product'.DS.'placeholder';
$target_dir = 'media'.DS.'catalog'.DS.'new_product'.DS.'placeholder';
$dir_iterator = new RecursiveDirectoryIterator($source_dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if ($file->isFile()) {
$dir = str_replace($source_dir, $target_dir, $file->getPath());
$source = $file->getPath() . DS . $file->getFilename();
$target = $dir . DS . $file->getFilename();
if(!is_dir($dir))
{
mkdir($dir, 0755, true);
}
copy($source, $target);
echo str_replace('media'.DS.'catalog'.DS.'product', '', $source)."\n";
}
}
@riker09
Copy link

riker09 commented Jan 15, 2014

Wie wäre es mit einer Anleitung in Form einer Readme.md? Nur der Name macht es für andere etwas schwierig auf die Funktion zu schließen: "Aha. Das Skript kopiert Bilder." Von wo? Wohin? Welches System? Mir fehlt der Anwendungszweck.

@mklooss
Copy link
Author

mklooss commented Jan 20, 2014

richtig, kopiert Daten von A nach B ;-) ist zum Aufräumen gedacht, da Magento von Haus aus keine Datei löscht. kann man so einfach aufräumen.
Sprich nachdem das Script fertig ist, gibt es einen neuen Ordner unter "media/catalog" mit dem Namen "new_product" dieser Enthält alle Aktiven Bilder aus "product".
Sprich man müsste nur noch "product" löschen oder umbenennen und "new_product" in "product" bennen.
In einem Projekt konnte ich damit 30 GB freischaufeln.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment