Как рассчитать, где обрезать изображение - imagecopyresampled - PullRequest
0 голосов
/ 05 февраля 2019

Теперь мой код дает мне изображение с правильной уменьшенной высотой и шириной, но использует верхнюю часть исходного изображения.Я хотел бы использовать центр изображения.Но я не могу понять, как рассчитать значение dst_y для imagecopyresampled для этого.Может кто-нибудь, пожалуйста, помогите мне решить :-)

<?php

function ReSize ($source,$destination,$dest_imagex,$dest_imagey,$quality) {

$source_image = imagecreatefromjpeg($source);
// Get dimensions of the original image
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);

$after_width = $dest_imagex;

    //get the reduced width
    $reduced_width = ($source_imagex - $after_width);
    //now convert the reduced width to a percentage and round it to 2 decimal places
    $reduced_radio = round(($reduced_width / $source_imagex) * 100, 2);

    //ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places.
    $reduced_height = round(($source_imagey / 100) * $reduced_radio, 2);
    //reduce the calculated height from the original height
    $after_height = $source_imagey - $reduced_height;

      $dst_y = 0; // The calculation I cannot figure out.....

$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, $dst_y, 0, 0, $after_width, $after_height, $source_imagex, $source_imagey);

$cache_folder = 'images/cache/';
$new_image = $cache_folder . rawurlencode($destination). '_' . $dest_imagex . 'x' . $dest_imagey . '.jpg';
imagejpeg($dest_image, $new_image,$quality);

echo $new_image;
}
?>

1 Ответ

0 голосов
/ 05 февраля 2019

Предположим, вы хотите изменить размер CSS-типа "обложки", у вас есть две опции:

  • Изображение по вертикали
  • Изображение по горизонтали

сначала определите соотношение источника и назначения:

$dest_ratio = $dest_imagex / $dest_imagey;
$source_ratio = $source_imagex / $source_imagey;

Если соотношение источников больше, чем коэффициент описания, это означает, что вам нужна шкала горизонтальной подгонки, в противном случае вам нужна шкала вертикальной подгонки.


Шкала горизонтальной посадки

enter image description here

imagecopyresampled(
    $dest_image,
    $source_image,
    0,
    0,
    0,
    ($source_imagey - ($source_imagex * $dest_ratio)) / 2,
    $dest_imagex,
    $dest_imagex * $source_ratio,
    $source_imagex,
    $source_imagey
);

Шкала вертикальной посадки

Vertical Fit Scale

imagecopyresampled(
    $dest_image,
    $source_image,
    0,
    0,
    ($source_imagex - ($source_imagey / $dest_ratio)) / 2,
    0,
    $dest_imagey / $source_ratio,
    $dest_imagey,
    $source_imagex,
    $source_imagey
);

Все вместе:

function crop($source, $dest, $dest_imagex, $dest_imagey, $quality)
{
    $source_image = imagecreatefromjpeg($source);
    // Get dimensions of the original image
    $source_imagex = imagesx($source_image);
    $source_imagey = imagesy($source_image);
    $source_ratio = $source_imagey / $source_imagex;
    $dest_ratio = $dest_imagey / $dest_imagex;

    $dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
    imagefill($dest_image, 0, 0, imagecolorallocate($dest_image, 255, 0, 0));

    if ($dest_ratio >= $source_ratio) {
        imagecopyresampled(
            $dest_image,
            $source_image,
            0,
            0,
            ($source_imagex - ($source_imagey / $dest_ratio)) / 2,
            0,
            $dest_imagey / $source_ratio,
            $dest_imagey,
            $source_imagex,
            $source_imagey
        );
    } else {
        imagecopyresampled(
            $dest_image,
            $source_image,
            0,
            0,
            0,
            ($source_imagey - ($source_imagex * $dest_ratio)) / 2,
            $dest_imagex,
            $dest_imagex * $source_ratio,
            $source_imagex,
            $source_imagey
        );
    }
    imagejpeg($dest_image, $dest, $quality);
}

Использование

crop("image1.jpg", "image1_resized.jpg", 100, 100, 100);
...