简体   繁体   English

将png转换为jpg并下载jpg图像

[英]convert png to jpg and download jpg image

I have given an option to user to Export Images As JPG and I have stored png images 我为用户提供了将图像导出为JPG的选项,并且已存储png图像
So, i have created some php script to do so. 所以,我已经创建了一些PHP脚本来做到这一点。
png is converting fine.. png转换良好。
but when i downloaded the jpg image and try to open in image viewer, its showing corrupted. 但是当我下载jpg图片并尝试在图片查看器中打开时,其显示已损坏。
My php code: 我的PHP代码:

Converting PNG to JPG 将PNG转换为JPG

$input_file = 'images/image1364926178.png';
$filename = time().'.jpg';
$output_file = 'images/'.$filename;                 
$input = imagecreatefrompng($input_file);
if($input){
    list($width, $height) = getimagesize($input_file);
    $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);
    imagejpeg($output,$output_file);
}

Script downloading jpg image 脚本下载jpg图片

$Image_file_name = '/images/'.$filename;
$filedetail = getimagesize($filename);
$mimetype = $filedetail['mime'];
if (file_exists($filename)) {
    @ob_end_clean();                    
    header('Content-Description: File Transfer');
    header('Content-Type: '.$mimetype);
    header('Content-Disposition: attachment; filename='.basename($filename));
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    header('Cache-Control: private');
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
}

What am i doing wrong? 我究竟做错了什么?
OR 要么
Is there any better idea for how to do that? 有什么更好的主意吗?

Thanks in Advance 提前致谢

A simple google let me get this question on stackoverflow , a simple function is given to convert images. 一个简单的谷歌让我在stackoverflow上得到这个问题 ,给出了一个简单的函数来转换图像。

For the correct jpg headers I found this answer . 对于正确的jpg标头,我找到了这个答案

Combining the two should do the job 结合两者应该做的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM