Search code examples
imagemagickimagickimagemagick-convertjpeg2000

convert image 75 dpi to 300 dpi using Imagemagick PHP


I'm attempting to increase a very low resolution jp2 image to a higher DPI so that the image can been seen without any inconvenience to our eyes.

I have been successful in reading a jpeg2000 encoded string and displaying it as a PNG file. (Below is the code)

$imagedata = "AAAADGpQICANCocKAAAAFGZ0eXBqcDIgAAAAAGpwMiAAAAAtanAyaAAAABZpaGRyAAAAyAAAAKAAAwcHAAAAAAAPY29scgEAAAAAABAAAAGXanAyY/9P/1EALwAAAAAAoAAAAMgAAAAAAAAAAAAAAKAAAADIAAAAAAAAAAAAAwcBAQcBAQcBAf9SAAwAAAABAQUEBAAA/1wAI0JvGG7qbupuvGcAZwBm4l9MX0xfZEgDSANIRU/ST9JPYf9kACIAAUNyZWF0ZWQgYnk6IEpKMjAwMCB2ZXJzaW9uIDQuMf+QAAoAAAAAAQMAAf9SAAwAAAABAQUEBAAA/5PPoKgT/dHUscn3uMJWDWKb153z8hPvSInB8QsdvHSg4pzoLevV6cHhwCOWrDWed1zB8RKHyC4PEhigx/MYuIx4wci8q/CEo2kiHBrV8DhszG7ymZ/UH7atm39cdbppgIDD4VYfCrB00E+GI+Qf3v1IHzVdC6k/pMRXolANASf+TQYCTKERfZoHB65rCU23EcMzjiQo+2MAmLli7aos4tyAgMOrw6tBVpk5rPA9rz1HB6Wn+siLUizMFl3TKpn7s1pJGcCba3pGnanMUNO8OP+EwaMdppACpwb6vbqSpeUbgICAgICAgID/2Q==";

$image=base64_decode($imagedata);

// Create Imagick object
$im = new Imagick();

// Convert image into Imagick
$im->readImageBlob($image);

//Set the output format
$im->setImageFormat("png");

header('Content-type: image/png');

echo $im;

I read it is a possibility to increase the DPI using ImageMagick. See here http://www.imagemagick.org/discourse-server/viewtopic.php?t=18241

How do I achieve this in my PHP script (NOT through command line) ? Any help and guidance would be very much appreciated.


Solution

  • If you look at the UK Government website for the Passport Office, it says that passport photos need to be at least 600px wide by 750px tall.

    Let's start with a photo of adequate quality (if not content) for Mr Bean at 600x750:

    enter image description here

    If we now resize him down to the same as your image (160x200), then back up you will see the quality has suffered through trying to represent the image at 160x200 and you can't invent all those pixels you lost - they are gone for good. Look at his teeth and the highlights in his eyes:

    convert bean.jpg -resize 160x200 -resize 600x750 result.jpg
    

    enter image description here

    So, all you can do in Imagick is:

    Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = FALSE [, bool $legacy = FALSE ]] )
    

    to go back up to 600x750 and experiment with setting the filter to Catrom or Lanczos. But you can't invent stuff that isn't there...