script for PNG to JPG & JPG to PNG

So without further talks, let’s jump into code But before that check this demo out to know what our final project looks like:
Demo of Image converter

File Structure
Since this project contains multiple files we will define a folder and file structure as well As usual, we will give you complete details So the file will be as mentioned in the below picture:
UI of Image Converter
Now that we are aware of all files we need to make, let’s start with the main file “index.php“ And furthermore, we will divide our code for simplicity:
● “header.php“: this file contains only the basic details of the navigation bar of our JPG to PNG image converter And we will include this file in our “index php“ With a separate header file, we don’t need to code navigation related changes to all files. One of the benefits of using a backend language like PHP
● “footer.php“: Like the header file, our footer content is separated into another file called “footerphp” For now, we only writing basic code And loading our vital javascript file for Bootstrap 5.
● “social _link.php“: this file is purely for enabling those awesome looking social icons that you see on all modern websites
Now let’s see the UI/UX code of our PNG to JPG converter “headerphp“
Folder and File Structure PNG to JPG Image Converter<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF 8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device width, initial scale=1.0"> <title>Image Converter | JPG to PNG | JPG to GIF | PNG to JPG | PNG to GIF | GIF to JPG | GIF to PNG | JPEG to PNG | JPEG to GIF</title>
<meta name="title" content="Image Converter">
<meta name="description" content="Ads Free Image Converter that allows you to convert any image to JPG, PNG, and GIF You can convert these and vice versa too We promise zero ads ">
<meta name="keywords" content="Image Converter, JPG Converter, PNG Converter, GIF Converter, JPG to PNG, JPG to GIF, PNG to JPG, PNG to GIF, GIF to JPG, GIF to PNG, JPEG to PNG, JPEG to GIF">
<meta name="robots" content="index, follow">
<meta http equiv="Content Type" content="text/html; charset=utf 8">
<meta name="language" content="English">
<meta name="revisit after" content="7 days">
<meta name="author" content="Pawan">
<! Favicon >
<link rel="apple touch icon" sizes="180x180" href="img/apple touch icon png"> <link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32 png"> <link rel="icon" type="image/png" sizes="16x16" href="img/favicon 16x16.png">
<! Links > <link href="https://cdn jsdelivrnet/npm/bootstrap@5 2 0/dist/css/bootstrap min css" rel="stylesheet"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts gstatic com" crossorigin> <link href="https://fonts googleapis com/css2?family=Quicksand&display=swap" rel="stylesheet"> <link rel="stylesheet" href="css/style.css">
class="d-flex
class="navbar
min-vh-100">
//import the converter class require('image converter.php'); if ($ FILES) { $obj = new Image converter(); //call upload function and send the $ FILES, target folder and input name $upload = $obj >upload image($ FILES, 'uploads', 'fileToUpload'); if ($upload) { $imageName = urlencode($upload[0]); $imageType = urlencode($upload[1]); if ($imageType == 'jpeg') { $imageType = 'jpg'; } header('Location: convert.php?imageName=' . $imageName . '&imageType=' . $imageType); } } ?> <?php include("includes/header.php"); ?> <div class="container pb 4"> <div class="row"> <div class="col col-lg-12 col-md-12 col-sm-12"> <h1 class="text center h2 fw bold">Image Converter</h1> <h2 class="text center h4">ConvertAny image to JPG, PNG, GIF</h2> <br> <div class="card text center mx auto p 2 gradient card"> <div class="card body"> <h4 class="card title h4 fw bold">Upload and Convert Image of your choice</h4> <p class="card-text">JPG to PNG | JPG to GIF | PNG to JPG | PNG to GIF | GIF to JPG | GIF to PNG | JPEG to PNG | JPEG to GIF</p> </div> <div class="card body"> <form action="" enctype="multipart/form data" method="post" onsubmit="return checkEmpty()"> <div class="mb 3"> <label for="fileToUpload" class="form-label">Upload file</label> <input type="file" class="form control" name="fileToUpload" id="fileToUpload"> </div> <button type="submit" class="btn btn warning btn lg fw bold"><i class="fa solid fa-upload"></i> Upload</button> </form> </div> </div> </div> </div> <?php include once("includes/social link php"); ?> </div> <?php include("includes/footer.php"); ?>
Note: Here are some important points to keep in mind when to convert png to jpg or vice versa.
● “image_converter.php“: this is an important file for our project Hence we have made it require Unlike “headerphp” or “social link php” Why? Read this article about the difference between require() and include function.
● In our image uploading form code, we added “enctype=”multipart/form data“ This is a compulsory HTMLattribute if you want to upload a file in your HTMLform Read more about the Enctype attribute in this MDN guide
Lastly,
class="bg dark mt auto"> <div class="container
<p class="text light text center py 3">@2022 Image Converter ||All Right Reserved || <a href="https://beproblemsolvercom">Designed by Be Problem Solver</a></p> </div> </footer> </body> <! JavaScript Bundle
> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://kit.fontawesome.com/0a82b36f45.js" crossorigin="anonymous"></script> <script src="js/main js"></script> </html>
color: #3b5998; }
.fa square twitter { color: #00acee; }
.fa square instagram { color: #f09433; }
social icons btn:hover { border color: transparent; } /* Navbar */ .only top navbar { background color: var( theme color primary) !important; } /* Card */ .gradient card { background: linear gradient(90deg, #C197D2 0%, #f8a8c5 100%); } /* Select Element */ .select box select { width: 60%; margin: 0 auto; } img 200 { height: 200px; }
We are done with the UI part Don’t
download images used in this project at our Github Repo Or feel free to use your images for styling
Now let’s
JPG
converter work.
Check our post! The logic behind PNG to JPG Image converter
For ease
or in an
if($convert type == 'png'){
$binary = imagecreatefromstring(file get contents($image)); $image quality = floor(10 ($image quality / 10)); ImagePNG($binary, $target dir$img name ' ' $convert type, $image quality); return $img name ' ' $convert type;
}
if($convert type == 'jpg'){
$binary = imagecreatefromstring(file get contents($image)); imageJpeg($binary, $target dir$img name ' ' $convert type, $image quality); return $img name.'.'.$convert type;
}
if($convert type == 'gif'){
$binary = imagecreatefromstring(file get contents($image)); imageGif($binary, $target dir$img name ' ' $convert type, $image quality); return $img name ' ' $convert type; } return false; } public function upload image($files, $target dir, $input name){ $target dir = "$target dir/"; $base name = basename($files[$input name]["name"]); $imageFileType = $this >get image type($base name); $new name = $this->get dynamic name($base name, $imageFileType); $target file = $target dir . $new name; $validate = $this >validate image($files[$input name]["tmp name"]); if(!$validate){
echo "Doesn't seem like an image file :("; return false; }
$file size = $this >check file size($files[$input name]["size"], 1000000); if(!$file size){ echo "You cannot upload more than 1MB file"; return false;
}
$file type = $this >check only allowed image types($imageFileType); if(!$file type){
echo "You cannot upload other than JPG, JPEG, GIF and PNG"; return false;
}
if (move uploaded file($files[$input name]["tmp name"], $target file)) { return array($new name, $imageFileType); } else { echo "Sorry, there was an error uploading your file "; } } protected function get image type($target file){ $imageFileType = pathinfo($target file,PATHINFO EXTENSION); return $imageFileType;
protected function validate image($file){ $check = getimagesize($file); if($check !== false) { return true; } return false; } protected function check file size($file, $size limit){ if ($file > $size limit) { return false; } return true; } protected function check only allowed image types($imagetype){ if($imagetype != "jpg" && $imagetype != "png" && $imagetype != "jpeg" && $imagetype != "gif" ) { return false; } return true; } protected function get dynamic name($basename, $imagetype){ $only name = basename($basename, ' ' $imagetype); $combine time = $only name.' '.time(); $new name = $combine time ' ' $imagetype; return $new name; } protected function remove extension from image($image){ $extension = $this >get image type($image); $only name = basename($image, ' ' $extension); return $only name;