zKy Bảo

https://zkybao.com

Hướng dẫn tích hợp up ảnh lên web khu vực đăng tin thành viên

Vì nhiều lý do, module news hiện tại chưa cho phép tải hình minh họa khi đăng tin tại khu vực giành cho thành viên site. Hướng dẫn này giúp bạn bổ sung tính năng này nếu có nhu cầu
Nút chọn ảnh minh họa tại khu vực đăng tin của thành viên
Với thay đổi này, khu vực đăng tin của thành viên sẽ được trang bị thêm nút "Chọn hình minh họa". Ảnh sau khi upload sẽ được lưu vào thư mục uploads đúng theo cấu hình module news.

Sửa modules/news/funcs/content.php

Tìm và xóa (khoảng dòng 265) 
// Xu ly anh minh hoa
$rowcontent['homeimgthumb'] = 0;
if (! nv_is_url($rowcontent['homeimgfile']) and nv_is_file($rowcontent['homeimgfile'], NV_UPLOADS_DIR . '/' . $module_upload)) {
    $lu = strlen(NV_BASE_SITEURL . NV_UPLOADS_DIR . '/' . $module_upload . '/');
    $rowcontent['homeimgfile'] = substr($rowcontent['homeimgfile'], $lu);
    if (is_file(NV_ROOTDIR . '/' . NV_FILES_DIR . '/' . $module_upload . '/' . $rowcontent['homeimgfile'])) {
        $rowcontent['homeimgthumb'] = 1;
    } else {
        $rowcontent['homeimgthumb'] = 2;
    }
} elseif (nv_is_url($rowcontent['homeimgfile'])) {
    $rowcontent['homeimgthumb'] = 3;
} else {
    $rowcontent['homeimgfile'] = '';
}

Tìm 
if ($rowcontent['id'] == 0) {

Thêm bên trên 
$fileupload = '';
if (isset($_FILES['upload_fileupload']) and is_uploaded_file($_FILES['upload_fileupload']['tmp_name'])) {

$username_alias = defined('NV_IS_USER') ? change_alias($user_info['username']) : 'guest';
$array_structure_image = array();
$array_structure_image[''] = $module_upload;
$array_structure_image['Y'] = $module_upload . '/' . date('Y');
$array_structure_image['Ym'] = $module_upload . '/' . date('Y_m');
$array_structure_image['Y_m'] = $module_upload . '/' . date('Y/m');
$array_structure_image['Ym_d'] = $module_upload . '/' . date('Y_m/d');
$array_structure_image['Y_m_d'] = $module_upload . '/' . date('Y/m/d');
$array_structure_image['username'] = $module_upload . '/' . $username_alias;

$array_structure_image['username_Y'] = $module_upload . '/' . $username_alias . '/' . date('Y');
$array_structure_image['username_Ym'] = $module_upload . '/' . $username_alias . '/' . date('Y_m');
$array_structure_image['username_Y_m'] = $module_upload . '/' . $username_alias . '/' . date('Y/m');
$array_structure_image['username_Ym_d'] = $module_upload . '/' . $username_alias . '/' . date('Y_m/d');
$array_structure_image['username_Y_m_d'] = $module_upload . '/' . $username_alias . '/' . date('Y/m/d');

$structure_upload = isset($module_config[$module_name]['structure_upload']) ? $module_config[$module_name]['structure_upload'] : 'Ym';
$currentpath = isset($array_structure_image[$structure_upload]) ? $array_structure_image[$structure_upload] : '';

if (file_exists(NV_UPLOADS_REAL_DIR . '/' . $currentpath)) {
    $upload_real_dir_page = NV_UPLOADS_REAL_DIR . '/' . $currentpath;
} else {
    $upload_real_dir_page = NV_UPLOADS_REAL_DIR . '/' . $module_upload;
    $e = explode('/', $currentpath);
    if (!empty($e)) {
        $cp = '';
        foreach ($e as $p) {
            if (!empty($p) and !is_dir(NV_UPLOADS_REAL_DIR . '/' . $cp . $p)) {
                $mk = nv_mkdir(NV_UPLOADS_REAL_DIR . '/' . $cp, $p);
                if ($mk[0] > 0) {
                    $upload_real_dir_page = $mk[2];
                    try {
                        $db->query("INSERT INTO " . NV_UPLOAD_GLOBALTABLE . "_dir (dirname, time) VALUES ('" . NV_UPLOADS_DIR . "/" . $cp . $p . "', 0)");
                    } catch (PDOException $e) {
                        trigger_error($e->getMessage());
                    }
                }
            } elseif (!empty($p)) {
                $upload_real_dir_page = NV_UPLOADS_REAL_DIR . '/' . $cp . $p;
            }
            $cp .= $p . '/';
        }
    }
    $upload_real_dir_page = str_replace('\\', '/', $upload_real_dir_page);
}

$currentpath = str_replace(NV_ROOTDIR . '/', '', $upload_real_dir_page);
$uploads_dir_user = NV_UPLOADS_DIR . '/' . $module_upload;
if (!defined('NV_IS_SPADMIN') and strpos($structure_upload, 'username') !== false) {
    $array_currentpath = explode('/', $currentpath);
    if ($array_currentpath[2] == $username_alias) {
        $uploads_dir_user = NV_UPLOADS_DIR . '/' . $module_upload . '/' . $username_alias;
    }
}

$upload = new NukeViet\Files\Upload(array('images'), $global_config['forbid_extensions'], $global_config['forbid_mimes'], $global_config['nv_max_size'], NV_MAX_WIDTH, NV_MAX_HEIGHT);
$upload_info = $upload->save_file($_FILES['upload_fileupload'], NV_ROOTDIR . '/' . $currentpath, false);
@unlink($_FILES['upload_fileupload']['tmp_name']);
if (empty($upload_info['error'])) {
    mt_srand((double) microtime() * 1000000);
    $maxran = 1000000;
    $random_num = mt_rand(0, $maxran);
    $random_num = md5($random_num);
    $nv_pathinfo_filename = nv_pathinfo_filename($upload_info['name']);
    $new_name = NV_ROOTDIR . '/' . $currentpath . '/' . $nv_pathinfo_filename . '.' . $random_num . '.' . $upload_info['ext'];
    $rename = nv_renamefile($upload_info['name'], $new_name);
    if ($rename[0] == 1) {
        $fileupload = $new_name;
    } else {
        $fileupload = $upload_info['name'];
    }
    @chmod($fileupload, 0777);

    // tạo thumb
    nv_create_thumb(str_replace(NV_ROOTDIR . '/', '', $fileupload));

    $rowcontent['homeimgfile'] = str_replace(NV_ROOTDIR, '', $fileupload);
} else {
    $is_error = true;
    $error[] = $upload_info['error'];
}
unset($upload, $upload_info);
}

$rowcontent['homeimgthumb'] = 0;
if (! nv_is_url($rowcontent['homeimgfile']) and nv_is_file($rowcontent['homeimgfile'], NV_UPLOADS_DIR . '/' . $module_upload)) {
$lu = strlen(NV_BASE_SITEURL . NV_UPLOADS_DIR . '/' . $module_upload . '/');
$rowcontent['homeimgfile'] = substr($rowcontent['homeimgfile'], $lu);
if (is_file(NV_ROOTDIR . '/' . NV_FILES_DIR . '/' . $module_upload . '/' . $rowcontent['homeimgfile'])) {
    $rowcontent['homeimgthumb'] = 1;
} else {
    $rowcontent['homeimgthumb'] = 2;
}
} elseif (nv_is_url($rowcontent['homeimgfile'])) {
$rowcontent['homeimgthumb'] = 3;
} else {
$rowcontent['homeimgfile'] = '';
}

Tìm (khoảng dòng 591) 
$rowcontent['homeimgfile'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . '/' . $module_upload . '/' . $rowcontent['homeimgfile'];

Thêm lên trên 
if(file_exists(NV_ROOTDIR . '/' . NV_ASSETS_DIR . '/' . $module_upload . '/' . $rowcontent['homeimgfile'])){
    $rowcontent['homeimgthumb'] = NV_BASE_SITEURL . NV_ASSETS_DIR . '/' . $module_upload . '/' . $rowcontent['homeimgfile'];
}

Tìm (khoảng dòng 526) 
$xtpl->assign('HTMLBODYTEXT', $htmlbodyhtml);

Thêm bên dưới 
if(!empty($rowcontent['homeimgthumb'])){
    $xtpl->parse('main.homeimgthumb');
}

Sửa modules/news/functions.php

Thêm cuối file 
/**
 * nv_create_thumb()
 *
 * @param mixed $fileName            
 * @return
 *
 */
function nv_create_thumb($fileName)
{
    global $db;

    $array_thumb_config = array();

    $sql = 'SELECT * FROM ' . NV_UPLOAD_GLOBALTABLE . '_dir ORDER BY dirname ASC';
    $result = $db->query($sql);
    while ($row = $result->fetch()) {
        $array_dirname[$row['dirname']] = $row['did'];
        if ($row['thumb_type']) {
            $array_thumb_config[$row['dirname']] = $row;
        }
    }
    unset($array_dirname['']);

    if (preg_match('/^' . nv_preg_quote(NV_UPLOADS_DIR) . '\/(([a-z0-9\-\_\/]+\/)*([a-z0-9\-\_\.]+)(\.(gif|jpg|jpeg|png|bmp|ico)))$/i', $fileName, $m)) {
        $viewFile = NV_FILES_DIR . '/' . $m[1];

        if (file_exists(NV_ROOTDIR . '/' . $viewFile)) {
            $size = @getimagesize(NV_ROOTDIR . '/' . $viewFile);
            return array(
                $viewFile,
                $size[0],
                $size[1]
            );
        } else {
            $m[2] = rtrim($m[2], '/');

            if (isset($array_thumb_config[NV_UPLOADS_DIR . '/' . $m[2]])) {
                $thumb_config = $array_thumb_config[NV_UPLOADS_DIR . '/' . $m[2]];
            } else {
                $thumb_config = $array_thumb_config[''];
                $_arr_path = explode('/', NV_UPLOADS_DIR . '/' . $m[2]);
                while (sizeof($_arr_path) > 1) {
                    array_pop($_arr_path);
                    $_path = implode('/', $_arr_path);
                    if (isset($array_thumb_config[$_path])) {
                        $thumb_config = $array_thumb_config[$_path];
                        break;
                    }
                }
            }

            $viewDir = NV_FILES_DIR;
            if (! empty($m[2])) {
                if (! is_dir(NV_ROOTDIR . '/' . $m[2])) {
                    $e = explode('/', $m[2]);
                    $cp = NV_FILES_DIR;
                    foreach ($e as $p) {
                        if (is_dir(NV_ROOTDIR . '/' . $cp . '/' . $p)) {
                            $viewDir .= '/' . $p;
                        } else {
                            $mk = nv_mkdir(NV_ROOTDIR . '/' . $cp, $p);
                            if ($mk[0] > 0) {
                                $viewDir .= '/' . $p;
                            }
                        }
                        $cp .= '/' . $p;
                    }
                }
            }
            $image = new NukeViet\Files\Image(NV_ROOTDIR . '/' . $fileName, NV_MAX_WIDTH, NV_MAX_HEIGHT);
            if ($thumb_config['thumb_type'] == 4) {
                $thumb_width = $thumb_config['thumb_width'];
                $thumb_height = $thumb_config['thumb_height'];
                $maxwh = max($thumb_width, $thumb_height);
                if ($image->fileinfo['width'] > $image->fileinfo['height']) {
                    $thumb_config['thumb_width'] = 0;
                    $thumb_config['thumb_height'] = $maxwh;
                } else {
                    $thumb_config['thumb_width'] = $maxwh;
                    $thumb_config['thumb_height'] = 0;
                }
            }
            if ($image->fileinfo['width'] > $thumb_config['thumb_width'] or $image->fileinfo['height'] > $thumb_config['thumb_height']) {
                $image->resizeXY($thumb_config['thumb_width'], $thumb_config['thumb_height']);
                if ($thumb_config['thumb_type'] == 4) {
                    $image->cropFromCenter($thumb_width, $thumb_height);
                }
                $image->save(NV_ROOTDIR . '/' . $viewDir, $m[3] . $m[4], $thumb_config['thumb_quality']);
                $create_Image_info = $image->create_Image_info;
                $error = $image->error;
                $image->close();
                if (empty($error)) {
                    return array(
                        $viewDir . '/' . basename($create_Image_info['src']),
                        $create_Image_info['width'],
                        $create_Image_info['height']
                    );
                }
            } elseif (copy(NV_ROOTDIR . '/' . $fileName, NV_ROOTDIR . '/' . $viewDir . '/' . $m[3] . $m[4])) {
                $return = array(
                    $viewDir . '/' . $m[3] . $m[4],
                    $image->fileinfo['width'],
                    $image->fileinfo['height']
                );
                $image->close();
                return $return;
            } else {
                return false;
            }
        }
    } else {
        $size = @getimagesize(NV_ROOTDIR . '/' . $fileName);
        return array(
            $fileName,
            $size[0],
            $size[1]
        );
    }
    return false;
}

Sửa themes/default/modules/news/content.tpl

Tìm 
<form action="{CONTENT_URL}" name="fsea" method="post" id="fsea" class="form-horizontal">

Thay bằng 
<form action="{CONTENT_URL}" name="fsea" method="post" id="fsea" class="form-horizontal" enctype="multipart/form-data">

Tìm (khoảng dòng 54) 
<div class="form-group">
    <label class="col-sm-4 control-label">{LANG.content_homeimg}</label>
    <div class="col-sm-20">
        <input class="form-control" name="homeimgfile" id="homeimg" value="{DATA.homeimgfile}" type="text" />
    </div>
</div>

Thay bằng 
<div class="form-group">
    <label class="col-sm-4 control-label"><strong>{LANG.content_homeimg}</strong></label>
    <div class="col-sm-20">
        <div class="input-group">
            <input type="text" class="form-control" id="file_name" disabled>
            <span class="input-group-btn">
                <button class="btn btn-default" onclick="$('#upload_fileupload').click();" type="button">
                    <em class="fa fa-folder-open-o fa-fix">&nbsp;</em> {LANG.file_selectfile}
                </button> </span>
        </div>
        <input type="file" name="upload_fileupload" id="upload_fileupload" style="display: none" />
        <input type="hidden" name="homeimgfile" value="{DATA.homeimgfile}" />

        <!-- BEGIN: homeimgthumb -->
        <img style="margin-top: 10px" src="{DATA.homeimgthumb}" class="img-thumbnail" />
        <!-- END: homeimgthumb -->

        <script type="text/javascript">
            $('#upload_fileupload').change(function(){
                 $('#file_name').val($(this).val().match(/[-_\w]+[.][\w]+$/i)[0]);
            });
        </script>
    </div>
</div>

Nguồn tin: mynukeviet.net