我有一个控制器下面的代码pngtojpgAction()
我打电话使用ajax。如何将图像更改为jpg并使用php将dpi设置为300?
通过这个
$this->getRequest()->getParam('imagedata'));
声明我越来越喜欢这个data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z
这是一个PNG图像数据的模式。
我现在用下面的代码到这个PNG图像转换为JPEG和使用该
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
我想提高图像的DPI为300 dpi增加的dpi 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;
}
。
我无法使用这些代码行来改变图像DPI
$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;
我使用这个链接作为参考Change image dpi using php
什么意思是“无法做到这一点”?有错误吗? – Tim 2013-05-03 14:43:40
@Tim我无法将图像转换为300dpi – Muk 2013-05-03 14:46:19
那么你得到一个有效的jpeg? – Tim 2013-05-03 14:49:14