PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.4.0
ShopBuilder – WooCommerce Builder For Elementor v3.4.0
3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.3.0 All 62 releases
shopbuilder / app / Models / ReSizer.php

ReSizer.php in ShopBuilder – WooCommerce Builder For Elementor 3.4.0, at app/Models/ReSizer.php

227 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for Image Resize.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Models;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 die();
12 }
13
14 use Exception;
15
16 // Do not allow directly accessing this file.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit( 'This script cannot be accessed directly.' );
19 }
20
21 /**
22 * Class for Image Resize.
23 */
24 class ReSizer {
25 /**
26 * The singleton instance
27 */
28 private static $instance = null;
29
30 /**
31 * Should an Exception be thrown on error?
32 * If false (default), then the error will just be logged.
33 */
34 public $throwOnError = false;
35
36 /**
37 * No cloning allowed
38 */
39 private function __clone() {
40 }
41
42 /**
43 * For your custom default usage you may want to initialize an ReSizer object by yourself and then have own defaults
44 */
45 public static function getInstance() {
46 if ( self::$instance == null ) {
47 self::$instance = new self();
48 }
49
50 return self::$instance;
51 }
52
53 /**
54 * Run, forest.
55 */
56 public function process( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) {
57 try {
58 // Validate inputs.
59 if ( ! $url ) {
60 throw new Exception( '$url parameter is required' );
61 }
62 if ( ! $width ) {
63 throw new Exception( '$width parameter is required' );
64 }
65 if ( ! $height ) {
66 throw new Exception( '$height parameter is required' );
67 }
68
69 // Caipt'n, ready to hook.
70 if ( true === $upscale ) {
71 add_filter( 'image_resize_dimensions', [ $this, 'aq_upscale' ], 10, 6 );
72 }
73
74 // Define upload path & dir.
75 $upload_info = wp_upload_dir();
76 $upload_dir = $upload_info['basedir'];
77 $upload_url = $upload_info['baseurl'];
78
79 $http_prefix = 'http://';
80 $https_prefix = 'https://';
81 $relative_prefix = '//'; // The protocol-relative URL
82
83 /*
84 * if the $url scheme differs from $upload_url scheme, make them match
85 * if the schemes differe, images don't show up.
86 */
87 if ( ! strncmp( $url, $https_prefix, strlen( $https_prefix ) ) ) { // if url begins with https:// make $upload_url begin with https:// as well
88 $upload_url = str_replace( $http_prefix, $https_prefix, $upload_url );
89 } elseif ( ! strncmp( $url, $http_prefix, strlen( $http_prefix ) ) ) { // if url begins with http:// make $upload_url begin with http:// as well
90 $upload_url = str_replace( $https_prefix, $http_prefix, $upload_url );
91 } elseif ( ! strncmp( $url, $relative_prefix, strlen( $relative_prefix ) ) ) { // if url begins with // make $upload_url begin with // as well
92 $upload_url = str_replace(
93 [
94 0 => "$http_prefix",
95 1 => "$https_prefix",
96 ],
97 $relative_prefix,
98 $upload_url
99 );
100 }
101
102 // Check if $img_url is local.
103 if ( false === strpos( $url, $upload_url ) ) {
104 throw new Exception( 'Image must be local: ' . $url );
105 }
106
107 // Define path of image.
108 $rel_path = str_replace( $upload_url, '', $url );
109 $img_path = $upload_dir . $rel_path;
110
111 // Check if img path exists, and is an image indeed.
112 if ( ! file_exists( $img_path ) or ! getimagesize( $img_path ) ) {
113 throw new Exception( 'Image file does not exist (or is not an image): ' . $img_path );
114 }
115
116 // Get image info.
117 $info = pathinfo( $img_path );
118 $ext = $info['extension'];
119 list( $orig_w, $orig_h ) = getimagesize( $img_path );
120
121 // Get image size after cropping.
122 $dims = image_resize_dimensions( $orig_w, $orig_h, $width, $height, $crop );
123 $dst_w = ! empty( $dims[4] ) ? $dims[4] : null;
124 $dst_h = ! empty( $dims[5] ) ? $dims[5] : null;
125
126 // Return the original image only if it exactly fits the needed measures.
127 if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {
128 $img_url = $url;
129 $dst_w = $orig_w;
130 $dst_h = $orig_h;
131 } else {
132 // Use this to check if cropped image already exists, so we can return that instead.
133 $suffix = "{$dst_w}x{$dst_h}";
134 $dst_rel_path = str_replace( '.' . $ext, '', $rel_path );
135 $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
136
137 if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) {
138 // Can't resize, so return false saying that the action to do could not be processed as planned.
139 throw new Exception( 'Unable to resize image because image_resize_dimensions() failed' );
140 } // Else check if cache exists.
141 elseif ( file_exists( $destfilename ) && getimagesize( $destfilename ) ) {
142 $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
143 } // Else, we resize the image and return the new resized image url.
144 else {
145
146 $editor = wp_get_image_editor( $img_path );
147
148 if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) {
149 throw new Exception( 'Unable to get WP_Image_Editor: ' . $editor->get_error_message() . ' (is GD or ImageMagick installed?)' );
150 }
151
152 $resized_file = $editor->save();
153
154 if ( ! is_wp_error( $resized_file ) ) {
155 $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path'] );
156 $img_url = $upload_url . $resized_rel_path;
157 } else {
158 throw new Exception( 'Unable to save resized image file: ' . $editor->get_error_message() );
159 }
160 }
161 }
162
163 // Okay, leave the ship.
164 if ( true === $upscale ) {
165 remove_filter( 'image_resize_dimensions', [ $this, 'aq_upscale' ] );
166 }
167
168 // Return the output.
169 if ( $single ) {
170 // str return.
171 $image = $img_url;
172 } else {
173 // array return.
174 $image = [
175 0 => $img_url,
176 1 => $dst_w,
177 2 => $dst_h,
178 ];
179 }
180
181 return $image;
182 } catch ( Exception $ex ) {
183 //error_log( 'ReSizer.process() error: ' . $ex->getMessage() );
184
185 if ( $this->throwOnError ) {
186 // Bubble up exception.
187 throw $ex;
188 } else {
189 // Return false, so that this patch is backwards-compatible.
190 return false;
191 }
192 }
193 }
194
195 /**
196 * Callback to overwrite WP computing of thumbnail measures
197 */
198 public function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
199 if ( ! $crop ) {
200 return null;
201 } // Let the WordPress default function handle this.
202
203 // Here is the point we allow to use larger image size than the original one.
204 $aspect_ratio = $orig_w / $orig_h;
205 $new_w = $dest_w;
206 $new_h = $dest_h;
207
208 if ( ! $new_w ) {
209 $new_w = intval( $new_h * $aspect_ratio );
210 }
211
212 if ( ! $new_h ) {
213 $new_h = intval( $new_w / $aspect_ratio );
214 }
215
216 $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
217
218 $crop_w = round( $new_w / $size_ratio );
219 $crop_h = round( $new_h / $size_ratio );
220
221 $s_x = floor( ( $orig_w - $crop_w ) / 2 );
222 $s_y = floor( ( $orig_h - $crop_h ) / 2 );
223
224 return [ 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ];
225 }
226 }
227