function sampleImage($image_path, $width, $height, $preshrink) { $imagick = new \Imagick(realpath($image_path)); if ($preshrink) { // Easier to see pixel when source image is tiny. $imagick->scaleImage( $imagick->getImageWidth() / 8, $imagick->getImageHeight() / 8 ); } $imagick->sampleImage($width, $height); header("Content-Type: image/jpeg"); echo $imagick->getImageBlob(); }