Frank
Frank

Reputation: 624

uploading converted image to server

Below you can see the script that, if a user provides a .jpg-file, uploads it to the server directly, but when the user provides a .png-file, uploads it as well and then converts it to .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);

What I would like to know is how to upload that new converted .jpg (imagejpeg($output, $output_file)) to my server. Do I use move_uploaded_file as well? Thanks!

PS: I know, MySQL is outdated and dangerous, will change that asap!

Upvotes: 0

Views: 66

Answers (1)

M-Garebaghi
M-Garebaghi

Reputation: 89

Change this

$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.jpg';

to

$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.'.$ext;

The problem is with your output file extension

And your function

imagejpeg($output, $output_file);

Upvotes: 1

Related Questions