[javascript] PhotoSwipe를 이용한 이미지 파일명 변경 방법

PhotoSwipe는 모바일 및 데스크톱에서 사용할 수 있는 자바스크립트 라이브러리로, 이미지 갤러리 및 라이트박스 기능을 제공합니다. 이 기능을 통해 사용자는 이미지를 확대, 축소하거나 슬라이드로 이동할 수 있습니다.

이미지 파일명을 변경하는 것은 PhotoSwipe의 내장 기능은 아니지만, 다음과 같은 절차를 통해 파일명을 변경할 수 있습니다.

단계 1: 이미지 파일 준비하기

먼저, 변경하고자 하는 이미지 파일들을 준비합니다. 이 예제에서는 photos 폴더에 img1.jpg, img2.jpg, img3.jpg라는 이름의 이미지 파일들을 사용합니다.

단계 2: HTML 및 자바스크립트 코드 작성하기

다음으로, HTML 파일에 다음과 같이 PhotoSwipe의 HTML 구조와 자바스크립트 코드를 작성합니다.

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="path/to/photoswipe.css" />
  <link rel="stylesheet" href="path/to/default-skin/default-skin.css" />
</head>
<body>
  
  <!-- 이미지 갤러리 HTML 구조 -->
  <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
      <a href="path/to/img1.jpg" data-size="1200x800">
          <img src="path/to/img1.jpg" itemprop="thumbnail" alt="Image description" />
      </a>
    </figure>

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="path/to/img2.jpg" data-size="1200x800">
            <img src="path/to/img2.jpg" itemprop="thumbnail" alt="Image description" />
        </a>
    </figure>

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="path/to/img3.jpg" data-size="1200x800">
            <img src="path/to/img3.jpg" itemprop="thumbnail" alt="Image description" />
        </a>
    </figure>

  </div>

  <!-- PhotoSwipe 라이브러리와 초기화하는 자바스크립트 코드 -->
  <script src="path/to/photoswipe.min.js"></script>
  <script src="path/to/photoswipe-ui-default.min.js"></script>
  <script>
      (function() {
          // 이미지 갤러리 요소 선택
          var galleryElement = document.querySelector('.my-gallery');

          // PhotoSwipe 객체 생성 및 옵션 설정
          var gallery = new PhotoSwipe(galleryElement, PhotoSwipeUI_Default, items, options);
          gallery.init();
      })();
  </script>
</body>
</html>

위 코드에서는 이미지 갤러리의 HTML 구조를 정의한 후, path/to/img1.jpg, path/to/img2.jpg, path/to/img3.jpg에 해당하는 이미지 파일들을 사용하도록 설정합니다.

단계 3: 이미지 파일명 변경하기

이제 이미지 파일명을 변경하는 JavaScript 함수를 작성합니다. 아래 예제 코드는 이미지 파일명을 newimg1.jpg, newimg2.jpg, newimg3.jpg로 변경하는 예시입니다.

(function() {
    var items = [
        { src: 'path/to/newimg1.jpg', w: 1200, h: 800 },
        { src: 'path/to/newimg2.jpg', w: 1200, h: 800 },
        { src: 'path/to/newimg3.jpg', w: 1200, h: 800 }
    ];

    var options = {
        // 옵션 설정
    };

    // 이미지 갤러리 요소 선택
    var galleryElement = document.querySelector('.my-gallery');

    // PhotoSwipe 객체 생성 및 초기화
    var gallery = new PhotoSwipe(galleryElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
})();

위의 JavaScript 코드에서 items 배열에 새로운 이미지 파일 경로와 크기를 설정한 후, PhotoSwipe 객체 생성 및 초기화를 수행합니다. 이렇게 하면 이미지 파일명이 변경된 상태에서 PhotoSwipe를 사용할 수 있습니다.

이제 HTML 파일을 열어 PhotoSwipe를 실행해보면 변경된 이미지 파일명을 확인할 수 있을 것입니다.

결론

PhotoSwipe를 사용하여 이미지 파일명을 변경하는 방법에 대해 살펴보았습니다. 이미지 파일명을 변경하면 웹 앱에서 더 효과적인 이미지 관리와 사용자 경험을 제공할 수 있습니다. PhotoSwipe의 강력한 기능과 조합하여 이미지 갤러리를 구축하는 데에 활용해보세요.

참고 링크: