function imagepicker_copy_form_submit in Image Picker 7
Same name and namespace in other branches
- 6.2 imagepicker.functions.inc \imagepicker_copy_form_submit()
Function to submit the copy form
File
- ./
imagepicker.functions.inc, line 2752 - @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL
Code
function imagepicker_copy_form_submit($form, &$form_state) {
module_load_include('inc', 'imagepicker', 'imagepicker.imagefuncs');
if (isset($form_state['values']['uid'])) {
$user = user_load($form_state['values']['uid']);
}
else {
global $user;
}
$img_id = $form_state['values']['img_id'];
$newname = $form_state['values']['imagepicker_copy'];
$img = _imagepicker_get_img($img_id, TRUE, $user);
if ($img) {
$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;
// relative paths
$destdirscheme = imagepicker_get_path(FALSE, isset($form_state['values']['admin']) ? array(
'name' => $user->name,
'uid' => $user->uid,
) : TRUE, TRUE);
$thumbsdirscheme = $destdirscheme . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR;
$browserdirscheme = $destdirscheme . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR;
$origdirscheme = $destdirscheme . IMAGEPICKER_ORIG_DIR . DIRECTORY_SEPARATOR;
if (file_prepare_directory($destdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($thumbsdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($browserdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($origdir, FILE_CREATE_DIRECTORY)) {
// clear out the noisy 'created' messages
drupal_get_messages('status', TRUE);
// Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
$destdir = $destdir . DIRECTORY_SEPARATOR;
$thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
$browserdir = $browserdir . DIRECTORY_SEPARATOR;
$origdir = $origdir . DIRECTORY_SEPARATOR;
$doinsert = TRUE;
if (file_exists($destdir . $newname)) {
$doinsert = FALSE;
}
$scaleto = $form_state['values']['scale'] ? $form_state['values']['scale'] : FALSE;
$dest = $destdirscheme . $newname;
if ($scaleto) {
// as origdir is quite new...
$source = $origdir . $img->img_name;
if (!file_exists($source)) {
$source = $destdir . $img->img_name;
}
// enforce a max size
$max_scale = imagepicker_variable_get('imagepicker_max_scale', '');
if ($max_scale) {
if ($info = image_get_info($source)) {
if ($info['width'] > $max_scale || $info['height'] > $max_scale) {
$scaleto = $max_scale;
}
}
}
$imagescaled = imagepicker_scale_image($source, IMAGEPICKER_FILE_SCHEME . $dest, $scaleto);
// if watermark is enabled just apply to destdir image, not orig or the thumbs
if (isset($form_state['values']['watermark']) && $form_state['values']['watermark']) {
if (!imagepicker_watermark_do($dest, $user)) {
drupal_set_message(t('Error while watermarking an uploaded image.'), 'error');
}
}
}
else {
// no scaling, copy direct from $destdir to $destdir
$source = $destdir . $img->img_name;
imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $dest, FILE_EXISTS_REPLACE);
}
$source = $thumbsdir . $img->img_name;
$dest = $thumbsdirscheme . $newname;
imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $dest, FILE_EXISTS_REPLACE);
$source = $browserdir . $img->img_name;
$dest = $browserdirscheme . $newname;
imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $dest, FILE_EXISTS_REPLACE);
$source = $origdir . $img->img_name;
$dest = $origdirscheme . $newname;
imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $dest, FILE_EXISTS_REPLACE);
if ($doinsert) {
$nextimgid = imagepicker_insert_image($user->uid, $newname, $img->img_title, $img->img_description);
if ($nextimgid) {
$record->img_id = $nextimgid;
$gids = imagepicker_get_image_groups($img_id);
if (count($gids)) {
foreach ($gids as $gid) {
$record->gid = $gid;
imagepicker_insert_group_image($record);
}
}
drupal_set_message(t('Copy done.'));
}
else {
// rollback
file_unmanaged_delete($thumbsdir . $newname);
file_unmanaged_delete($browserdir . $newname);
file_unmanaged_delete($origdir . $newname);
file_unmanaged_delete($destdir . $newname);
// warn
drupal_set_message(t('Copy failed.'), 'warning');
}
}
}
}
}