2

I have following code in a controller under pngtojpgAction() which I am calling using ajax.

Through this

$this->getRequest()->getParam('imagedata'));

statement I am getting a pattern like this data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z

Which is a png image data.

now I am using following code to convert this png image to jpeg and to increase dpi to 300.

public function pngtojpgAction()   
{
    //Code to convert png to jpg image
    $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
    $width=imagesx($input);
    $height=imagesy($input);
    $output = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($output,  255, 255, 255);
    imagefilledrectangle($output, 0, 0, $width, $height, $white);
    imagecopy($output, $input, 0, 0, 0, 0, $width, $height);  
     
    ob_start();
    imagejpeg($output);
    $contents =  ob_get_contents();
    ob_end_clean();
     //echo 'data:image/jpeg;base64,'.base64_encode($contents); /*Up to here code works well*/
    
     $jpgImage='data:image/jpeg;base64,'.base64_encode($contents);

            
     $image = file_get_contents($jpgImage);

     $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

     header("Content-type: image/jpeg");
     header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
     echo  $image;
    
}

using this

 $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

I want to increase dpi of image to 300 dpi.

I am unable to change the image dpi using these line of code

$jpgImage='data:image/jpeg;base64,'.base64_encode($contents);

$image = file_get_contents($jpgImage);

$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
echo  $image;

I used this link as reference Change image dpi using php

Mukesh
  • 7,630
  • 21
  • 105
  • 159

3 Answers3

5

After making some changes it worked for me.

public function pngtojpgAction()   
{
            //Code to convert png to jpg image
        $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
        $width=imagesx($input);
        $height=imagesy($input);
        $output = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($output,  255, 255, 255);
        imagefilledrectangle($output, 0, 0, $width, $height, $white);
        imagecopy($output, $input, 0, 0, 0, 0, $width, $height);                  

        ob_start();
        imagejpeg($output);
        $contents =  ob_get_contents();
        //Converting Image DPI to 300DPI                
        $contents = substr_replace($contents, pack("cnn", 1, 300, 300), 13, 5);             
        ob_end_clean();     
        echo 'data:image/jpeg;base64,'.base64_encode($contents); 

}
Mukesh
  • 7,630
  • 21
  • 105
  • 159
  • 1
    But my personal experience says that image magick library is better for image and dpi conversion.This gives better quality compared to GD library. – Mukesh Aug 02 '13 at 11:20
  • can you please tell me how i can use imag magick in magento site to make product page as like [here](http://www.dailyobjects.com/custom-cases/apple/iphone-4-4s/custom-iphone-4-4s-case/slim) – fresher Jul 18 '16 at 12:12
3

I would use imagemagic instead:

convert Bird.jpg -density 300 Bird2.jpg

But you could allso do that with GD.

Link to class

$filename = 'Bird.jpg';
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);

$b = new Resampler;
$im = $b->resample($source, $height, $width, 300);

file_put_contents('Bird2.jpg', $im);

Tested on Windows enviroment.

Aivar
  • 2,029
  • 20
  • 18
0

Simple Code to Re-generate image with adjustable quality.

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
    $image = imagecreatefromjpeg($source_url);
    imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}
echo compress_image($_FILES["file"]["tmp_name"], "destination .jpg", 80);//Adjust Quality

Note: Ensure that the GD library for PHP is installed.

abhi chavda
  • 153
  • 1
  • 11