resize.php
2.58 KB
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
<?php
/**
* @Description : Resize Image
* @Package : Drag & Drop Multiple File Upload - Contact Form 7
* @Author : CodeDropz
*/
if ( ! defined( 'ABSPATH' ) || ! defined('dnd_upload_cf7') || ! defined('dnd_upload_cf7_PRO') ) {
exit;
}
class CodeDropz_Uploader_Resize {
private static $instance = null;
public static function get_instance() {
if( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct(){
}
// Resize Image
public function resize( $path, $file_name ){
if( ! $path || ! $file_name ) {
return;
}
// Get base dir and url
$wp_upload_dir = CodeDropz_Uploader_Directory::get_instance()->get_dir();
// Concat
$new_file_path = untrailingslashit( $wp_upload_dir['basedir'] ) . $path . $file_name;
$new_file = pathinfo( $file_name );
$image_format = array('jpg','png','jpeg','gif','tif');
// Call default image editor
$image = wp_get_image_editor( $new_file_path );
if ( ! is_wp_error( $image ) && in_array( strtolower( $new_file['extension'] ), $image_format ) ) {
// Get image sizes.
$orig_image = $image->get_size();
// Make sure resize image option is not empty.
if( $size = get_option('drag_n_drop_image_resize') ) {
$size = explode('x', $size);
if( $orig_image['width'] > $size[0] || $orig_image['height'] > $size[1] ) {
$image->resize( $size[0], $size[1] );
}
}
// Minor optimization
$quality = ( get_option('drag_n_drop_image_quality') ? get_option('drag_n_drop_image_quality') : 82 );
$image->set_quality( $quality );
// Fix image orientation
if( $image && file_exists( $new_file_path ) ) {
$image = dndmfu_fix_orientation( $new_file_path, $image );
}
// Save image
$final_image = $image->save( $new_file_path );
// Return image path
if( ! is_wp_error( $final_image ) ) {
return untrailingslashit( $wp_upload_dir['baseurl'] ) . $path . $final_image['file'];
}
}
return untrailingslashit( $wp_upload_dir['baseurl'] ) . $path . $file_name;
}
}