Skip to content

Commit

Permalink
Performance Optimalisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRAoW authored Apr 5, 2024
1 parent b35357f commit 295148a
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/Repositories/ImagesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,9 @@ public function generateImage(int $attachmentId, array $size, string $destinatio
return null;
}

$imageEditor = wp_get_image_editor($originalPath);
if (is_wp_error($imageEditor)) {
trigger_error($imageEditor->get_error_message(), E_USER_WARNING);
return null;
}

$originalSize = $imageEditor->get_size();
list($originalWidth, $originalHeight) = getimagesize($originalPath);

if ($originalSize['width'] < $size['width'] || $originalSize['height'] < $size['height']) {
if ($originalWidth < $size['width'] || $originalHeight < $size['height']) {
return null;
}

Expand All @@ -219,6 +213,12 @@ public function generateImage(int $attachmentId, array $size, string $destinatio
$focalpointY = 0.5;
}

$imageEditor = wp_get_image_editor($originalPath);
if (is_wp_error($imageEditor)) {
trigger_error($imageEditor->get_error_message(), E_USER_WARNING);
return null;
}

if ($isCrop) {
// sanitize and distribute parameters
$dst_w = (int)$size['width'];
Expand All @@ -228,35 +228,35 @@ public function generateImage(int $attachmentId, array $size, string $destinatio

// maybe replace empty sizes
if (!$dst_w) {
$dst_w = $originalSize['width'];
$dst_w = $originalWidth;
}
if (!$dst_h) {
$dst_h = $originalSize['height'];
$dst_h = $originalHeight;
}

// calculate cropped image size
$src_w = $originalSize['width'];
$src_h = $originalSize['height'];
$src_w = $originalWidth;
$src_h = $originalHeight;

if ($originalSize['width'] / $originalSize['height'] > $dst_w / $dst_h) {
$src_w = round($originalSize['height'] * ($dst_w / $dst_h));
if ($originalWidth / $originalHeight > $dst_w / $dst_h) {
$src_w = round($originalHeight * ($dst_w / $dst_h));
} else {
$src_h = round($originalSize['width'] * ($dst_h / $dst_w));
$src_h = round($originalWidth * ($dst_h / $dst_w));
}

// calculate focal top left position
$src_x = $originalSize['width'] * $focal_x - $src_w * $focal_x;
if ($src_x + $src_w > $originalSize['width']) {
$src_x += $originalSize['width'] - $src_w - $src_x;
$src_x = $originalWidth * $focal_x - $src_w * $focal_x;
if ($src_x + $src_w > $originalWidth) {
$src_x += $originalWidth - $src_w - $src_x;
}
if ($src_x < 0) {
$src_x = 0;
}
$src_x = round($src_x);

$src_y = $originalSize['height'] * $focal_y - $src_h * $focal_y;
if ($src_y + $src_h > $originalSize['height']) {
$src_y += $originalSize['height'] - $src_h - $src_y;
$src_y = $originalHeight * $focal_y - $src_h * $focal_y;
if ($src_y + $src_h > $originalHeight) {
$src_y += $originalHeight - $src_h - $src_y;
}
if ($src_y < 0) {
$src_y = 0;
Expand Down

0 comments on commit 295148a

Please sign in to comment.