Kiểm tra hình ảnh tải lên
Đây là một module thí dụ cách:
- Tạo một biểu mẫu upload hình ảnh,
- Kiểm tra
- Tập tin được tải lên có phải là một ảnh?
- Có vượt quá dung lượng cho phép?
- Có vượt quá kích thước cho phép?
- Lưu tập tin vào thư mục tập tin của hệ thống.
<?php
/**
* Implementation of hook_perm
*/
function image_validate_perm () {
return array ('access upload form');
}
/**
* Implementation of hook_menu
*/
function image_validate_menu ($may_cache) {
if ($may_cache) {
$items[] = array (
'path' => 'image_validate',
'title' => t('Image validate upload form'),
'access' => user_access ('access upload form'),
'callback' => 'drupal_get_form',
'callback arguments' => array ('image_validate_upload_form')
);
}
}
/**
* Define maximum allowed upload image size and dimensions
*/
function image_validate_preset () {
return array (
'max_size' => '200', // KB
'max_width' => '150', // px
'max_height' => '150' // px
);
}
/**
* Image uploading form
*/
function image_validate_upload_form () {
$form = array ();
$form['#attributes'] = array('enctype' => 'multipart/form-data');
$preset = image_validate_preset ();
$form['image_to_validate'] = array (
'#type' => 'file',
'#title' => t('Image'),
'#description' => t(
'Use this field to upload your image. Maximum dimensions is %dimensions and maximum size is %size kB',
array (
'%dimensions' => $preset['max_width'] . 'x' . $preset['max_height'],
'%size' => $preset['max_size']
)
)
);
}
/**
* Image validator
*/
function image_validate_upload_form_validate ($form_id, $form_values) {
$uploaded_file = file_check_upload ('image_to_validate');
// Check that uploaded file is an image, with a maximum file size
// and maximum height/width.
$info = image_get_info($uploaded_file->filepath);
$preset = image_validate_preset ();
if (!$info || !$info['extension']) {
form_set_error(
'image_to_validate',
t('The uploaded file was not an image.')
);
}
elseif (image_get_toolkit()) {
image_scale(
$uploaded_file->filepath,
$uploaded_file->filepath,
$preset['max_width'],
$preset['max_height']
);
}
else if (filesize($uploaded_file->filepath) > ($preset['max_size'] * 1000)) {
form_set_error(
'image_to_validate',
t(
'The uploaded image is too large; the maximum file size is %size kB.',
array(
'%size' => $preset['max_size']
)
)
);
}
elseif ($info['width'] > $preset['max_width'] || $info['height'] > $preset['max_height']) {
form_set_error(
'image_to_validate',
t(
'The uploaded image is too large; the maximum dimensions are %dimensions pixels.',
array(
'%dimensions' => $preset['max_width'] . 'x' . $preset['max_height']
)
)
);
}
}
/**
* Submit handler
*/
function image_validate_upload_form_submit ($form_id, $form_values) {
$uploaded_file = file_check_upload ('image_to_validate');
if ($uploaded_file = file_save_upload('image_to_validate', file_directory_path () . $info['extension'], 1)) {
drupal_set_message (t("Your uploaded image is okay and was saved to site file folder."));
}
else {
drupal_set_message (t("Your uploaded image is okay but an error occured while saving it."), "error");
}
}
?>Tham khảo user_validate_picture của hệ thống.
Thế Hồng
Unsigned version
1 day 5 hours ago
1 day 5 hours ago
2 days 8 hours ago
2 days 19 hours ago
3 days 8 hours ago
6 days 8 hours ago
6 days 12 hours ago
1 week 2 days ago
1 week 2 days ago
2 weeks 20 hours ago