PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.2
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 1.5.2, at app/src/Media/Image/Resizer.php

154 lines 3.5 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 $finder = new FileResizedFinder( $this->file );
71
72 // try to find the resized image from disk
73 if( $imagePath = $finder->process( $resizeW, $resizeH, $cropW, $cropH, $args ) ){
74 return $imagePath;
75 }
76
77 // check if resizer should create the image or not
78 if ( empty( $args['dry'] ) ) {
79 $result = (new FileEdit( $this->file ))->process( $resizeW, $resizeH, $cropW, $cropH, $args )->save();
80
81 if( !empty( $result['path'] ) ){
82 return Uri::toUrl( $result['path'] );
83 }
84 }
85
86 return '';
87 }
88
89 /**
90 * Resize the current image.
91 *
92 * @param int $width Resize width
93 * @param int $height Resize height
94 * @param array $args Cropping options
95 *
96 * @return mixed
97 * @throws ImageEditorException
98 */
99 public function resize( $width = null, $height = null, $args = [] ){
100 if ( empty( $this->file ) ) {
101 return '';
102 }
103
104 $finder = new FileResizedFinder( $this->file );
105
106 // try to find the resized image from disk
107 if( $imagePath = $finder->resize( $width, $height, $args ) ){
108 return $imagePath;
109 }
110
111 // check if resizer should create the image or not
112 if ( empty( $args['dry'] ) ) {
113 $result = ( new FileEdit( $this->file ) )->resize( $width, $height, $args )->save();
114
115 if( !empty( $result['path'] ) ){
116 return Uri::toUrl( $result['path'] );
117 }
118 }
119
120 return '';
121 }
122
123 /**
124 * Crops the images
125 *
126 * @param int $width Width to crop
127 * @param int $height Height to crop
128 * @param array $args Cropping options
129 *
130 * @return mixed
131 * @throws ImageEditorException
132 */
133 public function crop( $width = null, $height = null, $args = [] ){
134 if ( empty( $this->file ) ) {
135 return '';
136 }
137
138 $finder = new FileResizedFinder( $this->file );
139
140 // try to find the resized image from disk
141 if( $imagePath = $finder->crop( $width, $height, $args ) ){
142 return $imagePath;
143 }
144
145 $result = ( new FileEdit( $this->file ) )->crop( $width, $height, $args )->save();
146
147 if( !empty( $result['path'] ) ){
148 return Uri::toUrl( $result['path'] );
149 }
150
151 return '';
152 }
153 }
154