Current Location: Home> Latest Articles> Complete Guide to Creating Mosaic Effects on Images with PHP and Imagick

Complete Guide to Creating Mosaic Effects on Images with PHP and Imagick

M66 2025-07-28

Creating Mosaic Effects on Images with PHP and Imagick

The mosaic effect is a common image editing technique widely used for privacy protection and hiding sensitive content. By dividing the image into small blocks and applying blur, the detailed information can be effectively concealed. This article explains how to achieve this effect using PHP combined with the Imagick library.

Preparation: Installing the Imagick Extension

Before starting, make sure the Imagick extension is installed in your PHP environment. You can check it by running the following command:

$ php -m | grep imagick

If the output includes "imagick", the extension is installed. Otherwise, you need to install it first.

PHP Code Example for Creating Mosaic Effect

The following code demonstrates how to perform mosaic processing on an image using the Imagick class. The logic is straightforward and easy to understand or extend:

<?php
function mosaicImage($inputImage, $outputImage, $blockSize) {
  
  // Create Imagick object and read image
  $imagick = new Imagick();
  $imagick->readImage($inputImage);
  
  // Get image width and height
  $width = $imagick->getImageWidth();
  $height = $imagick->getImageHeight();
  
  // Calculate the number of mosaic blocks
  $blocksX = $width / $blockSize;
  $blocksY = $height / $blockSize;
  
  // Loop through each mosaic block and apply blur
  for ($y = 0; $y < $blocksY; $y++) {
    for ($x = 0; $x < $blocksX; $x++) {
      $startX = $x * $blockSize;
      $startY = $y * $blockSize;
      
      // Set the current processing region
      $imagick->setImagePage($blockSize, $blockSize, $startX, $startY);
      
      // Apply blur to create mosaic effect
      $imagick->blurImage($blockSize / 10, $blockSize / 10);
    }
  }
  
  // Save the processed image
  $imagick->writeImage($outputImage);
  
  // Release resources
  $imagick->destroy();
}

// Set input, output paths and mosaic block size
$inputImage = "input.jpg";
$outputImage = "output.jpg";
$blockSize = 20;

// Execute mosaic processing
mosaicImage($inputImage, $outputImage, $blockSize);
?>

Code Explanation

The code defines a function named mosaicImage that accepts the input image path, output image path, and mosaic block size as parameters. It instantiates the Imagick object and loads the image, then calculates how many mosaic blocks the image can be divided into.

Using nested loops, it traverses each block, sets the region with setImagePage, applies the blur effect with blurImage, and finally saves the result using writeImage.

Summary

Using PHP and the Imagick library, you can easily implement image mosaic processing. This method is clear in structure, highly extensible, and suitable for scenarios requiring image privacy protection or special visual effects. Adjusting the mosaic block size allows flexible control over the blur intensity and image detail.