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.
Use the following script to convert any image(JPEG, PNG and GIF) to PNG format. Go through the following script carefully, I have added comments at every critical step.
// $dir specifies the directory where you upload your image files
// get the file by it's temporary name
$tmp_file_name = $_FILES['image']['tmp_name'];
// get the file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
// specify the whole path here
$actual_file_name = $dir . basename($_FILES['image']['name'], "." . $ext) . ".png";
// check whether a valid image is uploaded or not
if(getimagesize($tmp_file_name)){
// get the mime type of the uploaded image
$image_array = getimagesize($tmp_file_name);
$mime_type = $image_array['mime'];
// get the height and width of the uploaded image
list($width_orig, $height_orig) = getimagesize($tmp_file_name);
$width = $width_orig;
$height = $height_orig;
if($mime_type == "image/gif"){
// create a new true color image
if($image_p = imagecreatetruecolor($width, $height)){
// create a new image from file
if($image = imagecreatefromgif($tmp_file_name)){
// copy and resize part of an image with resampling
if(imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)){
if(imagepng($image_p, $actual_file_name, 0)){
// image is successfully uploaded
// free resources
imagedestroy($image_p);
imagedestroy($image);
// perform the insert operation and get the last inserted id
// $lastinsertID = XXXX
// new file name
$filename = $dir . $lastinsertID . basename($_FILES['image']['name'], "." . $ext) . ".png";
//move the file to your desired location
if(rename($actual_file_name, $filename)){
echo "success";
}else{
echo "error";
}
}else{
//Destroy both image resource handler
imagedestroy($image);
imagedestroy($image_p);
echo "Error";
}
}else{
//Destroy both image resource handlers
imagedestroy($image);
imagedestroy($image_p);
echo "Error";
}
}else{
//destroy $image_p image resource handler
imagedestroy($image_p);
echo "Error";
}
}else{
echo "Error";
}
}elseif($mime_type == "image/png"){
// the uploaded image is already in .png format
if(move_uploaded_file($tmp_file_name, $actual_file_name)){
// perform the insert operation and get the last inserted id
// $lastinsertID = XXXX
// new file name
$filename = $dir . $lastinsertID . $_FILES['image']['name'];
//move the file to your desired location
if(rename($actual_file_name, $filename)){
echo "success";
}else{
echo "error";
}
}else{
echo "error";
}
}elseif($mime_type == "image/jpeg"){
// create a new true color image
if($image_p = imagecreatetruecolor($width, $height)){
// create a new image from file
if($image = imagecreatefromjpeg($tmp_file_name)){
// copy and resize part of an image with resampling
if(imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)){
if(imagepng($image_p, $actual_file_name, 0)){
// image is successfully uploaded
// free resources
imagedestroy($image_p);
imagedestroy($image);
// perform the insert operation and get the last inserted id
// $lastinsertID = XXXX
// new file name
$filename = $dir . $lastinsertID . basename($_FILES['image']['name'], "." . $ext) . ".png";
//move the file to your desired location
if(rename($actual_file_name, $filename)){
echo "success";
}else{
echo "error";
}
}else{
//Destroy both image resource handler
imagedestroy($image);
imagedestroy($image_p);
echo "Error";
}
}else{
//Destroy both image resource handlers
imagedestroy($image);
imagedestroy($image_p);
echo "Error";
}
}else{
//destroy $image_p image resource handler
imagedestroy($image_p);
echo "Error";
}
}else{
echo "error_An unexpected error has been occured. Please try again later.";
}
}else{
echo "Only JPEG, PNG and GIF images are allowed.";
}
}else{
echo "Bad image format";
}