我想问一下PHP怎么把上转的PNG图片转力JPG格式??
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg('test.jpg');
imagejpeg($image,'',100);
imagedestroy($image);
?>
这是输出JPG的,要怎么改才能进入PNG图片转成JPG

解决方案 »

  1.   


    打开png图片
    imagecreatefrompng创建一个新的图片构造器
    imagecreatetruecolor copy刚才png的图片到新的jpg图片构造器
    imagecopyresampled
    <?php
    // The file
    $filename = 'test.png';
    $percent = 0.5;// Content type
    header('Content-type: image/jpeg');// Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $width * $percent;
    $new_height = $height * $percent;// Resample
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefrompng($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);// Output
    imagejpeg($image_p, null, 100);imagedestroy($image_p);
    imagedestroy($image);
      

  2.   

    imagejpeg($image,'文件名',100);
      

  3.   


    测试了一下,加了文件名之后会出错.错误信息如下:图像http://localhost/test.php因其本身有错无法显示.
    这个和页面之前有任何html代码或者空格的现象一样.
    暂时不知道为什么.