summaryrefslogtreecommitdiffstats
path: root/imagefield_crop.module
blob: c40dba3644e4f16680340c8a6c48023ebad5154e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
 * @file
 * Imagefield_crop module
 *
 * Provide a widget to crop uploaded image
 */

/**
 * Implements hook_field_widget_info().
 */
function imagefield_crop_field_widget_info() {
  return array(
    'imagefield_crop_widget' => array(
      'label' => t('Image with cropping'),
      'field types' => array('image'),
      'settings' => array(
        'progress_indicator' => 'throbber',
        'preview_image_style' => 'thumbnail',
        'collapsible' => 2,
        'resolution' => '200x150',
        'enforce_ratio' => TRUE,
        'enforce_minimum' => TRUE,
        'croparea' => '500x500',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
        'default value' => FIELD_BEHAVIOR_NONE,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_settings_form().
 */
function imagefield_crop_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

  // Use the image widget settings form.
  $form = image_field_widget_settings_form($field, $instance);

  $form['collapsible'] = array(
    '#type' => 'radios',
    '#title' => t('Collapsible behavior'),
    '#options' => array(
      1 => t('None.'),
      2 => t('Collapsible, expanded by default.'),
      3 => t('Collapsible, collapsed by default.'),
    ),
    '#default_value' => $settings['collapsible'],
  );

  // Resolution settings.
  $resolution = explode('x', $settings['resolution']) + array('', '');
  $form['resolution'] = array(
    '#title' => t('The resolution to crop the image onto'),
    '#element_validate' => array('_image_field_resolution_validate', '_imagefield_crop_widget_resolution_validate'),
    '#theme_wrappers' => array('form_element'),
    '#description' => t('The output resolution of the cropped image, expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 not to rescale after cropping. Note: output resolution must be defined in order to present a dynamic preview.'),
  );
  $form['resolution']['x'] = array(
    '#type' => 'textfield',
    '#default_value' => $resolution[0],
    '#size' => 5,
    '#maxlength' => 5,
    '#field_suffix' => ' x ',
    '#theme_wrappers' => array(),
  );
  $form['resolution']['y'] = array(
    '#type' => 'textfield',
    '#default_value' => $resolution[1],
    '#size' => 5,
    '#maxlength' => 5,
    '#field_suffix' => ' ' . t('pixels'),
    '#theme_wrappers' => array(),
  );
  $form['enforce_ratio'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enforce crop box ratio'),
    '#default_value' => $settings['enforce_ratio'],
    '#description' => t('Check this to force the ratio of the output on the crop box. NOTE: If you leave this unchecked but enforce an output resolution, the final image might be distorted'),
    '#element_validate' => array('_imagefield_crop_widget_enforce_ratio_validate'),
  );
  $form['enforce_minimum'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enforce minimum crop size based on the output size'),
    '#default_value' => $settings['enforce_minimum'],
    '#description' => t('Check this to force a minimum cropping selection equal to the output size. NOTE: If you leave this unchecked you might get zoomed pixels if the cropping area is smaller than the output resolution.'),
    '#element_validate' => array('_imagefield_crop_widget_enforce_minimum_validate'),
  );

  // Crop area settings.
  $croparea = explode('x', $settings['croparea']) + array('', '');
  $form['croparea'] = array(
    '#title' => t('The resolution of the cropping area'),
    '#element_validate' => array('_imagefield_crop_widget_croparea_validate'),
    '#theme_wrappers' => array('form_element'),
    '#description' => t('The resolution of the area used for the cropping of the image. Image will displayed at this resolution for cropping. Use WIDTHxHEIGHT format, empty or zero values are permitted, e.g. 500x will limit crop box to 500 pixels width.'),
  );
  $form['croparea']['x'] = array(
    '#type' => 'textfield',
    '#default_value' => $croparea[0],
    '#size' => 5,
    '#maxlength' => 5,
    '#field_suffix' => ' x ',
    '#theme_wrappers' => array(),
  );
  $form['croparea']['y'] = array(
    '#type' => 'textfield',
    '#default_value' => $croparea[1],
    '#size' => 5,
    '#maxlength' => 5,
    '#field_suffix' => ' ' . t('pixels'),
    '#theme_wrappers' => array(),
  );

  return $form;
}

function _imagefield_crop_widget_resolution_validate($element, &$form_state) {
  $settings = $form_state['values']['instance']['widget']['settings'];
  // _image_field_resolution_validate() does most of the validation
  if ($settings['enforce_ratio'] && (empty($element['x']['#value']))) {
    form_error($element, t('Target resolution must be defined as WIDTHxHEIGHT if resolution is to be enforced'));
  }
}

function _imagefield_crop_widget_enforce_ratio_validate($element, &$form_state) {
  $settings = $form_state['values']['instance']['widget']['settings'];
  if ($settings['resolution'] && !$element['#value']) {
    drupal_set_message(t('Output resolution is defined, but not enforced. Final images might be distroted'));
  }
}

function _imagefield_crop_widget_enforce_minimum_validate($element, &$form_state) {
  $settings = $form_state['values']['instance']['widget']['settings'];
  list($rw, $rh) = !empty($settings['resolution']) ? explode('x', $settings['resolution']): array(0, 0);

  if ($settings['enforce_minimum'] &&
      (!is_numeric($rw) || intval($rw) != $rw || $rw <= 0 ||
       !is_numeric($rh) || intval($rh) != $rh || $rh <= 0)) {
    form_error($element, t('Target resolution must be defined as WIDTH_HEIGHT if minimum is to be enforced.'));
  }
}


function _imagefield_crop_widget_croparea_validate($element, &$form_state) {
  foreach (array('x', 'y') as $dimension) {
    $value = $element[$dimension]['#value'];
    if (!empty($value) && !is_numeric($value)) {
      form_error($element[$dimension], t('The @dimension value must be numeric.', array('@dimesion' => $dimension)));
      return;
    }
  }
  form_set_value($element, intval($element['x']['#value']) . 'x' . intval($element['y']['#value']), $form_state);
}

/**
 * Implements hook_field_widget_form().
 */
function imagefield_crop_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $elements = image_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  foreach (element_children($elements) as $delta) {
    // Add all extra functionality provided by the imagefield_crop widget.
    $elements[$delta]['#process'][] = 'imagefield_crop_widget_process';

    // Register our value callback
    $elements[$delta]['#file_value_callbacks'] =
      array_merge(array('imagefield_crop_widget_value'), element_info_property('managed_file', '#file_value_callbacks', array()));

  }
  return $elements;
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Provides option to disable the max width fix.
 */
function imagefield_crop_form_system_themes_admin_form_alter(&$form, &$form_state, $form_id) {
  $form['admin_theme']['imagefield_crop_max_width_fix'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use the max-width fix for imagefield_crop'),
    '#default_value' => variable_get('imagefield_crop_max_width_fix', TRUE),
  );
  $form['#submit'][] = 'imagefield_crop_form_system_themes_admin_form_alter_submit';
}

/**
 * Implements hook_form_FORM_ID_alter() submit handler.
 * Provides option to disable the max width fix.
 */
function imagefield_crop_form_system_themes_admin_form_alter_submit($form, &$form_state) {
  $imagefield_crop_max_width_fix = isset($form_state['values']['imagefield_crop_max_width_fix']) ?
    $form_state['values']['imagefield_crop_max_width_fix'] : TRUE;
  variable_set('imagefield_crop_max_width_fix', $imagefield_crop_max_width_fix);
}

/**
 * An element #process callback for the imagefield_crop field type.
 */
function imagefield_crop_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];

  $instance = field_info_instance($element['#entity_type'], $element['#field_name'],  $element['#bundle']);

  $settings = $instance['settings'];
  $widget_settings = $instance['widget']['settings'];
  $element['#theme'] = 'imagefield_crop_widget';

  $path = drupal_get_path('module', 'imagefield_crop');
  $element['#attached']['js'][] = "$path/Jcrop/js/jquery.Jcrop.js";
  // We must define Drupal.behaviors for ahah to work, even if there is no file
  $element['#attached']['js'][] = "$path/imagefield_crop.js";
  $element['#attached']['css'][] = "$path/Jcrop/css/jquery.Jcrop.css";

  // Made a configuration since this is something that your local theme might not need.
  if (variable_get('imagefield_crop_max_width_fix', TRUE)) {
    $element['#attached']['css'][] = $path . '/css/imagefield-zenfix.css';
  }

  if ($element['#file']) {
    $file_to_crop = _imagefield_crop_file_to_crop($element['#file']->fid);

    $element['cropinfo'] = _imagefield_add_cropinfo_fields($element['#file']->fid);
    list($res_w, $res_h) = explode('x', $widget_settings['resolution']);
    list($crop_w, $crop_h) = explode('x', $widget_settings['croparea']);

    $element['preview'] = array(
      '#type' => 'markup',
      '#file' => $file_to_crop, // This is used by the #process function
      '#process' => array('imagefield_crop_widget_preview_process'),
      '#theme' => 'imagefield_crop_preview',
      '#description' => t('Image preview <strong>(@res_wpx x @res_hpx)</strong>',array ('@res_w' => $res_w,'@res_h' => $res_h)),
      '#markup' => theme('image', array(
        'path' => $element['#file']->uri,
        'getsize' => FALSE,
        'attributes' => array('class' => 'preview-existing'),
      )),
    );
    $element['cropbox'] = array(
      '#markup' => theme('image', array(
          'path' => $file_to_crop->uri,
          'attributes' => array(
            'class' => 'cropbox',
            'id' => $element['#id'] . '-cropbox'))),
      '#description' => t('Click on the image and drag to mark how the image will be cropped.'),
    );

    $settings = array(
      $element['#id'] => array(
        'box' => array(
          'ratio' => $res_h ? $widget_settings['enforce_ratio'] * $res_w/$res_h : 0,
          'box_width' => $crop_w,
          'box_height' => $crop_h,
        ),
        'minimum' => array(
          'width' => $widget_settings['enforce_minimum'] ? $res_w : NULL,
          'height' => $widget_settings['enforce_minimum'] ? $res_h : NULL,
        ),
        //'preview' => $preview_js,
      ),
    );
    $element['#attached']['js'][] = array(
      'data' => array('imagefield_crop' => $settings),
      'type' => 'setting',
      'scope' => 'header',
      );
  }

  // Prepend submit handler to remove button.
  array_unshift($element['remove_button']['#submit'], 'imagefield_crop_widget_delete');

  return $element;
}

function _imagefield_add_cropinfo_fields($fid = NULL) {
  $defaults = array(
    'x'       => 0,
    'y'       => 0,
    'width'   => 50,
    'height'  => 50,
    'changed' => 0,
  );
  if ($fid) {
    $crop_info = variable_get('imagefield_crop_info', array());
    if (isset($crop_info[$fid]) && !empty($crop_info[$fid])) {
      $defaults = array_merge($defaults, $crop_info[$fid]);
    }
  }

  foreach ($defaults as $name => $default) {
    $element[$name] = array(
      '#type' => 'hidden',
      '#title' => $name,
      '#attributes' => array('class' => array('edit-image-crop-' . $name)),
      '#default_value' => $default,
    );
  }
  return $element;
}


function imagefield_crop_widget_preview_process($element, &$form_state, $form) {
  $file = $element['#file'];
  if ($file->fid == 0) {
    return $element;
  }
  // The widget belongs to the parent, so we got to find it first
  $parents = array_slice($element['#array_parents'], 0, -1);
  $parent = drupal_array_get_nested_value($form, $parents);
  $instance = field_widget_instance($parent, $form_state);
  if ($instance['widget']['settings']['resolution']) {
    list($width, $height) = explode('x', $instance['widget']['settings']['resolution']);
  }

  $image_info = image_get_info(drupal_realpath($file->uri));
  $settings = array(
    $parent['#id'] => array(
      'preview' => array(
        'orig_width' => $image_info['width'],
        'orig_height' => $image_info['height'],
        'width' => (integer)$width,
        'height' => (integer)$height,
      ),
    ),
  );

  $element['#attached']['js'][] = array(
    'data' => array('imagefield_crop' => $settings),
    'type' => 'setting',
    'scope' => 'header',
  );
  $element['#imagefield_crop'] = array(
    '#file' => $element['#file'],
    '#width' => $width,
    '#height' => $height,
    '#path' => file_create_url($file->uri),
    );
  return $element;
}

/**
 * value callback
 *
 * Registered by imagefield_crop_field_widget_form()
 */
function imagefield_crop_widget_value(&$element, &$input, $form_state) {
  // set $input['fid'] and that will be the value of the element
  if (!empty($input['fid']) && $input['cropinfo']['changed']) {

    // get crop and scale info
    $crop = $input['cropinfo'];
    $instance = field_widget_instance($element, $form_state);
    $scale = NULL;
    if ($instance['widget']['settings']['resolution']) {
      list($scale['width'], $scale['height']) = explode('x', $instance['widget']['settings']['resolution']);
    }

    $src = file_load($input['fid']);
    $file_to_crop = _imagefield_crop_file_to_crop($src->fid);
    // Copy the original aside, for future cropping
    if ($file_to_crop->fid == $src->fid &&
        $orig_uri = file_unmanaged_copy($src->uri, $src->uri)) {
      $orig = clone $src;
      $orig->fid = 0;
      $orig->uri = $orig_uri;
      $orig->filename = basename($orig_uri);
      $orig->status = 1;
      $orig = file_save($orig);
      file_usage_add($orig, 'imagefield_crop', 'file', $src->fid);
    }
    // do the crop. @todo check for errors
    // This worked in D6, doesn't work in D7
    // // Save crop data to the database
    // $src->imagefield_crop = array('crop' => $crop);
    // file_save($src);

    if (_imagefield_crop_resize(drupal_realpath($file_to_crop->uri), $crop, $scale, drupal_realpath($src->uri))) {
      // insert crop info for this image in imagefield_crop_info variable
      $crop_info = variable_get('imagefield_crop_info', array());
      unset($crop['changed']);
      $crop_info[$src->fid] = $crop;
      variable_set('imagefield_crop_info', $crop_info);
      // Remove cached versions of the cropped image.
      image_path_flush($src->uri);
    }

  }
}

function imagefield_crop_widget_delete($form, &$form_state) {
  $parents = array_slice($form_state['triggering_element']['#array_parents'], 0, -1);
  $element = drupal_array_get_nested_value($form_state['values'], $parents);
  $orig = _imagefield_crop_file_to_crop($element['fid']);
  if ($orig->fid != $element['fid']) {
    file_usage_delete($orig, 'imagefield_crop');
    file_delete($orig);
    $crop_info = variable_get('imagefield_crop_info', array());
    unset($crop_info[$element['fid']]);
    variable_set('imagefield_crop_info', $crop_info);
  }
}

/**
 * Implements hook_entity_presave().
 */
function imagefield_crop_entity_presave($entity, $type) {
  // HACK - until http://drupal.org/node/1448124 is addressed, the iamgesize
  // is stored in the field instance and we have no way of cleanly altering
  // it correctly, so do it here.
  $fields = field_info_instances($type, isset($entity->type) ? $entity->type : NULL);
  if (isset($entity->type)) {
    _imagefield_crop_entity_presave($entity, $fields);
  }
  else {
    foreach ($fields as $bundle) {
      _imagefield_crop_entity_presave($entity, $bundle);
    }
  }
}

/**
 * Helper function to reset the width and height of the imagefield to
 * those of the cropped image.
 */
function _imagefield_crop_entity_presave($entity, $fields) {
  foreach ($fields as $field_name => $field) {
    if ($field['widget']['type'] != 'imagefield_crop_widget') {
      continue;
    }
    if (!isset($entity->{$field_name})) {
      continue;
    }

    // Get the language key for the field, if fields language key is not
    // $entity->language set $lang to LANGUAGE_NONE.
    $lang = LANGUAGE_NONE;
    if (isset($entity->language) && isset($entity->{$field_name}[$entity->language])) {
      $lang = $entity->language;
    }
    elseif (empty($entity->{$field_name}[$lang])) {
      continue;
    }

    foreach ($entity->{$field_name}[$lang] as $delta => $imagefield) {
      $file = file_load($imagefield['fid']);
      $info = image_get_info($file->uri);
      if (is_array($info)) {
        $entity->{$field_name}[$lang][$delta]['width'] = $info['width'];
        $entity->{$field_name}[$lang][$delta]['height'] = $info['height'];
      }
    }
  }
}

/**
 * Implements hook_theme().
 */
function imagefield_crop_theme() {
  return array(
    'imagefield_crop_widget' => array(
      'render element' => 'element',
      'file' => 'imagefield_crop.theme.inc',
    ),
    'imagefield_crop_preview' => array(
      'render element' => 'element',
      'file' => 'imagefield_crop.theme.inc',
    ),
  );
}

/**
 * Crop the image and resize it
 */
function _imagefield_crop_resize($src, $crop = NULL, $scale = NULL, $dst = NULL) {
  $image = image_load($src);
  if ($image) {
    $result = TRUE;
    if ($crop) {
      $result = $result && image_crop($image, $crop['x'], $crop['y'], $crop['width'], $crop['height']);
    }

    if ($scale) {
      $result = $result && image_scale_and_crop($image, $scale['width'], $scale['height']);
    }

    $result = $result && image_save($image, $dst ? $dst : $src);
    return $result;
  }

  return FALSE;
}

function _imagefield_crop_file_to_crop($fid) {
  // Try to find the original file for this image.
  $result = db_select('file_usage', 'fu')
    ->fields('fu', array('fid'))
    ->condition('module', 'imagefield_crop')
    ->condition('type', 'file')
    ->condition('id', $fid)
    ->condition('count', 0, '>')
    ->range(0, 1)
    ->execute();
  if ($row = $result->fetch()) {
    $fid = $row->fid;
  }
  return file_load($fid);
}