PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Media / Image / Resizer.php

Resizer.php in Depicter — Popup & Slider Builder trunk, at app/src/Media/Image/Resizer.php

158 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Media\Image;
4
5 use Depicter\Media\Uri;
6 use Depicter\Media\Url;
7
8 /**
9 * Finds or generates resized/cropped size of image file.
10 *
11 * @package Depicter\Media\Image
12 */
13 class Resizer implements FileEditInterface
14 {
15 /**
16 * @var string
17 */
18 public $file = '';
19
20 /**
21 * Resizer constructor.
22 *
23 * @param $file
24 */
25 public function __construct( $file ) {
26 if ( is_numeric( $file ) ) {
27 $this->file = get_attached_file( $file );
28 } else {
29 $urlHandler = new Url();
30 if ( $urlHandler->isUrl( $file ) ) {
31
32 /* WPML Fix */
33 if ( function_exists('icl_object_id') ) {
34 $current_language = apply_filters( 'wpml_current_language', NULL );
35 $file = apply_filters( 'wpml_permalink', $file, $current_language, true );
36 }
37
38 /* PolyLang Fix */
39 if( function_exists( 'pll_current_language' ) && $current_language = pll_current_language() ){
40 $file = str_replace('/' . $current_language, '', $file );
41 }
42
43 $uri = $urlHandler->toUri( $file );
44 $this->file = $uri ? $uri : '';
45 } else {
46 if ( file_exists( $file ) ) {
47 $this->file = $file;
48 }
49 }
50 }
51 }
52
53 /**
54 * Shorthand method for calling resize and crop methods.
55 *
56 * @param int $resizeW Resize width
57 * @param int $resizeH Resize height
58 * @param int $cropW Crop with
59 * @param int $cropH Crop height
60 * @param array $args Resizing options
61 *
62 * @return mixed
63 * @throws ImageEditorException
64 */
65 public function process( $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){
66 if ( empty( $this->file ) ) {
67 return '';
68 }
69
70 if ( false !== strpos( $this->file, 'http' ) && strpos($this->file, 'http') === 0 ){
71 return $this->file;
72 }
73
74 $finder = new FileResizedFinder( $this->file );
75
76 // try to find the resized image from disk
77 if( $imagePath = $finder->process( $resizeW, $resizeH, $cropW, $cropH, $args ) ){
78 return $imagePath;
79 }
80
81 // check if resizer should create the image or not
82 if ( empty( $args['dry'] ) ) {
83 $result = (new FileEdit( $this->file ))->process( $resizeW, $resizeH, $cropW, $cropH, $args )->save();
84
85 if( ! is_wp_error( $result ) && !empty( $result['path'] ) ){
86 return Uri::toUrl( $result['path'] );
87 }
88 }
89
90 return '';
91 }
92
93 /**
94 * Resize the current image.
95 *
96 * @param int $width Resize width
97 * @param int $height Resize height
98 * @param array $args Cropping options
99 *
100 * @return mixed
101 * @throws ImageEditorException
102 */
103 public function resize( $width = null, $height = null, $args = [] ){
104 if ( empty( $this->file ) ) {
105 return '';
106 }
107
108 $finder = new FileResizedFinder( $this->file );
109
110 // try to find the resized image from disk
111 if( $imagePath = $finder->resize( $width, $height, $args ) ){
112 return $imagePath;
113 }
114
115 // check if resizer should create the image or not
116 if ( empty( $args['dry'] ) ) {
117 $result = ( new FileEdit( $this->file ) )->resize( $width, $height, $args )->save();
118
119 if( !empty( $result['path'] ) ){
120 return Uri::toUrl( $result['path'] );
121 }
122 }
123
124 return '';
125 }
126
127 /**
128 * Crops the images
129 *
130 * @param int $width Width to crop
131 * @param int $height Height to crop
132 * @param array $args Cropping options
133 *
134 * @return mixed
135 * @throws ImageEditorException
136 */
137 public function crop( $width = null, $height = null, $args = [] ){
138 if ( empty( $this->file ) ) {
139 return '';
140 }
141
142 $finder = new FileResizedFinder( $this->file );
143
144 // try to find the resized image from disk
145 if( $imagePath = $finder->crop( $width, $height, $args ) ){
146 return $imagePath;
147 }
148
149 $result = ( new FileEdit( $this->file ) )->crop( $width, $height, $args )->save();
150
151 if( !empty( $result['path'] ) ){
152 return Uri::toUrl( $result['path'] );
153 }
154
155 return '';
156 }
157 }
158