function imagepicker_upload_form_submit in Image Picker 6.2
Same name and namespace in other branches
- 5.2 imagepicker.module \imagepicker_upload_form_submit()
- 5 imagepicker.module \imagepicker_upload_form_submit()
- 7 imagepicker.upload.inc \imagepicker_upload_form_submit()
Submit form
File
- ./
imagepicker.upload.inc, line 227
Code
function imagepicker_upload_form_submit($form, &$form_state) {
$success = 0;
$errormsg = '';
if ($form_state['values']['op'] == t('Upload')) {
if (isset($form_state['values']['account']) && isset($form_state['values']['admin'])) {
$user = user_load(array(
'uid' => $form_state['values']['account'],
));
}
else {
global $user;
}
$destdir = imagepicker_get_path(FALSE, isset($form_state['values']['admin']) ? array(
'name' => $user->name,
'uid' => $user->uid,
) : TRUE);
$thumbsdir = $destdir . IMAGEPICKER_THUMBS_DIR;
$browserdir = $destdir . IMAGEPICKER_BROWSER_DIR;
$origdir = $destdir . IMAGEPICKER_ORIG_DIR;
if (file_check_directory($destdir, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE) && file_check_directory($origdir, TRUE)) {
// clear out the noisy 'created' messages
drupal_get_messages('status', TRUE);
// Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
$destdir .= DIRECTORY_SEPARATOR;
$thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
$browserdir = $browserdir . DIRECTORY_SEPARATOR;
$origdir = $origdir . DIRECTORY_SEPARATOR;
$max_uploads = variable_get('imagepicker_max_uploads', 1);
for ($ct = 1; $ct <= $max_uploads; $ct++) {
$maxthumbsize = $form_state['values']['thumb_' . $ct] ? $form_state['values']['thumb_' . $ct] : 100;
$scaleto = $form_state['values']['scale_' . $ct] ? $form_state['values']['scale_' . $ct] : FALSE;
// $destination gets altered in imagepicker_copy_uploaded_file to give us the final path + name
$destination = $origdir;
foreach ($form_state['values'] as $name => $value) {
if ($name == 'file_upload_' . $ct && !empty($_FILES['files']['name'][$name])) {
// save the original and use that from here on
if ($imagemoved = imagepicker_copy_uploaded_file($destination, $name)) {
$file = basename($destination);
$source = $origdir . $file;
// enforce a max size
$max_scale = variable_get('imagepicker_max_scale', '');
if ($max_scale) {
if ($scaleto && $scaleto > $max_scale) {
$scaleto = $max_scale;
}
else {
if ($info = image_get_info($source)) {
if ($info['width'] > $max_scale || $info['height'] > $max_scale) {
$scaleto = $max_scale;
}
}
}
}
if ($scaleto) {
$imagescaled = imagepicker_scale_image($source, $destdir . $file, $scaleto);
}
else {
// no scaling, save direct from $origdir to $destdir
file_copy($source, $destdir, FILE_EXISTS_RENAME);
}
}
if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {
// Source file should still be an uploaded one, as scaled image
// might have some watermarks etc. from drupal's filters/hooks.
// if watermark is enabled just apply to destdir image, not orig or the thumbs
if (isset($form_state['values']['watermark_' . $ct]) && $form_state['values']['watermark_' . $ct]) {
$perc = isset($form_state['values']['watermark_perc' . $ct]) ? $form_state['values']['watermark_perc' . $ct] : variable_get('image_jpeg_quality', 75);
if (!imagepicker_watermark_do($destdir . $file, $user, $perc)) {
drupal_set_message(t('Error while watermarking an uploaded image.'), 'error');
}
}
// not sure why this has to be restated, but if not done the thumbs
// get wmarked too when not scaling image above
$source = $origdir . $file;
if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
imagepicker_scale_image($source, $browserdir . $file, $user->imagepicker_default_browser_thumbnail_size ? $user->imagepicker_default_browser_thumbnail_size : variable_get('imagepicker_default_browser_thumbnail_size', 100));
$title = htmlspecialchars($form_state['values']['title_' . $ct]);
$description = htmlspecialchars($form_state['values']['description_' . $ct]);
$nextimgid = imagepicker_insert_image($user->uid, $file, $title, $description);
if ($nextimgid) {
if (is_array($form_state['values']['grouplist']) && variable_get('imagepicker_groups_enabled', 1) && variable_get('imagepicker_groups_in_upload_enabled', 1)) {
$result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d' AND img_name = '%s'", array(
$user->uid,
$file,
));
$row = db_fetch_array($result);
$record['img_id'] = $row['img_id'];
foreach ($form_state['values']['grouplist'] as $gid) {
$record['gid'] = $gid;
imagepicker_insert_group_image($record);
}
}
// external functions
if (variable_get('imagepicker_invoke_extra', 0)) {
module_invoke_all('imagepicker_upload_extra', $nextimgid);
}
$success++;
}
else {
file_delete($thumbsdir . $file);
file_delete($browserdir . $file);
file_delete($origdir . $file);
file_delete($destdir . $file);
$errormsg = t('Error while saving information to database for uploaded image.');
}
}
else {
$errormsg = t('Error while creating a thumbnail for uploaded image.');
}
}
else {
if (!$scaleto && !$imagemoved) {
$errormsg = t('Error while moving uploaded file to its destination.');
}
else {
$errormsg = t('Error while scaling uploaded file.');
}
}
}
}
}
// ct
}
else {
drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
}
}
if ($success) {
drupal_set_message(t('%c successfully uploaded.', array(
'%c' => format_plural($success, '1 image', '@count images'),
)));
if (isset($form_state['values']['admin'])) {
$outpath = IMAGEPICKER_ADMIN_PATH . '/images/user/' . $user->uid . '/browse';
}
elseif (isset($form_state['values']['account'])) {
$outpath = 'user/' . $user->uid . '/imagepicker/images/browse';
}
else {
$outpath = 'imagepicker/browse' . ($success == 1 ? '/' . $nextimgid : '');
}
drupal_goto($outpath);
}
else {
if (!$errormsg) {
$max_filesize = ini_get('upload_max_filesize');
$errormsg = t('No files uploaded. This may be because the files totalled more than %c', array(
'%c' => $max_filesize,
));
}
drupal_set_message($errormsg, 'error');
}
}