Home » Php » How to combine image compression and sql upload with php

How to combine image compression and sql upload with php

Posted by: admin February 25, 2020 Leave a comment

Questions:

Looking to combine image compression on upload with the second file into one file.
I know there are duplicate lines that can be removed but I am trying to use PHP to compress the image before uploading to the database. Each file works fine by themselves but I need to combine and cannot figure it out. Any assistance would be appreciated.

<?php 

 /* 
 * Custom function to compress image size and 
 * upload to the server using PHP 
 */ 
 function compressImage($source, $destination, $quality) { 
// Get image info 
$imgInfo = getimagesize($source); 
$mime = $imgInfo['mime']; 

// Create a new image from file 
switch($mime){ 
    case 'image/jpeg': 
        $image = imagecreatefromjpeg($source); 
        break; 
    case 'image/png': 
        $image = imagecreatefrompng($source); 
        break; 
    case 'image/gif': 
        $image = imagecreatefromgif($source); 
        break; 
    default: 
        $image = imagecreatefromjpeg($source); 
} 

// Save image 
imagejpeg($image, $destination, $quality); 

// Return compressed image 
return $destination; 
} 


// File upload path 
$uploadPath = "../assets/uploads/images/"; 

// If file upload form is submitted 
$status = $statusMsg = ''; 
if(isset($_POST["submit"])){ 
$status = 'error'; 
if(!empty($_FILES["image"]["name"])) { 
    // File info 
    $fileName = basename($_FILES["image"]["name"]); 
    $imageUploadPath = $uploadPath . $fileName; 
    $fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION); 

    // Allow certain file formats 
    $allowTypes = array('jpg','png','jpeg','gif'); 
    if(in_array($fileType, $allowTypes)){ 
        // Image temp source 
        $imageTemp = $_FILES["image"]["tmp_name"]; 
        $imageSize = $_FILES["image"]["size"];

        // Compress size and upload image 
        $compressedImage = compressImage($imageTemp, $imageUploadPath, 75); 

        if($compressedImage){ 
            $compressedImageSize = filesize($compressedImage);

            $status = 'success'; 
            $statusMsg = "Image compressed successfully."; 
        }else{ 
            $statusMsg = "Image compress failed!"; 
        } 
    }else{ 
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
    } 
   }else{ 
    $statusMsg = 'Please select an image file to upload.'; 
  } 
} 

// Display status message 
echo $statusMsg; 

?>

To be incorporated in this “upload” function of this file

<?php
// 'product image' object
class ProductImage{

// database connection and table name
private $conn;
private $table_name = "product_images";

// object properties
public $id;
public $product_id;
public $name;
public $timestamp;

// constructor 
public function __construct($db){
    $this->conn = $db;
}

// read the first product image related to a product
function readFirst(){

    // select query
    $query = "SELECT id, product_id, name 
            FROM " . $this->table_name . " 
            WHERE product_id = ? 
            ORDER BY name DESC  
            LIMIT 0, 1";

    // prepare query statement
    $stmt = $this->conn->prepare( $query );

    // sanitize
    $this->id=htmlspecialchars(strip_tags($this->id));

    // bind prodcut id variable
    $stmt->bindParam(1, $this->product_id);

    // execute query
    $stmt->execute();

    // return values
    return $stmt;
 }

 // upload product image files
 function upload(){

    // specify valid image types / formats
    $valid_formats = array("jpg", "png", "JPG", "bmp",  "jpeg");

    // specify maximum file size of file to be uploaded
    $max_file_size = 1024*3000; // 3MB

    // directory where the files will be uploaded
    $path = "../assets/uploads/images/";

    // count or number of files
    $count = 0;

    // if files were posted
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

        // Loop $_FILES to execute all files
        foreach ($_FILES['files']['name'] as $f => $name){

            if ($_FILES['files']['error'][$f] == 4) {
                continue; // Skip file if any error found
            }

            if ($_FILES['files']['error'][$f] == 0) {           
                if ($_FILES['files']['size'][$f] > $max_file_size) {
                    $message[] = "$name is too large!.";
                    continue; // Skip large files
                }
                elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                    $message[] = "$name is not a valid format";
                    continue; // Skip invalid file formats
                }

                // No error found! Move uploaded files 
                else{ 
                    if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)){
                        $count++; // Number of successfully uploaded file

                        // save name to database
                        $this->name = $name;

                        if($this->create()){
                            // successfully added to databaes
                        }
                    }
                }
            }
        }
    }
    }

  // create product image
  function create(){

    // to get time-stamp for 'created' field
    $this->getTimestamp();

    // query to insert new product image record
    $query = "INSERT INTO  " . $this->table_name . " 
            SET product_id = ?, name = ?, created = ?";

    // prepare query statement
    $stmt = $this->conn->prepare($query);

    // sanitize
    $this->product_id=htmlspecialchars(strip_tags($this->product_id));
    $this->name=htmlspecialchars(strip_tags($this->name));

    // bind values
    $stmt->bindParam(1, $this->product_id);
    $stmt->bindParam(2, $this->name);
    $stmt->bindParam(3, $this->timestamp);

    // execute query
    if($stmt->execute()){
        return true;
    }else{
        return false;
    }
  }

  // read all product image related to a product
  function readAll(){

    // select query
    $query = "SELECT id, product_id, name 
            FROM " . $this->table_name . " 
            WHERE product_id = ? 
            ORDER BY name ASC";

    // prepare query statement
    $stmt = $this->conn->prepare( $query );

    // sanitize
    $this->product_id=htmlspecialchars(strip_tags($this->product_id));

    // bind prodcut id variable
    $stmt->bindParam(1, $this->product_id);

    // execute query
    $stmt->execute();

    // return values
    return $stmt;
   }

   // delete the product image
   function delete(){

    // delete product image query
    $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

    // prepare query statement
    $stmt = $this->conn->prepare($query);

    // sanitize
    $this->id=htmlspecialchars(strip_tags($this->id));

    // bind product image id variable
    $stmt->bindParam(1, $this->id);

    // execute query
    if($result = $stmt->execute()){
        return true;
    }else{
        return false;
    }
   }

   // used for the 'created' field when creating a product image
   function getTimestamp(){
    date_default_timezone_set('America/Chicago');
    $this->timestamp = date('Y-m-d H:i:s');
   }
}
?>
How to&Answers: