HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Edge extend

← diffMarking   fxAnalyzeImage →
Sometimes it is necessary to make an image fill a larger space than it currently occupies, without distorting the image. This can be accomplished by setting an appropriate virtualPixelMethod, and then resizing the image using the distortImage method, and the Imagick::DISTORTION_AFFINEPROJECTION constant.";

Example

function edgeExtend($virtualPixelType, $image_path)
{
    $imagick = new \Imagick(realpath($image_path));
    $imagick->setImageVirtualPixelMethod($virtualPixelType);

    $imagick->scaleImage(400, 300, true);

    $imagick->setImageBackgroundColor('red');
   
    $desiredWidth = 600;
    $originalWidth = $imagick->getImageWidth();

    $desiredHeight = 450;
    $originalHeight = $imagick->getImageHeight();

    //Make the image be the desired width.
    $imagick->sampleimage($desiredWidth, $desiredHeight);//$imagick->getImageHeight());

    // Now scale, rotate, translate (aka affine project) it
    // to be how you want
    $points = array(
        // The x scaling factor is 0.5 when the desired width is double
        // the source width
        ($originalWidth / $desiredWidth),
        0, // ($originalHeight / $desiredHeight), //0 means don't scale vertically
        0, ($originalHeight / $desiredHeight),
        // Offset the image so that it's in the centre
        ($desiredWidth - $originalWidth) / 2,
        ($desiredHeight - $originalHeight) / 2,
    );

    $imagick->distortImage(\Imagick::DISTORTION_AFFINEPROJECTION, $points, false);

    header("Content-Type: image/jpeg");
    echo $imagick->getImageBlob();

//Fyi it may be easier to think of the affine transform by 
//how it works for a rotation:
//$affineRotate = array(
//    "sx" => cos($angle),
//    "sy" => cos($angle),
//    "rx" => sin($angle),
//    "ry" => -sin($angle),
//    "tx" => 0,
//    "ty" => 0,
//);
}