Hi everybody!
I wanted to share a small modification i did to auto generate the path for the image in the Image Manager.
Use it at your own risk
When you add the first image to an item it generates the path for where it will be stored.
I manage multiple site and i wanted one site to create the path with the category and sub-category and the rest of my sites based on the manufacturer.
So here it goes :
First, add these function to the top of /yourAdmin/image_handler.php (I added them around line 36)
The function replace_weird_char($str) replaces the characters you don't want in a file path. You can add any character you want in the array.PHP Code:
function replace_weird_char($str){
$unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A',
'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C',
'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I',
'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U',
'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a',
'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i',
'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u',
'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', '/'=>'', '\\'=>'',
' '=> '-', '.'=>'_', '®'=>'', '™'=>'' );
return strtr( $str, $unwanted_array );
}
function get_new_img_path($product_id, $base = '', $create_path_from = ''){
$path = '';
switch ($create_path_from) {
case 'category':
$cat_array = zen_generate_category_path($product_id, 'product');
$temp_path = '';
$is_first = true;
foreach ($cat_array[0] as $value) {
if (!$is_first) {
$temp_path .= '/';
}
$temp_path .= replace_weird_char($value['text']);
$is_first = false;
}
if ($temp_path != '') {
$path = $base != '' ? ' value="' . $base . '/' . $temp_path . '"' : ' value="' . $temp_path . '"';
}
break;
case 'manufacturer':
$manufacturer_name = replace_weird_char(zen_get_products_manufacturers_name($product_id));
if ($base != '') {
$path = ' value="' . $base . ($manufacturer_name != '' ? '/' . $manufacturer_name : '') .'"';
} else {
$path = ($manufacturer_name != '' ? ' value="' . $manufacturer_name .'"' : '');
}
break;
case '':
$path = $base != '' ? ' value="' . $base . '"' : '';
break;
}
return strtolower($path);
}
The second function get_new_img_path(...) creates the path according to the information you supplied. (Explained in the next section)
Second, change this line (around line 867) :
to this :PHP Code:
$contents[] = array('text' => TEXT_INFO_OR.' ' . zen_draw_input_field('imgNewBaseDir', '', 'size="20"') );
This is where the magic happens. The function get_new_img_path receive 3 parameters.PHP Code:
$contents[] = array('text' => TEXT_INFO_OR.' ' . zen_draw_input_field('imgNewBaseDir', '', 'size="20"' . get_new_img_path($pInfo->products_id, 'products', 'manufacturer')) );
The first one is the product id, don't change it.
The second is the base of the path. If you enter 'products' the path will start with 'product/[something]'.
The last is the type of path you want, you have 3 options ('', 'category' or 'manufacturer')
Here is some example :
- get_new_img_path($pInfo->products_id, 'products', 'manufacturer')
output : 'products/[manufacturer]/'
- get_new_img_path($pInfo->products_id, '', 'manufacturer')
output : '[manufacturer]/'
- get_new_img_path($pInfo->products_id, '', '')
output : ''
- get_new_img_path($pInfo->products_id, 'products', 'category')
output : 'products/[category]/[sub-category]/[sub-category]/.../[sub-category]/'
- get_new_img_path($pInfo->products_id, 'products', 'category')
output : '[category]/[sub-category]/[sub-category]/.../[sub-category]/'
Finally, i had to make a small modification to the database because the maximum number of characters in the file path for the image is 64 and you can easily go over this with the category option.
In the table 'products' the field 'products_image' needs to be change from varchar(64) to varchar(200). (I choose 200 but less could work to, i just didn't want to bother with it anymore...)
This has been tested on 4 different website and with more than 5000 items but it still should be tested thoroughly before going to a live site.
I hope it can help some of you out!
Have a good one!![]()
Bookmarks