Advanced Custom Fields: Image Crop Add-on - Version 1.4.3

Version Description

  • Removed unused assets
Download this release

Release Info

Developer andersthorborg
Plugin Icon wp plugin Advanced Custom Fields: Image Crop Add-on
Version 1.4.3
Comparing to
See all releases

Code changes from version 1.4.2 to 1.4.3

acf-image-crop-v4.php CHANGED
@@ -450,48 +450,50 @@ class acf_field_image_crop extends acf_field_image
450
  elseif( $field['save_format'] == 'object' )
451
  {
452
  if(is_numeric($data->cropped_image )){
453
- $attachment = get_post( $data->cropped_image );
454
- // validate
455
- if( !$attachment )
456
- {
457
- return false;
458
- }
459
-
460
-
461
- // create array to hold value data
462
- $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
463
-
464
- $value = array(
465
- 'id' => $attachment->ID,
466
- 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
467
- 'title' => $attachment->post_title,
468
- 'caption' => $attachment->post_excerpt,
469
- 'description' => $attachment->post_content,
470
- 'mime_type' => $attachment->post_mime_type,
471
- 'url' => $src[0],
472
- 'width' => $src[1],
473
- 'height' => $src[2],
474
- 'sizes' => array(),
475
- );
476
-
477
-
478
- // find all image sizes
479
- $image_sizes = get_intermediate_image_sizes();
480
-
481
- if( $image_sizes )
482
- {
483
- foreach( $image_sizes as $image_size )
484
- {
485
- // find src
486
- $src = wp_get_attachment_image_src( $attachment->ID, $image_size );
487
-
488
- // add src
489
- $value[ 'sizes' ][ $image_size ] = $src[0];
490
- $value[ 'sizes' ][ $image_size . '-width' ] = $src[1];
491
- $value[ 'sizes' ][ $image_size . '-height' ] = $src[2];
492
- }
493
- // foreach( $image_sizes as $image_size )
494
- }
 
 
495
  }
496
  elseif(is_array( $data->cropped_image) || is_object($data->cropped_image)){
497
  // Cropped image is not saved to media directory. Get data from original image instead
@@ -695,6 +697,7 @@ class acf_field_image_crop extends acf_field_image
695
  $imageData->original_image_height = $imageAtts[2];
696
  $imageData->preview_image_url = $this->get_image_src($field['value'], $field['preview_size']);
697
  $imageData->image_url = $this->get_image_src($field['value'], 'full');
 
698
  return $imageData;
699
  }
700
 
@@ -761,96 +764,110 @@ class acf_field_image_crop extends acf_field_image
761
  // Get image editor from original image path to crop the image
762
  $image = wp_get_image_editor( $mediaDir['basedir'] . '/' . $originalImageData['file'] );
763
 
764
- // Crop the image using the provided measurements
765
- $image->crop($x1, $y1, $x2 - $x1, $y2 - $y1, $targetW, $targetH);
766
-
767
- // Retrieve original filename and seperate it from its file extension
768
- $originalFileName = explode('.', basename($originalImageData['file']));
769
-
770
- // Retrieve and remove file extension from array
771
- $originalFileExtension = array_pop($originalFileName);
772
-
773
- // Generate new base filename
774
- $targetFileName = implode('.', $originalFileName) . '_' . $targetW . 'x' . $targetH . '_acf_cropped' . '.' . $originalFileExtension;
775
-
776
- // Generate target path new file using existing media library
777
- $targetFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], $targetFileName);
778
-
779
- // Get the relative path to save as the actual image url
780
- $targetRelativePath = str_replace($mediaDir['basedir'] . '/', '', $targetFilePath);
781
-
782
- // Save the image to the target path
783
- if(is_wp_error($image->save($targetFilePath))){
784
- // There was an error saving the image
785
- //TODO handle it
786
- }
787
-
788
- // If file should be saved to media library, create an attachment for it at get the new attachment ID
789
- if($saveToMediaLibrary){
790
- // Generate attachment from created file
791
-
792
- // Get the filetype
793
- $wp_filetype = wp_check_filetype(basename($targetFilePath), null );
794
- $attachment = array(
795
- 'guid' => $mediaDir['url'] . '/' . basename($targetFilePath),
796
- 'post_mime_type' => $wp_filetype['type'],
797
- 'post_title' => preg_replace('/\.[^.]+$/', '', basename($targetFilePath)),
798
- 'post_content' => '',
799
- 'post_status' => 'inherit'
800
- );
801
- $attachmentId = wp_insert_attachment( $attachment, $targetFilePath);
802
- $attachmentData = wp_generate_attachment_metadata( $attachmentId, $targetFilePath );
803
- wp_update_attachment_metadata( $attachmentId, $attachmentData );
804
- add_post_meta($attachmentId, 'acf_is_cropped', 'true', true);
805
-
806
- // Add the id to the imageData-array
807
- $imageData['value'] = $attachmentId;
808
-
809
- // Add the image url
810
- $imageUrlObject = wp_get_attachment_image_src( $attachmentId, 'full');
811
- $imageData['url'] = $imageUrlObject[0];
812
-
813
- // Add the preview url as well
814
- $previewUrlObject = wp_get_attachment_image_src( $attachmentId, $previewSize);
815
- $imageData['preview_url'] = $previewUrlObject[0];
816
- }
817
- // Else we need to return the actual path of the cropped image
818
- else{
819
- // Add the image url to the imageData-array
820
- $imageData['value'] = array('image' => $targetRelativePath);
821
- $imageData['url'] = $mediaDir['baseurl'] . '/' . $targetRelativePath;
822
-
823
- // Get preview size dimensions
824
- global $_wp_additional_image_sizes;
825
- $previewWidth = 0;
826
- $previewHeight = 0;
827
- $crop = 0;
828
- if (isset($_wp_additional_image_sizes[$previewSize])) {
829
- $previewWidth = intval($_wp_additional_image_sizes[$previewSize]['width']);
830
- $previewHeight = intval($_wp_additional_image_sizes[$previewSize]['height']);
831
- $crop = $_wp_additional_image_sizes[$previewSize]['crop'];
832
- } else {
833
- $previewWidth = get_option($previewSize.'_size_w');
834
- $previewHeight = get_option($previewSize.'_size_h');
835
- $crop = get_option($previewSize.'_crop');
836
- }
837
-
838
- // Generate preview file path
839
- $previewFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], 'preview_' . $targetFileName);
840
- $previewRelativePath = str_replace($mediaDir['basedir'] . '/', '', $previewFilePath);
841
-
842
- // Get image editor from cropped image
843
- $croppedImage = wp_get_image_editor( $targetFilePath );
844
- $croppedImage->resize($previewWidth, $previewHeight, $crop);
845
-
846
- // Save the preview
847
- $croppedImage->save($previewFilePath);
848
 
