Converting DURING loading before saving to png / gif to jpg server

So, I have an image loading script. It downloads the image and saves it in space on the server. What I can't seem to think of is to say when the user downloads .png, by the time he saves on my server, I want it to be jpg.

Can someone help with this, and please do not just direct me to another question, as I have nothing working. Here is an example of my code.

$name = addslashes($_FILES['image']['name']); $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $size = $_FILES['image']['size']; $temp = $_FILES ['image']['tmp_name']; $error = $_FILES ['image']['error']; if ($error > 0) die("Error uploading file! Code $error."); else if ($password == "" || $size > 2000000) { move_uploaded_file($temp, $images.$name); mysql_query("INSERT INTO image_approval VALUES ('','$description','','$images$name','',NOW())"); echo "Upload complete!"; }else{ echo "Error uploading file"; } 
+2
source share
2 answers

Using GD , and suppose that $images is the directory in which you store your images (with the slash ending), and $name is the file name of the original image:

 $destinationPath = $images . basename($name, $ext) . '.jpg'; $source = imagecreatefrompng($images . $name); imagejpeg($source, $destinationPath, 75); imagedestroy($source); 

Or with Imagick :

 $image = new Imagick($images . $name); $image->writeImage($destinationPath); $image->destroy(); 
+1
source

Use this function to convert the downloaded image.

 // http://stackoverflow.com/a/1201823/358906 // Quality is a number between 0 (best compression) and 100 (best quality) function png2jpg($originalFile, $outputFile, $quality) { $image = imagecreatefrompng($originalFile); imagejpeg($image, $outputFile, $quality); imagedestroy($image); } 

Then delete the old image using unlink() .

Your code will look something like this:

 // After the upload png2jpg($the_jpg_file_path, $the_png_file_path, 80); unlink($the_jpg_file_path); 
0
source

All Articles