- PHP에서
file_get_contents()
및file_put_contents()
를 사용하여 URL에서 이미지 저장 cURL
을 사용하여 URL에서 이미지 저장- PHP에서
copy()
기능을 사용하여 URL에서 이미지 저장 - PHP에서
fread()
및fwrite()
를 사용하여 URL에서 이미지 저장 - PHP에서
gzip
이미지 저장

이 기사에서는 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.";
}
?>
출력:
;
$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";
}
?>
출력:
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";
}
?>
댓글 0
팁&유틸
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
8 |
xampp php8.2 ffmpeg 설치
![]() | 공수래 | 2024.02.24 | 103 |
7 |
jQuery Waterwheel Carousel
![]() | 공수래 | 2024.02.15 | 70 |
6 | 이미지 태그의 width, height 스타일이 모두 명시되어야 하는 이유 | 공수래 | 2024.02.12 | 161 |
5 | PHP에서 이미지 크기 조정 | 공수래 | 2024.02.11 | 137 |
» | PHP의 URL에서 이미지 저장 | 공수래 | 2024.02.11 | 218 |
3 | 이미지 비율 유지 축소 | 공수래 | 2024.02.11 | 57 |
2 | 이미지 사이즈 비율로 조정 | 공수래 | 2024.02.11 | 97 |
1 | php 이미지 리사이징 | 공수래 | 2024.02.11 | 114 |