Version Description
- Removed unused assets
Download this release
Release Info
Developer | andersthorborg |
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 +148 -131
- acf-image-crop-v5.php +94 -78
- acf-image-crop.php +1 -1
- assets/banner-772/303/227250.png +0 -0
- assets/icon-256x256.png +0 -0
- assets/screenshot-1.png +0 -0
- assets/screenshot-2.png +0 -0
- assets/screenshot-3.png +0 -0
- assets/screenshot-4.png +0 -0
- assets/screenshot-5.png +0 -0
- assets/screenshot-6.png +0 -0
- banner-772/303/227250.png +0 -0
- css/input.css +3 -0
- icon-256x256.png +0 -0
- js/input-v4.js +17 -10
- js/input.js +16 -8
- readme.txt +13 -2
- screenshot-1.png +0 -0
- screenshot-2.png +0 -0
- screenshot-3.png +0 -0
- screenshot-4.png +0 -0
- screenshot-5.png +0 -0
- screenshot-6.png +0 -0
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 |
-
$
|
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 |
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 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
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 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
531 |
-
$image->crop($x1, $y1, $x2 - $x1, $y2 - $y1, $targetW, $targetH);
|
532 |
|
533 |
-
|
534 |
-
|
535 |
|
536 |
-
|
537 |
-
|
538 |
|
539 |
-
|
540 |
-
|
541 |
|
542 |
-
|
543 |
-
|
544 |
|
545 |
-
|
546 |
-
|
547 |
|
548 |
-
|
549 |
-
|
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 |
-
//
|
580 |
-
$
|
581 |
-
|
582 |
-
|
583 |
-
|
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 |
-
//
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
$
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
602 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
603 |
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
|
612 |
-
|
613 |
-
|
614 |
|
615 |
-
|
616 |
-
|
617 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
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 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
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 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
|
|
|
|
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 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
|
|
|
|
|
|
|
|
|
|
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.
|
6 |
-
Stable tag: 1.4.
|
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
|