PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to change Image to jpg and set dpi to 300 using php?

 October 10, 2022     gd, magento, php     No comments   

Issue

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


Solution

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); 

}


Answered By - Mukesh
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing