save()

public function save(?string $filename = null, int $compression = 100, ?int $permissions = null): void

Description

Saves the currently loaded and processed image to disk. This method writes the image resource to a file with optional compression settings for JPEG and WEBP formats, and allows setting file permissions. The method automatically handles format-specific requirements such as transparency preservation for PNG images.

Load and Process First: This method requires an image to be loaded first using load() or upload(). Any processing operations (resize, crop, etc.) must be performed before calling save(). The method saves the current state of the image resource.

Parameters

Parameter Type Description Default Required
$filename string|null The path where the image will be saved. If null, uses the internal filename from load(). null No
$compression int Compression level for JPEG/WEBP (0-100). Higher values = better quality, larger files. Has no effect on GIF/PNG. 100 No
$permissions int|null File permissions in octal format (e.g., 0644, 0755). If null, uses system defaults. null No

Understanding Compression

The $compression parameter controls the quality vs. file size trade-off for JPEG and WEBP images:

  • 100: Maximum quality, largest file size - ideal for archival or source images
  • 85-95: High quality, reasonable file size - recommended for most web use
  • 70-84: Good quality, smaller files - suitable for thumbnails or bandwidth-conscious applications
  • Below 70: Noticeable quality loss - generally not recommended unless file size is critical

Format-Specific Behaviour: The compression parameter only affects JPEG and WEBP images. PNG uses lossless compression automatically, and GIF has no compression options. PNG images also have transparency (alpha channel) preserved automatically via imagesavealpha().

Return Value

Type Description
void This method does not return a value. It writes the image to disk.

Example #1

The code sample below demonstrates the most basic use of save().

The Standard Workflow: The typical pattern is load → manipulate → save. All processing operations (resize, crop, etc.) modify the image resource in memory. Call save() as the final step to write the processed image to disk. The compression value of 85 provides excellent quality whilst significantly reducing file size compared to 100.

Example #2

The example above shows how to save an image with specific permissions for security.

Security Through Permissions: Use the $permissions parameter to control file access. Common values include 0644 (owner read/write, others read), 0600 (owner read/write only), and 0755 (owner full access, others read/execute). For sensitive images, use restrictive permissions like 0600 and store outside the public directory.

Example #3

The example above demonstrates creating multiple quality versions of the same image.

Compression Strategy: Different use cases require different compression levels. Use high compression (95-100) for archival copies, moderate compression (80-90) for web display, and lower compression (70-80) for thumbnails where small file size matters more than perfect quality. This multi-quality approach allows you to serve appropriate versions based on context.

Example #4

The example above shows saving processed images whilst preserving transparency for PNG format.

Transparency Preservation: When working with PNG images that contain transparency (alpha channels), the save() method automatically preserves transparency through the imagesavealpha() function. This happens regardless of the compression parameter value. This is essential for logos, icons, and overlay images that require transparent backgrounds.