849
- // Add the preview url
850
- $imageData['preview_url'] = $mediaDir['baseurl'] . '/' . $previewRelativePath;
851
- $imageData['value']['preview'] = $previewRelativePath;
852
- }
853
- return $imageData;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
854
  }
855
 
856
  function get_image_src($id, $size = 'thumbnail'){
450
  elseif( $field['save_format'] == 'object' )
451
  {
452
  if(is_numeric($data->cropped_image )){
453
+ $value = $this->getImageArray($data->cropped_image);
454
+ $value['original_image'] = $this->getImageArray($data->original_image);
455
+ // $attachment = get_post( $data->cropped_image );
456
+ // // validate
457
+ // if( !$attachment )
458
+ // {
459
+ // return false;
460
+ // }
461
+
462
+
463
+ // // create array to hold value data
464
+ // $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
465
+
466
+ // $value = array(
467
+ // 'id' => $attachment->ID,
468
+ // 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
469
+ // 'title' => $attachment->post_title,
470
+ // 'caption' => $attachment->post_excerpt,
471
+ // 'description' => $attachment->post_content,
472
+ // 'mime_type' => $attachment->post_mime_type,
473
+ // 'url' => $src[0],
474
+ // 'width' => $src[1],
475
+ // 'height' => $src[2],
476
+ // 'sizes' => array(),
477
+ // );
478
+
479
+
480
+ // // find all image sizes
481
+ // $image_sizes = get_intermediate_image_sizes();
482
+
483
+ // if( $image_sizes )
484
+ // {
485
+ // foreach( $image_sizes as $image_size )
486
+ // {
487
+ // // find src
488
+ // $src = wp_get_attachment_image_src( $attachment->ID, $image_size );
489
+
490
+ // // add src
491
+ // $value[ 'sizes' ][ $image_size ] = $src[0];
492
+ // $value[ 'sizes' ][ $image_size . '-width' ] = $src[1];
493
+ // $value[ 'sizes' ][ $image_size . '-height' ] = $src[2];
494
+ // }
495
+ // // foreach( $image_sizes as $image_size )
496
+ // }
497
  }
498
  elseif(is_array( $data->cropped_image) || is_object($data->cropped_image)){
499
  // Cropped image is not saved to media directory. Get data from original image instead
697
  $imageData->original_image_height = $imageAtts[2];
698
  $imageData->preview_image_url = $this->get_image_src($field['value'], $field['preview_size']);
699
  $imageData->image_url = $this->get_image_src($field['value'], 'full');
700
+ $imageData->original_image_url = $imageData->image_url;
701
  return $imageData;
702
  }
703
 
764
  // Get image editor from original image path to crop the image
765
  $image = wp_get_image_editor( $mediaDir['basedir'] . '/' . $originalImageData['file'] );
766
 
767
+ if(! is_wp_error( $image ) ){
768
+
769
+ // Crop the image using the provided measurements
770
+ $image->crop($x1, $y1, $x2 - $x1, $y2 - $y1, $targetW, $targetH);
771
+
772
+ // Retrieve original filename and seperate it from its file extension
773
+ $originalFileName = explode('.', basename($originalImageData['file']));
774
+
775
+ // Retrieve and remove file extension from array
776
+ $originalFileExtension = array_pop($originalFileName);
777
+
778
+ // Generate new base filename
779
+ $targetFileName = implode('.', $originalFileName) . '_' . $targetW . 'x' . $targetH . '_acf_cropped' . '.' . $originalFileExtension;
780
+
781
+ // Generate target path new file using existing media library
782
+ $targetFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], $targetFileName);
783
+
784
+ // Get the relative path to save as the actual image url
785
+ $targetRelativePath = str_replace($mediaDir['basedir'] . '/', '', $targetFilePath);
786
+
787
+ // Save the image to the target path
788
+ if(is_wp_error($image->save($targetFilePath))){
789
+ // There was an error saving the image
790
+ //TODO handle it
791
+ }
792
+
793
+ // If file should be saved to media library, create an attachment for it at get the new attachment ID
794
+ if($saveToMediaLibrary){
795
+ // Generate attachment from created file
796
+
797
+ // Get the filetype
798
+ $wp_filetype = wp_check_filetype(basename($targetFilePath), null );
799
+ $attachment = array(
800
+ 'guid' => $mediaDir['url'] . '/' . basename($targetFilePath),
801
+ 'post_mime_type' => $wp_filetype['type'],
802
+ 'post_title' => preg_replace('/\.[^.]+$/', '', basename($targetFilePath)),
803
+ 'post_content' => '',
804
+ 'post_status' => 'inherit'
805
+ );
806
+ $attachmentId = wp_insert_attachment( $attachment, $targetFilePath);
807
+ $attachmentData = wp_generate_attachment_metadata( $attachmentId, $targetFilePath );
808
+ wp_update_attachment_metadata( $attachmentId, $attachmentData );
809
+ add_post_meta($attachmentId, 'acf_is_cropped', 'true', true);
810
+
811
+ // Add the id to the imageData-array
812
+ $imageData['value'] = $attachmentId;
813
+
814
+ // Add the image url
815
+ $imageUrlObject = wp_get_attachment_image_src( $attachmentId, 'full');
816
+ $imageData['url'] = $imageUrlObject[0];
817
+
818
+ // Add the preview url as well
819
+ $previewUrlObject = wp_get_attachment_image_src( $attachmentId, $previewSize);
820
+ $imageData['preview_url'] = $previewUrlObject[0];
821
+ }
822
+ // Else we need to return the actual path of the cropped image
823
+ else{
824
+ // Add the image url to the imageData-array
825
+ $imageData['value'] = array('image' => $targetRelativePath);
826
+ $imageData['url'] = $mediaDir['baseurl'] . '/' . $targetRelativePath;
827
+
828
+ // Get preview size dimensions
829
+ global $_wp_additional_image_sizes;
830
+ $previewWidth = 0;
831
+ $previewHeight = 0;
832
+ $crop = 0;
833
+ if (isset($_wp_additional_image_sizes[$previewSize])) {
834
+ $previewWidth = intval($_wp_additional_image_sizes[$previewSize]['width']);
835
+ $previewHeight = intval($_wp_additional_image_sizes[$previewSize]['height']);
836
+ $crop = $_wp_additional_image_sizes[$previewSize]['crop'];
837
+ } else {
838
+ $previewWidth = get_option($previewSize.'_size_w');
839
+ $previewHeight = get_option($previewSize.'_size_h');
840
+ $crop = get_option($previewSize.'_crop');
841
+ }
 
 
 
 
 
 
 
 
 
842
 
843
+ // Generate preview file path
844
+ $previewFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], 'preview_' . $targetFileName);
845
+ $previewRelativePath = str_replace($mediaDir['basedir'] . '/', '', $previewFilePath);
846
+
847
+ // Get image editor from cropped image
848
+ $croppedImage = wp_get_image_editor( $targetFilePath );
849
+ $croppedImage->resize($previewWidth, $previewHeight, $crop);
850
+
851
+ // Save the preview
852
+ $croppedImage->save($previewFilePath);
853
+
854
+ // Add the preview url
855
+ $imageData['preview_url'] = $mediaDir['baseurl'] . '/' . $previewRelativePath;
856
+ $imageData['value']['preview'] = $previewRelativePath;
857
+ }
858
+ $imageData['success'] = true;
859
+ return $imageData;
860
+ }
861
+ else{
862
+ // Handle WP_ERROR
863
+ $response = array();
864
+ $response['success'] = false;
865
+ $response['error_message'] = '';
866
+ foreach($image->error_data as $code => $message){
867
+ $response['error_message'] .= '<p><strong>' . $code . '</strong>: ' . $message . '</p>';
868
+ }
869
+ return $response;
870
+ }
871
  }
