Current Location: Home> Latest Articles> How to Save Remote Images Locally and Generate Thumbnails with PHP

How to Save Remote Images Locally and Generate Thumbnails with PHP

M66 2025-06-15

Practical Method to Save Remote Images Locally and Generate Thumbnails with PHP

When developing websites or applications, it’s common to need to download remote images and save them locally, then generate thumbnails to improve page load speed and reduce bandwidth usage. This article introduces how to achieve this using PHP, focusing on generating thumbnails with the GD library.

1. Download Remote Images to the Local Server

In PHP, you can use the file_get_contents() function to read remote image data, then use file_put_contents() to save the image content to a local path on the server.

<?php
// Remote image URL
$remoteImageUrl = "http://example.com/image.jpg";
// Local path to save image
$localImagePath = "/path/to/local/image.jpg";
<p>// Read remote image content<br>
$imageContent = file_get_contents($remoteImageUrl);<br>
// Save to local<br>
file_put_contents($localImagePath, $imageContent);<br>
?><br>

In this code, $remoteImageUrl is the remote image URL, and $localImagePath is the local path to save the image. This snippet downloads the remote image to your local server.

2. Generate Thumbnails Using the GD Library

The GD library is a widely used PHP extension for creating and manipulating images efficiently. The basic process for generating thumbnails is to create an image resource from the original image, calculate the scaled dimensions, create a thumbnail resource, then save the generated thumbnail.

<?php
// Original image path
$originalImagePath = "/path/to/local/image.jpg";
// Thumbnail image path
$thumbnailImagePath = "/path/to/local/thumbnail.jpg";
// Thumbnail size (max width or height)
$thumbnailSize = 200;
<p>// Create original image resource<br>
$originalImage = imagecreatefromjpeg($originalImagePath);</p>
<p>// Get original image dimensions<br>
$originalWidth = imagesx($originalImage);<br>
$originalHeight = imagesy($originalImage);</p>
<p>// Calculate scaled dimensions<br>
if ($originalWidth > $originalHeight) {<br>
$thumbnailWidth = $thumbnailSize;<br>
$thumbnailHeight = intval($originalHeight / $originalWidth * $thumbnailSize);<br>
} else {<br>
$thumbnailHeight = $thumbnailSize;<br>
$thumbnailWidth = intval($originalWidth / $originalHeight * $thumbnailSize);<br>
}</p>
<p>// Create thumbnail resource<br>
$thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);</p>
<p>// Resample original image into thumbnail<br>
imagecopyresampled($thumbnailImage, $originalImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $originalWidth, $originalHeight);</p>
<p>// Save the thumbnail<br>
imagejpeg($thumbnailImage, $thumbnailImagePath);</p>
<p>// Free resources<br>
imagedestroy($originalImage);<br>
imagedestroy($thumbnailImage);<br>
?><br>

This code loads the original image with imagecreatefromjpeg(), retrieves its width and height via imagesx() and imagesy(), calculates the proper thumbnail size, creates a true color thumbnail image resource, scales the original image into the thumbnail, and finally saves it with imagejpeg().

Summary

Using the above approach, developers can easily implement remote image saving and thumbnail generation to enhance website loading performance and user experience. It’s recommended to incorporate error handling and permission checks in real projects to ensure reliability and security during image processing.