0

I have given an option to user to Export Images As JPG and I have stored png images
So, i have created some php script to do so.
png is converting fine..
but when i downloaded the jpg image and try to open in image viewer, its showing corrupted.
My php code:

Converting PNG to 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

$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

4

1 回答 1

1

一个简单的 google 让我在 stackoverflow 上得到了这个问题,给出了一个简单的函数来转换图像。

对于正确的 jpg 标题,我找到了这个答案

将两者结合起来应该可以完成工作

于 2013-05-15T12:45:21.233 回答