872
 
873
  function get_image_src($id, $size = 'thumbnail'){
acf-image-crop-v5.php CHANGED
@@ -77,7 +77,8 @@ class acf_field_image_crop extends acf_field_image {
77
  'height_should_be' => __( 'Height should be at least: ','acf-image_crop' ),
78
  'selected_width' => __( 'Selected image width: ','acf-image_crop' ),
79
  'selected_height' => __( 'Selected image height: ','acf-image_crop' ),
80
- 'size_warning' => __( 'Warning: The selected image is smaller than the required size!','acf-image_crop' )
 
81
  );
82
 
83
 
@@ -407,6 +408,7 @@ class acf_field_image_crop extends acf_field_image {
407
  $imageData->original_image_height = $imageAtts[2];
408
  $imageData->preview_image_url = $this->get_image_src($field['value'], $field['preview_size']);
409
  $imageData->image_url = $this->get_image_src($field['value'], 'full');
 
410
  return $imageData;
411
  }
412
 
@@ -527,96 +529,110 @@ class acf_field_image_crop extends acf_field_image {
527
  // Get image editor from original image path to crop the image
528
  $image = wp_get_image_editor( $mediaDir['basedir'] . '/' . $originalImageData['file'] );
529
 
530
- // Crop the image using the provided measurements
531
- $image->crop($x1, $y1, $x2 - $x1, $y2 - $y1, $targetW, $targetH);
532
 
533
- // Retrieve original filename and seperate it from its file extension
534
- $originalFileName = explode('.', basename($originalImageData['file']));
535
 
536
- // Retrieve and remove file extension from array
537
- $originalFileExtension = array_pop($originalFileName);
538
 
539
- // Generate new base filename
540
- $targetFileName = implode('.', $originalFileName) . '_' . $targetW . 'x' . $targetH . '_acf_cropped' . '.' . $originalFileExtension;
541
 
542
- // Generate target path new file using existing media library
543
- $targetFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], $targetFileName);
544
 
