【发布时间】:2013-05-03 14:36:49
【问题描述】:
我在pngtojpgAction() 下的控制器中有以下代码,我使用ajax 调用它。
通过这个
$this->getRequest()->getParam('imagedata'));
声明我得到了这样的模式data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z
这是一个png图像数据。
现在我正在使用以下代码将此 png 图像转换为 jpeg 并将 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;
}
使用这个
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
我想将图像的 dpi 增加到 300 dpi。
我无法使用这行代码更改图像 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 我无法将图像转换为 300dpi
-
所以你得到一个有效的jpeg?
-
@Tim 是的,我得到了一个有效的 jpeg 图像。现在我想将 jpeg 图像设置为 300dpi
-
您可以使用 imagemagick stackoverflow.com/q/4076936/1100089 或尝试此方法 php.net/manual/de/function.imagejpeg.php#86858