티스토리 뷰

카테고리 없음

[PHP] SVG를 PNG 이미지로 변환

필살기쓰세요 2021. 1. 26. 12:31

가능한 해결책:

경로가 올바른지, 'image.png'에 대한 파일 쓰기 권한이 있는지 확인하십시오.

이 질문을 참조하십시오 .

writeImage 대신 writeImageFile 사용

이 게시물을 참조하십시오 .

-------------------

그래서 저는 img가 이미 존재하는지 아니면 업로드하고 있는지 물어 보았습니다. 이미 존재한다고 말씀 하셨지만 우리는 여러분이 img를 업로드하고 변환한다는 아이디어를 작업 할 것입니다.

먼저 항상 이미지를 업로드하는 양식과 마찬가지로 :

<form action="test.php" method="post">
    <input type="file" name="img">
        <input type="submit" name="submit">
        </form> 
        

둘째, 두 개의 폴더를 만듭니다. 하나는 기본 이미지 용이고 다른 하나는 변환 된 이미지 용입니다.

  • 첫 번째 폴더 이름은 "original"입니다.
  • 두 번째 폴더 이름은 "변환 됨"입니다.

이제 html 페이지와 같은 폴더에 test.php라는 페이지를 만듭니다.

여기에서는 원본 이미지를 가져와 첫 번째 폴더에 업로드 한 다음 다시 전화를 걸어 변환하고 두 번째 폴더에 새 이미지를 저장합니다.

<?php

    if(isset($_POST['submit']))
        {
        
                $filename = $_FILES['file']['name']; // getting the name of the uploaded img 
                        $file_loc = $_FILES['file']['tmp_name'];
                        
                                $folder1="original/"; 
                                        $folder2="converted/";
                                        
                                                if(move_uploaded_file($file_loc,$folder1.$filename)) // moving the uploaded or the original img to the folder 1 
                                                            {
                                                                            rename($folder1.$filename,$folder1.$filename.".SVG"); // adding SVG ext to the file name 
                                                                                            $newext =  substr($filename,0, -3);  // taking the .SVG string from the file name 
                                                                                                            imagepng(imagecreatefromstring(file_get_contents($folder1.$filename)),$folder2.$newext."png"); // Bring the file from folder 1 and add .png to its file name and then moving it to folder 2
                                                                                                            
                                                                                                                        }
                                                                                                                                else
                                                                                                                                        {
                                                                                                                                                    echo "Something wrong with moving the file ";
                                                                                                                                                            }
                                                                                                                                                                else
                                                                                                                                                                    {
                                                                                                                                                                            echo "submit did not done ";
                                                                                                                                                                                }
                                                                                                                                                                                ?>
                                                                                                                                                                                


출처
https://stackoverflow.com/questions/39917317
댓글
공지사항
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30