SVG 파일은 어디에나 있다 — 아이콘, 로고, 일러스트레이션, UI 그래픽...

발행: (2026년 2월 15일 오후 05:02 GMT+9)
4 분 소요
원문: Dev.to

Source: Dev.to

개요

SVG 파일은 어디에나 있습니다—아이콘, 로고, 일러스트레이션, UI 그래픽. 가볍고 확장 가능하며 웹 친화적이지만, 디자인 툴에서 내보낸 SVG는 종종 다음과 같은 문제를 가지고 있습니다:

  • 불필요한 메타데이터
  • 중복된 속성
  • 편집기 전용 흔적
  • 과도한 공백
  • 잠재적으로 위험한 요소

PHP에서 작업하면서 강력하고 구성 가능하며 보안에 신경 쓴 SVG 최적화기가 필요하다면, 목적에 맞게 제작된 솔루션이 있습니다:

👉 php-svg-optimizer는 SVG 2.0 사양을 준수하면서 SVG 파일을 최적화, 압축 및 정화하도록 설계된 가벼운 PHP 라이브러리입니다.

장점

  • 파일 크기 감소
  • 깔끔한 마크업
  • 표준 준수 SVG
  • 안전한 임베드
  • 시각적으로 동일한 출력
  • 성능 향상

보안

사용자로부터 SVG 업로드를 받는 경우, 정화는 선택 사항이 아니라 필수입니다.

설치

composer require mathiasreker/php-svg-optimizer

사용법

1️⃣ 명령줄 (CLI)

vendor/bin/svg-optimizer --with-all-rules process /path/to/svgs

2️⃣ PHP 패키지로 사용

try {
    $svgOptimizer = (new SvgOptimizer())
        ->withAllRules()
        ->optimize()
        ->saveToFile('path/to/output.svg');

    $metaData = $svgOptimizer->getMetaData();

    echo sprintf('Optimized size: %d bytes%s', $metaData->getOptimizedSize(), PHP_EOL);
    echo sprintf('Original size: %d bytes%s', $metaData->getOriginalSize(), PHP_EOL);
    echo sprintf('Size reduction: %d bytes%s', $metaData->getSavedBytes(), PHP_EOL);
    echo sprintf('Reduction percentage: %s %%s', $metaData->getSavedPercentage(), PHP_EOL);
    echo sprintf('Processing time: %s seconds%s', $metaData->getOptimizationTime(), PHP_EOL);
} catch (\Exception $exception) {
    echo $exception->getMessage();
}

구성 – 강력함의 비결

->withRules(
    convertColorsToHex: true,
    convertCssClassesToAttributes: true,
    convertEmptyTagsToSelfClosing: true,
    convertInlineStylesToAttributes: true,
    fixAttributeNames: false,
    flattenGroups: true,
    minifySvgCoordinates: true,
    minifyTransformations: true,
    removeAriaAndRole: true,
    removeComments: true,
    removeDataAttributes: false,
    removeDefaultAttributes: true,
    removeDeprecatedAttributes: true,
    removeDoctype: true,
    removeDuplicateElements: true,
    removeEmptyAttributes: true,
    removeEmptyGroups: true,
    removeEmptyTextElements: true,
    removeEnableBackgroundAttribute: false,
    removeInkscapeFootprints: true,
    removeInvisibleCharacters: true,
    removeMetadata: true,
    removeNonStandardAttributes: false,
    removeNonStandardTags: false,
    removeTitleAndDesc: true,
    removeUnnecessaryWhitespace: true,
    removeUnsafeElements: false,
    removeUnusedMasks: true,
    removeUnusedNamespaces: true,
    removeWidthHeightAttributes: false,
    sortAttributes: true,
)

안전한 SVG 업로드 예시

SvgOptimizerFacade::fromFile('uploaded.svg')
    ->withRules(
        removeUnsafeElements: true,
        removeNonStandardTags: true,
        removeNonStandardAttributes: true
    )
    ->allowRisky()
    ->optimize()
    ->saveToFile('sanitized.svg');

이렇게 하면 사용자 업로드 SVG를 임베드할 때 XSS 위험을 크게 줄일 수 있습니다.

개발자 경험

이 프로젝트는 다음을 강조합니다:

  • PHPStan Level 9
  • 100 % 타입 커버리지
  • 높은 테스트 커버리지
  • 전략 기반 아키텍처
  • 결정론적 출력

PHP에서 SVG를 다룬다면, 이 도구를 워크플로에 포함시킬 가치가 충분합니다.

⭐ 유용하다고 생각되면 GitHub에 ⭐️를 남겨 주세요.

0 조회
Back to Blog

관련 글

더 보기 »