545
- // Get the relative path to save as the actual image url
546
- $targetRelativePath = str_replace($mediaDir['basedir'] . '/', '', $targetFilePath);
547
 
548
- // Save the image to the target path
549
- if(is_wp_error($image->save($targetFilePath))){
550
- // There was an error saving the image
551
- //TODO handle it
552
- }
553
-
554
- // If file should be saved to media library, create an attachment for it at get the new attachment ID
555
- if($saveToMediaLibrary){
556
- // Generate attachment from created file
557
-
558
- // Get the filetype
559
- $wp_filetype = wp_check_filetype(basename($targetFilePath), null );
560
- $attachment = array(
561
- 'guid' => $mediaDir['url'] . '/' . basename($targetFilePath),
562
- 'post_mime_type' => $wp_filetype['type'],
563
- 'post_title' => preg_replace('/\.[^.]+$/', '', basename($targetFilePath)),
564
- 'post_content' => '',
565
- 'post_status' => 'inherit'
566
- );
567
- $attachmentId = wp_insert_attachment( $attachment, $targetFilePath);
568
- $attachmentData = wp_generate_attachment_metadata( $attachmentId, $targetFilePath );
569
- wp_update_attachment_metadata( $attachmentId, $attachmentData );
570
- add_post_meta($attachmentId, 'acf_is_cropped', 'true', true);
571
-
572
- // Add the id to the imageData-array
573
- $imageData['value'] = $attachmentId;
574
-
575
- // Add the image url
576
- $imageUrlObject = wp_get_attachment_image_src( $attachmentId, 'full');
577
- $imageData['url'] = $imageUrlObject[0];
578
 
579
- // Add the preview url as well
580
- $previewUrlObject = wp_get_attachment_image_src( $attachmentId, $previewSize);
581
- $imageData['preview_url'] = $previewUrlObject[0];
582
- }
583
- // Else we need to return the actual path of the cropped image
584
- else{
585
- // Add the image url to the imageData-array
586
- $imageData['value'] = array('image' => $targetRelativePath);
587
- $imageData['url'] = $mediaDir['baseurl'] . '/' . $targetRelativePath;
588
 
589
- // Get preview size dimensions
590
- global $_wp_additional_image_sizes;
591
- $previewWidth = 0;
592
- $previewHeight = 0;
593
- $crop = 0;
594
- if (isset($_wp_additional_image_sizes[$previewSize])) {
595
- $previewWidth = intval($_wp_additional_image_sizes[$previewSize]['width']);
596
- $previewHeight = intval($_wp_additional_image_sizes[$previewSize]['height']);
597
- $crop = $_wp_additional_image_sizes[$previewSize]['crop'];
598
- } else {
599
- $previewWidth = get_option($previewSize.'_size_w');
600
- $previewHeight = get_option($previewSize.'_size_h');
601
- $crop = get_option($previewSize.'_crop');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
 
604
- // Generate preview file path
605
- $previewFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], 'preview_' . $targetFileName);
606
- $previewRelativePath = str_replace($mediaDir['basedir'] . '/', '', $previewFilePath);
607
 
