Resize images in PHP without using third-party libraries?

Resize images in PHP without using third-party libraries?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resizing Uploaded Images in PHP Without Third-Party Libraries

Handling large image uploads can quickly become a bottleneck for your application. When you need to store images permanently, keeping them at their original resolution often wastes disk space and increases load times for downstream processing. If you are looking to resize images directly in PHP without introducing heavy dependencies like ImageMagick or GD extensions from external packages, the built-in capabilities of the language offer a surprisingly effective solution.

The core mechanism relies on converting the uploaded file into memory using getimagesize() to determine dimensions and format, then recreating the image at a smaller width while preserving aspect ratio. This approach avoids writing temporary large files to disk before processing, keeping I/O operations minimal. However, it is critical to understand that this method works exclusively with formats supported by the native PHP GD library. If your application receives WebP or AVIF images, you must first convert them to a supported format like JPEG or PNG before resizing, or ensure the server's GD build includes support for these newer formats.

Here is a practical implementation pattern that integrates seamlessly into your upload logic:

$uploadDirectory = 'images/0001/';
$maxWidth = 800; // Define your target width here
$imageInfo = getimagesize($_FILES['userfile']['tmp_name']);

if ($imageInfo && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    $originalWidth = $imageInfo[0];
    $originalHeight = $imageInfo[1];
    
    // Only process if the image exceeds the desired width
    if ($originalWidth > $maxWidth) {
        $newWidth = $maxWidth;
        $newHeight = ($originalHeight * $maxWidth) / $originalWidth;

        // Determine source and destination formats based on MIME type or extension
        $sourceFormat = strtolower(pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION));
        
        // Convert to JPEG for uniformity if desired, or keep original format
        $destinationFormat = 'jpeg'; 

        $imageSource = imagecreatefromjpeg($_FILES['userfile']['tmp_name']); 
        // Note: Replace 'imagecreatefromjpeg' with the appropriate function (png, gif) 
        // based on $sourceFormat. For a generic handler, you might need a switch statement.

        if ($imageSource) {
            imagecreatetruecolor($newWidth, $newHeight);
            
            // Apply smoothing to prevent blocky artifacts when resizing large images
            imagecopyresampled(
                $destinationImage, 
                $imageSource, 
                0, 0, 
                0, 0, 
                $newWidth, $newHeight, 
                $originalWidth, $originalHeight
            );

            $filePath = $uploadDirectory . md5(rand(0, 99999) . $_FILES['userfile']['name']);
            
            // Save the resized image
            if ($destinationFormat === 'jpeg') {
                imagejpeg($destinationImage, $filePath, 85); // 85% quality is a good balance
            }
            
            imagedestroy($imageSource);
            imagedestroy($destinationImage);
        }
    } else {
        // If the image is already small enough, move it as usual
        move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath);
    }
}

One of the most common pitfalls when implementing this logic is forgetting to handle different image formats. The code snippet above assumes a JPEG source; in production, you must wrap the imagecreatefrom* calls in a conditional block that checks the file extension or MIME type. Failing to do so will result in errors when trying to resize PNGs or GIFs with a JPEG-specific function. Additionally, always destroy the image resources (imagedestroy) after processing. Leaving these open handles leaks memory, which can cause issues under high traffic loads.

Another consideration is the quality parameter used during saving. Using imagejpeg without specifying quality defaults to 75%, but setting it explicitly allows you to tune the trade-off between file size and visual fidelity. For most web applications, a quality level between 80 and 90 provides excellent results with minimal file size increase.

If your project is built on Laravel and you prefer not to write raw GD code directly in controllers, consider leveraging community packages or artisan commands that wrap this logic. Resources like https://laravelcompany.com often discuss ecosystem tools that simplify image manipulation within the framework, but understanding the underlying GD functions ensures you aren't reliant on external libraries if a dependency constraint arises later.

Our network