CanFinalize.php
3 years ago
HasCharge.php
3 years ago
HasCheckout.php
3 years ago
HasCoupon.php
3 years ago
HasCustomer.php
3 years ago
HasDiscount.php
3 years ago
HasImageSizes.php
3 years ago
HasOrder.php
3 years ago
HasPaymentIntent.php
3 years ago
HasPaymentMethod.php
3 years ago
HasPrice.php
3 years ago
HasProcessorType.php
3 years ago
HasProduct.php
3 years ago
HasPurchase.php
3 years ago
HasPurchases.php
3 years ago
HasRefund.php
3 years ago
HasShippingAddress.php
3 years ago
HasSubscription.php
3 years ago
HasSubscriptions.php
3 years ago
SyncsCustomer.php
3 years ago
HasImageSizes.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models\Traits; |
| 4 | |
| 5 | /** |
| 6 | * If the model has an attached customer. |
| 7 | */ |
| 8 | trait HasImageSizes { |
| 9 | /** |
| 10 | * Sizes. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | protected $image_sizes = [ |
| 15 | 320, |
| 16 | 640, |
| 17 | 960, |
| 18 | 1280, |
| 19 | 1920, |
| 20 | ]; |
| 21 | |
| 22 | /** |
| 23 | * Options. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $resize_options = [ |
| 28 | 'fit=scale-down', |
| 29 | 'format=auto', |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * Set the image sizes. |
| 34 | * |
| 35 | * @param array $sizes Image sizes. |
| 36 | * |
| 37 | * @return $this |
| 38 | */ |
| 39 | public function withImageSizes( $sizes ) { |
| 40 | $this->image_sizes = $sizes; |
| 41 | return $this; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the image resize options. |
| 46 | * |
| 47 | * @param array $options Image resize options. |
| 48 | * |
| 49 | * @return $this |
| 50 | */ |
| 51 | public function withResizeOptions( $options ) { |
| 52 | $this->resize_options = $options; |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the URL. |
| 58 | * |
| 59 | * @param string $url The full url to the image. |
| 60 | * @param integer $size The size to use. |
| 61 | * @param boolean $append_width Append the width to the url. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | public function imageUrl( $url, $size, $append_width = false ) { |
| 66 | $url = "https://surecart.com/cdn-cgi/image/{$this->getResizeOptions()},width=$size/$url"; |
| 67 | if ( $append_width ) { |
| 68 | $url .= " $size" . 'w'; |
| 69 | } |
| 70 | return $url; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the image srcset. |
| 75 | * |
| 76 | * @param string $url The full url to the image. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function imageSrcSet( $url ) { |
| 81 | $sizes = []; |
| 82 | foreach ( $this->getImageSizes() as $size ) { |
| 83 | $sizes[] = $this->imageUrl( $url, $size, true ); |
| 84 | } |
| 85 | return implode( ', ', $sizes ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the image_sizes |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | public function getImageSizes() { |
| 94 | return apply_filters( 'surecart/default_image_sizes', $this->image_sizes, $this ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the image options. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function getResizeOptions() { |
| 103 | return implode( |
| 104 | ',', |
| 105 | apply_filters( 'surecart/image_resize_options', $this->resize_options, $this ) |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 |