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
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);
}
I would use imagemagic instead:
convert Bird.jpg -density 300 Bird2.jpg
But you could allso do that with GD.
$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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.