I am trying to convert a JPG (well any image) to PNG. I have an HTML form that posts the image just fine to the server. I need to rename that file and also convert it to PNG. Later in my code after I do a related table database insert I rename the file yet again to append the record ID to the file name to ensure its uniqueness.
I am more of an objective C programmer then PHP, so i'm struggling here with this code I have found in other questions that does not seem to work for me.
Here is print_r($_FILES);
Array ( [image] => Array ( [name] => BBnL9Ho.jpg [type] => image/jpeg [tmp_name] => /tmp/phphhqHam [error] => 0 [size] => 1636 ) )
So, I want to convert it to PNG and rename BBnL9Ho.jpg to image1.png
. I have tried using the following code, but to no avail:
$newfileName = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), "image1.png");
Later after I do a related database table INSERT, I change the name again and append the ID of the related database record (I store the filename in separate table then rest of form data due to one to many relationship):
$fileName="$lastinsertID".$newfileName;
Then I INSERT that name into the database which enters correctly. I then need to move the file to an uploads directory which I attempt to do like so:
move_uploaded_file("$fileName",$dir . $fileName);
Here is where my problem is. The file does not move AND when I do a check on the attributes of the file, it seems it did not actually convert the file. I use this to check the type:
$fileType = $_FILES["image"]["type"];
and it still shows it is a JPG. I must be missing something very obvious but I would appreciate some assistance.
Thank you very much.