ajax.php
1 month ago
custom-post.php
3 years ago
metabox.php
2 years ago
migration.php
2 years ago
resize.php
3 years ago
settings.php
4 years ago
shortcode.php
1 year ago
widget.php
4 years ago
resize.php
165 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Exit if accessed directly |
| 4 | */ |
| 5 | if ( ! defined('ABSPATH')) { |
| 6 | exit; |
| 7 | } |
| 8 | class PGCU_Image_Resizer |
| 9 | { |
| 10 | /** |
| 11 | * The attachment image ID |
| 12 | * |
| 13 | * @var int |
| 14 | */ |
| 15 | protected $attachmentId; |
| 16 | |
| 17 | /** |
| 18 | * Constructor |
| 19 | * |
| 20 | * @param int $attachmentId |
| 21 | * @return void |
| 22 | */ |
| 23 | public function __construct($attachmentId) |
| 24 | { |
| 25 | $this->attachmentId = $attachmentId; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Resizes an attachment image |
| 30 | * |
| 31 | * @param int $width |
| 32 | * @param int $height |
| 33 | * @param boolean $crop |
| 34 | * @param int $quality |
| 35 | * @return array |
| 36 | */ |
| 37 | public function resize($width, $height, $crop = true, $quality = 100) |
| 38 | { |
| 39 | global $wpdb; |
| 40 | |
| 41 | // Get the attachment |
| 42 | $attachmentUrl = wp_get_attachment_url($this->attachmentId, 'full'); |
| 43 | |
| 44 | // Bail if we don't have an attachment URL |
| 45 | if ( ! $attachmentUrl) { |
| 46 | return array('url' => $attachmentUrl, 'width' => $width, 'height' => $height); |
| 47 | } |
| 48 | |
| 49 | // Get the image file path |
| 50 | $filePath = parse_url($attachmentUrl); |
| 51 | $filePath = $_SERVER['DOCUMENT_ROOT'] . $filePath['path']; |
| 52 | |
| 53 | // Additional handling for multisite |
| 54 | if (is_multisite()) { |
| 55 | global $blog_id; |
| 56 | $blogDetails = get_blog_details($blog_id); |
| 57 | $filePath = str_replace($blogDetails->path . 'files/', '/wp-content/blogs.dir/'. $blog_id .'/files/', $filePath); |
| 58 | } |
| 59 | |
| 60 | // Destination width and height variables |
| 61 | $destWidth = apply_filters('easingslider_resize_image_width', $width, $attachmentUrl); |
| 62 | $destHeight = apply_filters('easingslider_resize_image_height', $height, $attachmentUrl); |
| 63 | |
| 64 | // File name suffix (appended to original file name) |
| 65 | $suffix = "{$destWidth}x{$destHeight}"; |
| 66 | |
| 67 | // Some additional info about the image |
| 68 | $info = pathinfo($filePath); |
| 69 | $dir = $info['dirname']; |
| 70 | $ext = $info['extension']; |
| 71 | $name = wp_basename($filePath, ".$ext"); |
| 72 | |
| 73 | // Suffix applied to filename |
| 74 | $suffix = "{$destWidth}x{$destHeight}"; |
| 75 | |
| 76 | // Get the destination file name |
| 77 | $destFileName = "{$dir}/{$name}-{$suffix}.{$ext}"; |
| 78 | |
| 79 | // Execute the resizing if resized image doesn't already exist. |
| 80 | if ( ! file_exists($destFileName)) { |
| 81 | |
| 82 | // Load Wordpress Image Editor |
| 83 | $editor = wp_get_image_editor($filePath); |
| 84 | |
| 85 | // Bail if we encounter a WP_Error |
| 86 | if (is_wp_error($editor)) { |
| 87 | return array('url' => $attachmentUrl, 'width' => $width, 'height' => $height); |
| 88 | } |
| 89 | |
| 90 | // Set the quality |
| 91 | $editor->set_quality($quality); |
| 92 | |
| 93 | // Get the original image size |
| 94 | $size = $editor->get_size(); |
| 95 | $origWidth = $size['width']; |
| 96 | $origHeight = $size['height']; |
| 97 | |
| 98 | $srcX = $srcY = 0; |
| 99 | $srcW = $origWidth; |
| 100 | $srcH = $origHeight; |
| 101 | |
| 102 | // Handle cropping |
| 103 | if ($crop) { |
| 104 | |
| 105 | $cmpX = $origWidth / $destWidth; |
| 106 | $cmpY = $origHeight / $destHeight; |
| 107 | |
| 108 | // Calculate x or y coordinate, and width or height of source |
| 109 | if ($cmpX > $cmpY) { |
| 110 | $srcW = round($origWidth / $cmpX * $cmpY); |
| 111 | $srcX = round(($origWidth - ($origWidth / $cmpX * $cmpY)) / 2); |
| 112 | } |
| 113 | else if ($cmpY > $cmpX) { |
| 114 | $srcH = round($origHeight / $cmpY * $cmpX); |
| 115 | $srcY = round(($origHeight - ($origHeight / $cmpY * $cmpX)) / 2); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | // Time to crop the image |
| 121 | $editor->crop($srcX, $srcY, $srcW, $srcH, $destWidth, $destHeight); |
| 122 | |
| 123 | // Now let's save the image |
| 124 | $saved = $editor->save($destFileName); |
| 125 | |
| 126 | // Get resized image information |
| 127 | $resizedUrl = str_replace(basename($attachmentUrl), basename($saved['path']), $attachmentUrl); |
| 128 | $resizedWidth = $saved['width']; |
| 129 | $resizedHeight = $saved['height']; |
| 130 | $resizedType = $saved['mime-type']; |
| 131 | |
| 132 | /** |
| 133 | * Add the resized dimensions to original image metadata |
| 134 | * |
| 135 | * This ensures our resized images are deleted when the original image is deleted from the Media Library |
| 136 | */ |
| 137 | $metadata = wp_get_attachment_metadata($this->attachmentId); |
| 138 | if (isset($metadata['image_meta'])) { |
| 139 | $metadata['image_meta']['resized_images'][] = $resizedWidth .'x'. $resizedHeight; |
| 140 | wp_update_attachment_metadata($this->attachmentId, $metadata); |
| 141 | } |
| 142 | |
| 143 | // Create the image array |
| 144 | $resizedImage = array( |
| 145 | 'url' => $resizedUrl, |
| 146 | 'width' => $resizedWidth, |
| 147 | 'height' => $resizedHeight, |
| 148 | 'type' => $resizedType |
| 149 | ); |
| 150 | |
| 151 | } |
| 152 | else { |
| 153 | $resizedImage = array( |
| 154 | 'url' => str_replace(basename($attachmentUrl), basename($destFileName), $attachmentUrl), |
| 155 | 'width' => $destWidth, |
| 156 | 'height' => $destHeight, |
| 157 | 'type' => $ext |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | // And we're done! |
| 162 | return $resizedImage; |
| 163 | } |
| 164 | } |
| 165 |