팁&유틸PHP의 URL에서 이미지 저장

공수래 2024.02.11 09:27 조회 수 : 218

  1. PHP에서 file_get_contents() 및 file_put_contents()를 사용하여 URL에서 이미지 저장
  2. cURL을 사용하여 URL에서 이미지 저장
  3. PHP에서 copy() 기능을 사용하여 URL에서 이미지 저장
  4. PHP에서 fread() 및 fwrite()를 사용하여 URL에서 이미지 저장
  5. PHP에서 gzip 이미지 저장
PHP의 URL에서 이미지 저장

이 기사에서는 PHP의 URL에서 이미지를 저장하는 다섯 가지 방법을 설명합니다. 이러한 메서드는 file_put_contents()copy()fopen()fread()fwrite() 및 gzdecode()와 같은 함수를 사용합니다.

PHP에서 file_get_contents() 및 file_put_contents()를 사용하여 URL에서 이미지 저장

PHP file_get_contents()는 파일을 문자열로 읽고 file_put_contents()는 해당 문자열을 파일에 쓸 수 있습니다. 두 기능을 결합하면 URL에서 이미지를 저장할 수 있습니다.

먼저 file_get_contents()를 사용하여 URL의 이미지를 문자열로 변환한 다음 file_put_contents()를 사용하여 이 문자열을 파일에 저장합니다. 결과는 URL의 이미지 사본입니다.

다음에서는 file_get_contents() 및 file_put_contents()를 사용하여 Imgur에서 이미지를 저장합니다. 또한 이미지의 이름을 01_computer_image.png로 바꾸지만 다른 이름과 유효한 이미지 확장자를 사용할 수 있습니다.

<?php
    // The image is from Unsplash, and we've uploaded
    // it to Imgur for this article.
    $image_url = 'https://i.imgur.com/NFyDQTj.jpeg';
    // Define the image location. Here, the location
    // is the saved_images folder.
    $image_location = 'saved_images/01_computer_image.png';
    // Use file_put_contents to grab the image from
    // the URL and place it into the image location
    // with its new name.
    if (file_put_contents($image_location, file_get_contents($image_url))) {
        echo "Image saved successfully";
    } else {
        echo "An error code, please check your code.";
    }
?>

출력:

![PHP file_put_contents 및 file_get_contents를 사용하여 URL에서 이미지 저장](</img/PHP/php file_put_contents 및 file_get_contents.gif>를 사용하여 URL에서 이미지 저장

cURL을 사용하여 URL에서 이미지 저장

cURL은 네트워크 프로토콜을 사용하여 데이터를 전송하는 명령줄 도구입니다. 이미지가 서버의 URL에 존재하므로 컴퓨터에 사본을 저장할 cURL 세션을 시작할 수 있습니다.

다음에는 Imgur URL에서 이미지를 저장하는 cURL 세션이 있습니다.

<?php
    // Initiate a cURL request to the image, and
    // define its location.
    $curl_handler = curl_init('https://i.imgur.com/ZgpqSGm.jpeg');
    $image_location = 'saved_images/02_apples.png';
    // Open the file for writing in binary mode
    $open_image_in_binary = fopen($image_location, 'wb');
    // Define where cURL should save the image.
    curl_setopt($curl_handler, CURLOPT_FILE, $open_image_in_binary);
    curl_setopt($curl_handler, CURLOPT_HEADER, 0);
    // Lets you use this script when there is
    // redirect on the server.
    curl_setopt($curl_handler, CURLOPT_FOLLOWLOCATION, true);
    // Auto detect encoding for the response | identity
    // deflation and gzip
    curl_setopt($curl_handler, CURLOPT_ENCODING, '');
    // Execute the current cURL session.
    curl_exec($curl_handler);
    // Close the connection and
    curl_close($curl_handler);
    // Close the file pointer
    fclose($open_image_in_binary);

    // Confirm the  new image exists in the saved_images
    // folder.
    if (file_exists($image_location)) {
        echo "Image saved successfully";
    } else {
        echo "An error occurred. Please check your code";
    }
?>

출력:

cURL을 사용하여 URL에서 이미지 저장

PHP에서 copy() 기능을 사용하여 URL에서 이미지 저장

PHP copy() 기능은 리소스를 한 위치에서 다른 위치로 복사할 수 있습니다. URL에서 이미지를 저장하려면 copy()에 URL과 새 위치를 제공하십시오.

이미지가 있는지 확인하려면 file_exists()를 사용하여 이미지가 있는지 확인하십시오.

<?php
    $image_url = 'https://i.imgur.com/CcicAAl.jpeg';
    $image_location = 'saved_images/03_robot.png';
    // Use the copy() function to copy the image from
    // its Imgur URL to a new file name in the
    // saved_images folder.
    $copy_image = copy($image_url, $image_location);
    // Confirm the  new image exists in the saved_images
    // folder.
    if (file_exists($image_location)) {
        echo "Image saved successfully";
    } else {
        echo "An error occurred. Please check your code";
    }
?>
  • 인기글
  • 최근글
  • 최근댓글