608
- // Get image editor from cropped image
609
- $croppedImage = wp_get_image_editor( $targetFilePath );
610
- $croppedImage->resize($previewWidth, $previewHeight, $crop);
611
 
612
- // Save the preview
613
- $croppedImage->save($previewFilePath);
614
 
615
- // Add the preview url
616
- $imageData['preview_url'] = $mediaDir['baseurl'] . '/' . $previewRelativePath;
617
- $imageData['value']['preview'] = $previewRelativePath;
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  }
619
- return $imageData;
620
  }
621
 
622
  function get_image_src($id, $size = 'thumbnail'){
@@ -637,7 +653,7 @@ class acf_field_image_crop extends acf_field_image {
637
  function filterMediaQuery($args){
638
  // get options
639
  $options = get_option( 'acf_image_crop_settings' );
640
- $hide = $options['hide_cropped'];
641
 
642
  // If hide option is enabled, do not select items with the acf_is_cropped meta-field
643
  if($hide){
77
  'height_should_be' => __( 'Height should be at least: ','acf-image_crop' ),
78
  'selected_width' => __( 'Selected image width: ','acf-image_crop' ),
79
  'selected_height' => __( 'Selected image height: ','acf-image_crop' ),
80
+ 'size_warning' => __( 'Warning: The selected image is smaller than the required size!','acf-image_crop' ),
81
+ 'crop_error' => __( 'Sorry, an error occurred when trying to crop your image:')
82
  );
83
 
84
 
408
  $imageData->original_image_height = $imageAtts[2];
409
  $imageData->preview_image_url = $this->get_image_src($field['value'], $field['preview_size']);
410
  $imageData->image_url = $this->get_image_src($field['value'], 'full');
411
+ $imageData->original_image_url = $imageData->image_url;
412
  return $imageData;
413
  }
414
 
529
  // Get image editor from original image path to crop the image
530
  $image = wp_get_image_editor( $mediaDir['basedir'] . '/' . $originalImageData['file'] );
531
 
532
+ if(! is_wp_error( $image ) ){
 
533
 
534
+ // Crop the image using the provided measurements
535
+ $image->crop($x1, $y1, $x2 - $x1, $y2 - $y1, $targetW, $targetH);
536
 
537
+ // Retrieve original filename and seperate it from its file extension
538
+ $originalFileName = explode('.', basename($originalImageData['file']));
539
 
540
+ // Retrieve and remove file extension from array
541
+ $originalFileExtension = array_pop($originalFileName);
542
 
543
+ // Generate new base filename
544
+ $targetFileName = implode('.', $originalFileName) . '_' . $targetW . 'x' . $targetH . '_acf_cropped' . '.' . $originalFileExtension;
545
 
546
+ // Generate target path new file using existing media library
547
+ $targetFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], $targetFileName);
548
 
549
+ // Get the relative path to save as the actual image url
550
+ $targetRelativePath = str_replace($mediaDir['basedir'] . '/', '', $targetFilePath);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
551
 
552
+ // Save the image to the target path
553
+ if(is_wp_error($image->save($targetFilePath))){
554
+ // There was an error saving the image
555
+ //TODO handle it
556
+ }
 
 
 
 
557
 
558
+ // If file should be saved to media library, create an attachment for it at get the new attachment ID
559
+ if($saveToMediaLibrary){
560
+ // Generate attachment from created file
561
+
562
+ // Get the filetype
563
+ $wp_filetype = wp_check_filetype(basename($targetFilePath), null );
564
+ $attachment = array(
565
+ 'guid' => $mediaDir['url'] . '/' . basename($targetFilePath),
566
+ 'post_mime_type' => $wp_filetype['type'],
567
+ 'post_title' => preg_replace('/\.[^.]+$/', '', basename($targetFilePath)),
568
+ 'post_content' => '',
569
+ 'post_status' => 'inherit'
570
+ );
571
+ $attachmentId = wp_insert_attachment( $attachment, $targetFilePath);
572
+ $attachmentData = wp_generate_attachment_metadata( $attachmentId, $targetFilePath );
573
+ wp_update_attachment_metadata( $attachmentId, $attachmentData );
574
+ add_post_meta($attachmentId, 'acf_is_cropped', 'true', true);
575
+
576
+ // Add the id to the imageData-array
577
+ $imageData['value'] = $attachmentId;
578
+
579
+ // Add the image url
580
+ $imageUrlObject = wp_get_attachment_image_src( $attachmentId, 'full');
581
+ $imageData['url'] = $imageUrlObject[0];
582
+
583
+ // Add the preview url as well
584
+ $previewUrlObject = wp_get_attachment_image_src( $attachmentId, $previewSize);
585
+ $imageData['preview_url'] = $previewUrlObject[0];
586
  }
587
+ // Else we need to return the actual path of the cropped image
588
+ else{
589
+ // Add the image url to the imageData-array
590
+ $imageData['value'] = array('image' => $targetRelativePath);
591
+ $imageData['url'] = $mediaDir['baseurl'] . '/' . $targetRelativePath;
592
+
593
+ // Get preview size dimensions
594
+ global $_wp_additional_image_sizes;
595
+ $previewWidth = 0;
596
+ $previewHeight = 0;
597
+ $crop = 0;
598
+ if (isset($_wp_additional_image_sizes[$previewSize])) {
599
+ $previewWidth = intval($_wp_additional_image_sizes[$previewSize]['width']);
600
+ $previewHeight = intval($_wp_additional_image_sizes[$previewSize]['height']);
601
+ $crop = $_wp_additional_image_sizes[$previewSize]['crop'];
602
+ } else {
603
+ $previewWidth = get_option($previewSize.'_size_w');
604
+ $previewHeight = get_option($previewSize.'_size_h');
605
+ $crop = get_option($previewSize.'_crop');
606
+ }
607
 
608
+ // Generate preview file path
609
+ $previewFilePath = $mediaDir['path'] . '/' . wp_unique_filename( $mediaDir['path'], 'preview_' . $targetFileName);
610
+ $previewRelativePath = str_replace($mediaDir['basedir'] . '/', '', $previewFilePath);
611
 
612
+ // Get image editor from cropped image
613
+ $croppedImage = wp_get_image_editor( $targetFilePath );
614
+ $croppedImage->resize($previewWidth, $previewHeight, $crop);
615
 
616
+ // Save the preview
617
+ $croppedImage->save($previewFilePath);
618
 
619
+ // Add the preview url
620
+ $imageData['preview_url'] = $mediaDir['baseurl'] . '/' . $previewRelativePath;
621
+ $imageData['value']['preview'] = $previewRelativePath;
622
+ }
623
+ $imageData['success'] = true;
624
+ return $imageData;
625
+ }
626
+ else{
627
+ // Handle WP_ERROR
628
+ $response = array();
629
+ $response['success'] = false;
630
+ $response['error_message'] = '';
631
+ foreach($image->error_data as $code => $message){
632
+ $response['error_message'] .= '<p><strong>' . $code . '</strong>: ' . $message . '</p>';
633
+ }
634
+ return $response;
635
  }
 
