webp Converter

Loading

<?php

if(isset($_POST['submit'])){

if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg","png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];

$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) {
echo "Error: Please select a valid file format.";
}
else{

$maxsize = 2 * 1024 * 1024;
if($filesize > $maxsize) {
echo "Error: File size should be less than 2 MB.";
}
else{

if(in_array($filetype, $allowed)){
$uniqid=uniqid();
if(file_exists("upload/" . $uniqid."_".$filename)){
echo $uniqid."_".$filename . " already exists.";
} else{

copy($_FILES["photo"]["tmp_name"], "upload/". $uniqid."_".$_FILES["photo"]["name"]);
if (strtolower($ext) == 'jpeg' || strtolower($ext) == 'jpg')
$image = imagecreatefromjpeg("upload/". $uniqid."_".$_FILES["photo"]["name"]);
elseif (strtolower($ext) == 'png')
$image = imagecreatefrompng( "upload/". $uniqid."_".$_FILES["photo"]["name"]);
$im=imagewebp( $image, "upload/webp/".$uniqid.".webp", 100);
header("Content-type: image/webp");
header("Cache-Control: no-store, no-cache");

header('Content-Disposition: attachment; filename='. "upload/webp/".$uniqid.".webp");
header('Content-Length: '.filesize("upload/webp/".$uniqid.".webp"));
ob_clean();
flush();
readfile("upload/webp/".$uniqid.".webp");
//copy($_FILES["photo"]["tmp_name"], "upload/". $uniqid."_".$filename);
echo "Your file was converted successfully.";
}
} else{
echo "Error in uploading image.";
}
}
}
}
}

?>

<form action="" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="photo">Image:</label>
<input type="file" name="photo" id="photo">
<input type="submit" name="submit" value="Submit">
<p>Only .jpg, .jpeg, .png are allowed (max fie size 2 MB.)</p>
</form>