下面您可以看到脚本,如果用户提供.jpg
- 文件,则直接将其上传到服务器,但当用户提供.png
文件时,也会上传该文件,然后将其转换为.jpg
。
$path = $_FILES["uploaded_image"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION); //extracts file extension
$tempimgloc = $_FILES["uploaded_image"]["tmp_name"];
$imagepic = ''.$_SESSION['userid'].'-'.$_SESSION['username'].'.'.$ext.'';
$img_url = 'img/uploaded/'.$imagepic.'';
if($ext == 'jpg'){
move_uploaded_file($tempimgloc, "img/uploaded/".$imagepic);
mysql_query('UPDATE table SET img = "'.$img_url.'" WHERE id = '.$_SESSION['userid'].'');
} elseif($ext == 'png'){
move_uploaded_file($tempimgloc, "img/uploaded/".$imagepic);
mysql_query('UPDATE table SET img = "'.$img_url.'" WHERE id = '.$_SESSION['userid'].'');
$input_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.png';
$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.jpg';
$input = imagecreatefrompng($input_file);
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);
我想知道的是如何将新转换的.jpg
(imagejpeg($output, $output_file)
)上传到我的服务器。我也使用move_uploaded_file
吗?谢谢!
答案 0 :(得分:1)
更改此
$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.jpg';
到
$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.'.$ext;
问题在于您的输出文件扩展名
你的功能
imagejpeg($output, $output_file);