Есть ли хороший способ редактировать изображения с помощью php? - PullRequest
0 голосов
/ 29 мая 2019

В настоящее время я пытаюсь настроить html-галерею с помощью php-скрипта, который автоматически изменяет их размер и помещает их в галерею.Моя проблема сейчас в том, что там около 30 картинок и:

  1. Загрузка сайта занимает вечно.
  2. Изображения загружаются неправильно.Строка, которая выглядит как двоичная?Я не знаю

Это единственный PHP-код, который я использую:

<?php
            $directory = "images/gallery/feet";
            $images = glob($directory . "/*.jpg");

            foreach ($images as $image) {

              //getting the image dimensions
              list($width, $height) = getimagesize($image);

              //saving the image into memory (for manipulation with GD Library)
              $image = imagecreatefromjpeg($image);

              // calculating the part of the image to use for thumbnail
              if ($width > $height) {
                $y = 0;
                $x = ($width - $height) / 2;
                $smallestSide = $height;
              } else {
                $x = 0;
                $y = ($height - $width) / 2;
                $smallestSide = $width;
              }
              $thumbSize = 500;
              $thumb = imagecreatetruecolor($thumbSize, $thumbSize);
              imagecopyresampled($thumb, $image, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

              imagejpeg($thumb, null, 100);

              echo "<div class=\"carousel-item\">";
              echo "<img class=\"d-block w-100\" src=\"${thumb}\" />";
              echo "</div>";
            }
            ?>

Вы знаете, что я могу сделать?

1 Ответ

0 голосов
/ 29 мая 2019

Не пытайтесь запускать алгоритм изменения размера для каждого изображения при каждой загрузке страницы, это глупо дорогой процесс.

Запускайте его один раз - КОГДА-ЛИБО - для каждого файла и вставляйте миниатюры в дочерний каталог,

Затем просто дайте ссылку на файл миниатюрного изображения и позвольте вашему веб-серверу обрабатывать ваши заголовки, чтобы изображение правильно отображалось, вместо получения строки base64.

Что-то вроде

<?php
$directory = "images/gallery/feet";
$thmb_dir = $directory . "/thumbnail";
$images = glob($directory . "/*.jpg");

foreach ($images as $image) {

    if (/*thumbnail doesn't already exist*/) {

        //getting the image dimensions
        list($width, $height) = getimagesize($image);

        //saving the image into memory (for manipulation with GD Library)
        $image = imagecreatefromjpeg($image);

        // calculating the part of the image to use for thumbnail
        if ($width > $height) {
            $y = 0;
            $x = ($width - $height) / 2;
            $smallestSide = $height;
        } else {
            $x = 0;
            $y = ($height - $width) / 2;
            $smallestSide = $width;
        }
        $thumbSize = 500;
        $thumb = imagecreatetruecolor($thumbSize, $thumbSize);
        imagecopyresampled($thumb, $image, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

        imagejpeg($thumb, null, 100);

        // Save this image w/ the same name in /thumbnails - I just made up this function name
        saveThumb($thumb, $thmb_dir);
    }

    // Grab the thumbnail with the right name - another made-up function name
    $thumb_link = getThumbLink($image)

    echo "<div class=\"carousel-item\">";
    echo "<img class=\"d-block w-100\" src=\"${thumb_link}\" />";
    echo "</div>";
}

?>

...