Reputation: 7778
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
Upvotes: 2
Views: 11118
Reputation: 2029
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.
Class behind link
<?php
/**
* Class used for resampling images
*/
class Resampler
{
/**
* Resample an image
*
* The image is returned as a string ready to be saved, it is not converteed back to a resource
* as that would just be unnecessary
*
* @param resource $image Resource storing JPEG image
* @param integer $dpi The dpi the image should be resampled at
* @return string Resampled JPEG image as a string
*/
function resample($image, $height, $width, $dpi = 300)
{
if(!$image)
{
throw new \Exception('Attempting to resample an empty image');
}
if(gettype($image) !== 'resource')
{
throw new \Exception('Attempting to resample something which is not a resource');
}
//Use truecolour image to avoid any issues with colours changing
$tmp_img = imagecreatetruecolor($width, $height);
//Resample the image to be ready for print
if(!imagecopyresampled ($tmp_img , $image , 0 , 0 ,0 , 0 , $width , $height , imagesx($image) , imagesy($image)))
{
throw new \Exception("Unable to resample image");
}
//Massive hack to get the image as a jpeg string but there is no php function which converts
//a GD image resource to a JPEG string
ob_start();
imagejpeg($tmp_img, "", 100);
$image = ob_get_contents();
ob_end_clean();
//change the JPEG header to 300 pixels per inch
$image = substr_replace($image, pack("Cnn", 0x01, $dpi, $dpi), 13, 5);
return $image;
}
}
Upvotes: 3
Reputation: 173
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.
Upvotes: 0
Reputation: 7778
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);
}
Upvotes: 5