636
  }
637
 
638
  function get_image_src($id, $size = 'thumbnail'){
653
  function filterMediaQuery($args){
654
  // get options
655
  $options = get_option( 'acf_image_crop_settings' );
656
+ $hide = ( isset($options['hide_cropped']) && $options['hide_cropped'] );
657
 
658
  // If hide option is enabled, do not select items with the acf_is_cropped meta-field
659
  if($hide){
acf-image-crop.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Advanced Custom Fields: Image Crop Add-on
4
  Plugin URI: https://github.com/andersthorborg/ACF-Image-Crop
5
  Description: An image field making it possible/required for the user to crop the selected image to the specified image size or dimensions
6
- Version: 1.4.1
7
  Author: Anders Thorborg
8
  Author URI: http://thorb.org
9
  License: GPLv2 or later
3
  Plugin Name: Advanced Custom Fields: Image Crop Add-on
4
  Plugin URI: https://github.com/andersthorborg/ACF-Image-Crop
5
  Description: An image field making it possible/required for the user to crop the selected image to the specified image size or dimensions
6
+ Version: 1.4.3
7
  Author: Anders Thorborg
8
  Author URI: http://thorb.org
9
  License: GPLv2 or later
assets/banner-772/303/227250.png DELETED
Binary file
assets/icon-256x256.png DELETED
Binary file
assets/screenshot-1.png DELETED
Binary file
assets/screenshot-2.png DELETED
Binary file
assets/screenshot-3.png DELETED
Binary file
assets/screenshot-4.png DELETED
Binary file
assets/screenshot-5.png DELETED
Binary file
assets/screenshot-6.png DELETED
Binary file
banner-772/303/227250.png DELETED
Binary file
css/input.css CHANGED
@@ -16,6 +16,9 @@
16
  .field_type-image_crop .crop-stage .crop-preview h4{
17
  margin-top:0;
18
  }
 
 
 
19
  /* added END */
20
  .field_type-image_crop .crop-stage .crop-preview .preview{
21
  width:100%;
16
  .field_type-image_crop .crop-stage .crop-preview h4{
17
  margin-top:0;
18
  }
19
+ .field_type-image_crop .error{
20
+ margin-top: 20px !important;
21
+ }
22
  /* added END */
23
  .field_type-image_crop .crop-stage .crop-preview .preview{
24
  width:100%;
icon-256x256.png DELETED
Binary file
js/input-v4.js CHANGED
@@ -166,10 +166,12 @@
166
  div.css('width', (selection.x2 - selection.x1) * factor);
167
  //height
168
  div.css('height', (selection.y2 - selection.y1) * factor);
169
- //x offset
170
- div.css('background-position-x', 0-(selection.x1 * factor));
171
- //y offset
172
- div.css('background-position-y', 0-(selection.y1 * factor));
 
 
173
  div.css('background-size', $options.find('.crop-stage img.crop-image').data('width') * factor + 'px' + ' ' + $options.find('.crop-stage img.crop-image').data('height') * factor + 'px');
174
  }
175
 
@@ -205,12 +207,17 @@
205
  save_to_media_library: saveToMediaLibrary
206
  }
207
  $.post(ajaxurl, data, function(data, textStatus, xhr) {
208
- $field.find('.acf-image-image').attr('src', data.preview_url);
209
- $field.find('.acf-image-value').data('cropped-image', data.value);
210
- $field.find('.acf-image-value').data('cropped', true);
211
- updateFieldValue($field);
212
- $field.find('.crop-stage').removeClass('loading');
213
- cancelCrop($field);
 
 
 
 
 
214
  }, 'json');
215
  }
216
  }
166
  div.css('width', (selection.x2 - selection.x1) * factor);
167
  //height
168
  div.css('height', (selection.y2 - selection.y1) * factor);
169
+
170
+ // Set offset - Fix by @christdg
171
+ pos_x = 0-(selection.x1 * factor);
172
+ pos_y = 0-(selection.y1 * factor);
173
+ div.css('background-position', pos_x + 'px ' + pos_y + 'px');
174
+
175
  div.css('background-size', $options.find('.crop-stage img.crop-image').data('width') * factor + 'px' + ' ' + $options.find('.crop-stage img.crop-image').data('height') * factor + 'px');
176
  }
177
 
207
  save_to_media_library: saveToMediaLibrary
208
  }
209
  $.post(ajaxurl, data, function(data, textStatus, xhr) {
210
+ if(data.success){
211
+ $field.find('[data-name=image]').attr('src', data.preview_url);
212
+ $field.find('.acf-image-value').data('cropped-image', data.value);
213
+ $field.find('.acf-image-value').data('cropped', true);
214
+ updateFieldValue($field);
215
+ }
216
+ else{
217
+ $field.append('<div class="error"><p>Sorry, an error occurred when trying to crop your image:</p>' + data.error_message);
218
+ }
219
+ $field.find('.crop-stage').removeClass('loading');
220
+ cancelCrop($field);
221
  }, 'json');
222
  }
223
  }
js/input.js CHANGED
@@ -327,6 +327,7 @@ function initialize_field( $el ) {
327
  }
328
 
329
  function updateThumbnail($field, img, selection){
 
330
  var $options = $field.find('.acf-image-uploader');
331
  var div = $field.find('.crop-preview .preview');
332
  var targetWidth = $field.find('.crop-preview .preview').width();
@@ -337,10 +338,12 @@ function initialize_field( $el ) {
337
  div.css('width', (selection.x2 - selection.x1) * factor);
338
  //height
339
  div.css('height', (selection.y2 - selection.y1) * factor);
340
- //x offset
341
- div.css('background-position-x', 0-(selection.x1 * factor));
342
- //y offset
343
- div.css('background-position-y', 0-(selection.y1 * factor));
 
 
344
  div.css('background-size', $options.find('.crop-stage img.crop-image').data('width') * factor + 'px' + ' ' + $options.find('.crop-stage img.crop-image').data('height') * factor + 'px');
345
  }
346
 
@@ -376,10 +379,15 @@ function initialize_field( $el ) {
376
  save_to_media_library: saveToMediaLibrary
377
  }
378
  $.post(ajaxurl, data, function(data, textStatus, xhr) {
379
- $field.find('[data-name=image]').attr('src', data.preview_url);
380
- $field.find('.acf-image-value').data('cropped-image', data.value);
381
- $field.find('.acf-image-value').data('cropped', true);
382
- updateFieldValue($field);
 
 
 
 
 
383
  $field.find('.crop-stage').removeClass('loading');
384
  cancelCrop($field);
385
  }, 'json');
327
  }
