php将用户图像转换为jpg


php convert user image to jpg

可能重复:
如何在PHP中将所有图像转换为JPG格式?

我正在尝试将用户名传递到文件名中,并使文件以.jpg结尾,因此输出将是"user-mycolnewimage.jpg"。目前关于如何做到这一点的任何建议都是更改为"randnumber-mycolnnewimage.png",这不是我想要的。随机数对服务器日志没有帮助,图像需要以jpg结尾,因为它正在被转换。

$file = rand(0, 10000000).$_FILES['t1']['name'];
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) {
    if($fp = fopen($file,"rb", 0)) {
        $picture = fread($fp,filesize($file));
        fclose($fp);
        $image = imagecreatefrompng($file);
        imagejpeg($image, $file, 70);
        imagedestroy($image);
        $tag1 = '<img src="'.$file.'" alt="" class="default" />';
        //unlink($file);        
     }
}

我想您要找的是basename(此处记录(

您可以做的一件事是重新编写您的脚本,以便创建两个不同的$file变量,一个包含初始png,另一个包含jpeg路径。你还需要unlink你的png一旦完成:

$source = $username . "-" . $_FILES['t1']['name'];
$destination = $username . "-" . basename($_FILES['t1']['name']) . ".jpg";
if (move_uploaded_file($_FILES['t1']['tmp_name'], $source)) {
    if($fp = fopen($source,"rb", 0)) {
        $picture = fread($fp,filesize($source));
        fclose($fp);
        $image = imagecreatefrompng($source);
        imagejpeg($image, $destination, 70);
        imagedestroy($image);
        $tag1 = '<img src="'.$file.'" alt="" class="default" />';
        unlink($source);        
     }
}