$thumb_destination = WB_PATH . MEDIA_DIRECTORY . '/' . $img_dir . '/thumbs/item' . $item_id . '/' . $filename . '.' . $fileext;
     // Check thumbnail type
     if ($fileext == 'png') {
         make_thumb_png($new_file, $thumb_destination, $resize);
     } else {
         make_thumb($new_file, $thumb_destination, $resize);
     }
     change_mode($thumb_destination);
 }
 // Check if we need to resize the image
 if ($imgresize == 'yes' && file_exists($new_file)) {
     // Image destination
     $img_destination = WB_PATH . MEDIA_DIRECTORY . '/' . $img_dir . '/images/item' . $item_id . '/' . $filename . '.' . $fileext;
     // Check image type
     if ($fileext == 'png') {
         resizePNG($new_file, $img_destination, $maxwidth, $maxheight);
     } else {
         resizeJPEG($new_file, $maxwidth, $maxheight, $quality);
     }
     change_mode($img_destination);
 }
 // Insert new image data into the db
 // Get image top position for this item
 $top_position = $database->get_one("SELECT MAX(position) AS top_position FROM " . TABLE_PREFIX . "mod_bakery_images WHERE item_id = '{$item_id}'");
 // Increment position (db function returns NULL if this item has no image yet)
 $top_position = intval($top_position) + 1;
 // Insert file into database
 $filename = $filename . '.' . $fileext;
 $database->query("INSERT INTO " . TABLE_PREFIX . "mod_bakery_images (item_id, filename, position) VALUES ('{$item_id}', '{$filename}', '{$top_position}')");
 // Check if there was a db error
 if ($database->is_error()) {
                    insert_prices($item_id, $prices);
                    $picture = @$xpath->query('./stk:stockHeader/stk:pictures/stk:picture[@default="true"]/stk:filepath', $node)->item(0)->nodeValue;
                    if ($item_id > 0 && !empty($picture) && is_file(WB_PATH . '/temp/images/' . $picture)) {
                        $main_image = $picture;
                        if (!is_dir(WB_PATH . '/media/eshop/images/item' . $item_id)) {
                            mkdir(WB_PATH . '/media/eshop/images/item' . $item_id);
                        }
                        if (!is_dir(WB_PATH . '/media/eshop/thumbs/item' . $item_id)) {
                            mkdir(WB_PATH . '/media/eshop/thumbs/item' . $item_id);
                        }
                        $new_file = WB_PATH . '/media/eshop/images/item' . $item_id . '/' . $main_image;
                        copy(WB_PATH . '/temp/images/' . $main_image, $new_file);
                        $thumb_destination = WB_PATH . MEDIA_DIRECTORY . '/eshop/thumbs/item' . $item_id . '/' . $main_image;
                        $fileext = pathinfo($main_image, PATHINFO_EXTENSION);
                        if ($fileext == "png") {
                            resizePNG($new_file, $thumb_destination, $resize, $resize);
                        } else {
                            make_thumb($new_file, $thumb_destination, $resize);
                        }
                        $q = "UPDATE " . TABLE_PREFIX . "mod_eshop_items SET main_image = '{$main_image}' WHERE item_id = {$item_id} LIMIT 1";
                        $database->query($q);
                    }
                }
            }
        }
    }
}
function insert_item($data)
{
    global $database, $module_pages_directory;
    $q = "INSERT INTO " . TABLE_PREFIX . "mod_eshop_items (section_id, page_id, active, title, sku, ean, stock, price, unit, weight, description, full_desc, pohoda_id)\n        VALUES ({$data['section_id']}, {$data['page_id']}, 1, '{$data['title']}', '{$data['sku']}', '{$data['ean']}', '{$data['stock']}', {$data['price']}, '{$data['unit']}', '{$data['weight']}', '{$data['description']}', '{$data['description']}', {$data['item_id']})";