328
 
329
  function updateThumbnail($field, img, selection){
330
+ console.log('updating thumbnail');
331
  var $options = $field.find('.acf-image-uploader');
332
  var div = $field.find('.crop-preview .preview');
333
  var targetWidth = $field.find('.crop-preview .preview').width();
338
  div.css('width', (selection.x2 - selection.x1) * factor);
339
  //height
340
  div.css('height', (selection.y2 - selection.y1) * factor);
341
+
342
+ // Set offset - Fix by @christdg
343
+ pos_x = 0-(selection.x1 * factor);
344
+ pos_y = 0-(selection.y1 * factor);
345
+ div.css('background-position', pos_x + 'px ' + pos_y + 'px');
346
+
347
  div.css('background-size', $options.find('.crop-stage img.crop-image').data('width') * factor + 'px' + ' ' + $options.find('.crop-stage img.crop-image').data('height') * factor + 'px');
348
  }
349
 
379
  save_to_media_library: saveToMediaLibrary
380
  }
381
  $.post(ajaxurl, data, function(data, textStatus, xhr) {
382
+ if(data.success){
383
+ $field.find('[data-name=image]').attr('src', data.preview_url);
384
+ $field.find('.acf-image-value').data('cropped-image', data.value);
385
+ $field.find('.acf-image-value').data('cropped', true);
386
+ updateFieldValue($field);
387
+ }
388
+ else{
389
+ $field.append('<div class="error"><p>' + acf._e('image_crop', 'crop_error') + '</p>' + data.error_message);
390
+ }
391
  $field.find('.crop-stage').removeClass('loading');
392
  cancelCrop($field);
393
  }, 'json');
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: andersthorborg
3
  Tags: afc, advanced custom fields, image crop, image, crop
4
  Requires at least: 3.5
5
- Tested up to: 4.0
6
- Stable tag: 1.4.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -61,6 +61,17 @@ function my_register_fields()
61
 
62
  == Changelog ==
63
 
 
 
 
 
 
 
 
 
 
 
 
64
  = 1.4.1 =
65
  * Fixed issue with image not cropping in v4
66
 
2
  Contributors: andersthorborg
3
  Tags: afc, advanced custom fields, image crop, image, crop
4
  Requires at least: 3.5
5
+ Tested up to: 4.1.1
6
+ Stable tag: 1.4.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
61
 
62
  == Changelog ==
63
 
64
+ = 1.4.3 =
65
+ * Removed unused assets
66
+
67
+ = 1.4.2 =
68
+ * Improved migration from standard field to ACF crop field
69
+ * Fixed and issue that caused warnings when options was not set
70
+ * Fixed crop preview not showing correct crop position in some browsers
71
+ * Improved error handling when server setup does not support image handling
72
+ * Fixed original image data missing when saving to media library in v4
73
+ * Improved error handling in v4
74
+
75
  = 1.4.1 =
76
  * Fixed issue with image not cropping in v4
77
 
screenshot-1.png DELETED
Binary file
screenshot-2.png DELETED
Binary file
screenshot-3.png DELETED
Binary file
screenshot-4.png DELETED
Binary file
screenshot-5.png DELETED
Binary file
screenshot-6.png DELETED
Binary file