Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created February 14, 2013 09:29
Show Gist options
  • Save hubgit/4951551 to your computer and use it in GitHub Desktop.
Save hubgit/4951551 to your computer and use it in GitHub Desktop.
<?php
function readInput() {
$file = 'test.png';
if (!file_exists($file)) {
copy('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', $file);
}
$im = new Imagick();
$im->readImage($file);
$im->setImageFormat('JPEG');
$im->setImageCompression(\Imagick::COMPRESSION_JPEG);
return $im;
}
// write directly to file with .jpg extension
$im = readInput();
$im->writeImage('test-writeimage.jpg');
// less test-writeimage.jpg:
// test-writeimage.jpg JPEG 800x600 800x600+0+0 8-bit DirectClass 47.7KB 0.000u 0:00.000
// write to file via string
$im = readInput();
file_put_contents('test-getimageblob.jpg', $im->getImageBlob());
// less test-getimageblob.jpg:
// test-getimageblob.jpg JPEG 800x600 800x600+0+0 8-bit DirectClass 47.7KB 0.000u 0:00.000
// write to resource
$im = readInput();
$output = fopen('test-writeimagefile.jpg', 'w');
$im->writeImageFile($output);
// less test-writeimagefile.jpg:
// test-writeimagefile.jpg PNG 800x600 800x600+0+0 8-bit DirectClass 235KB 0.000u 0:00.000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment