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 / FileResizedFinder.php

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

151 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Media\Image;
3
4 use Depicter\Media\Uri;
5
6 /**
7 * Search for cropped size of current image file
8 *
9 * @package Depicter\Media
10 */
11 class FileResizedFinder extends File implements FileEditInterface
12 {
13 /**
14 * Last founded file size path
15 *
16 * @var string
17 */
18 protected $lastFoundPath;
19
20 /**
21 * Constructor.
22 *
23 * @param string $path Path to the file or attachment id to load.
24 */
25 public function __construct( $path ){
26 parent::__construct( $path );
27 // Get original dimensions
28 $this->reset();
29 }
30
31 /**
32 * check to find image
33 *
34 * @param int $resizeW Resize width
35 * @param int $resizeH Resize height
36 * @param int $cropW Crop with
37 * @param int $cropH Crop height
38 * @param array $args Resizing options
39 *
40 * @return false|string
41 */
42 public function process( $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){
43
44 $this->orig_w = $this->getSize('width');
45 $this->orig_h = $this->getSize('height');
46
47 if ( empty( $this->orig_h ) || empty( $this->orig_w ) ) {
48 return false;
49 }
50
51 $aspect_ratio = $this->orig_w / $this->orig_h;
52
53 $this->focal[0] = !empty( $args['focalX'] ) ? $args['focalX'] : $this->focal[0];
54 $this->focal[1] = !empty( $args['focalY'] ) ? $args['focalY'] : $this->focal[1];
55
56 // auto calculate the width or height if it was set to 'auto'
57 if( 'auto' === $resizeH && is_numeric( $resizeW ) ){
58 $resizeH = $resizeW / $aspect_ratio;
59 }
60 if( 'auto' === $resizeW && is_numeric( $resizeH ) ){
61 $resizeW = $resizeH * $aspect_ratio;
62 }
63
64 if ( !empty( $cropW ) || !empty( $cropH ) ) {
65
66 if( !empty( $args['upscale'] ) ){
67 $new_w = $resizeW;
68 $new_h = $resizeH;
69 } else {
70 [ $new_w, $new_h ] = wp_constrain_dimensions( $this->orig_w, $this->orig_h, $resizeW, $resizeH );
71 }
72
73 if ( $cropW && $cropW < $new_w ) {
74 $new_w = $cropW;
75 }
76
77 if ( $cropH && $cropH < $new_h ) {
78 $new_h = $cropH;
79 }
80
81 // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
82 } else {
83 if( !empty( $args['upscale'] ) ){
84 $new_w = $resizeW;
85 $new_h = $resizeH;
86 } else {
87 [ $new_w, $new_h ] = wp_constrain_dimensions( $this->orig_w, $this->orig_h, $resizeW, $resizeH );
88 }
89
90 }
91
92 $newFilePath = $this->getFileInfo('dirname') . '/' . $this->getFileInfo('filename') . '-' . $this->getSuffix( $new_w, $new_h ) . '.' . $this->getExtension();
93
94 if ( file_exists( $newFilePath ) ) {
95 $this->lastFoundPath = $newFilePath;
96 // convert uri to url
97 return Uri::toUrl( $newFilePath );
98 }
99
100 return false;
101 }
102
103 /**
104 * Resize the current image.
105 *
106 * @param int $width Resize width
107 * @param int $height Resize height
108 * @param array $args Cropping options
109 *
110 * @return false|string
111 */
112 public function resize( $width = null, $height = null, $args = [] ){
113 return $this->process( $width, $height, null, null, $args );
114 }
115
116
117 /**
118 * Crops the images
119 *
120 * @param int $width Width to crop
121 * @param int $height Height to crop
122 * @param array $args Cropping options
123 *
124 * @return false|string
125 */
126 public function crop( $width = null, $height = null, $args = [] ){
127 return $this->process( null, null, $width, $height, $args );
128 }
129
130 /**
131 * Get image suffix
132 *
133 * @param $width
134 * @param $height
135 *
136 * @return string
137 */
138 protected function getSuffix( $width, $height ){
139 return $this->focalPointSuffix() . $width . 'x' . $height;
140 }
141
142 /**
143 * Last founded file size path
144 *
145 * @return mixed
146 */
147 public function last() {
148 return $this->lastFoundPath;
149 }
150 }
151