Coder Social home page Coder Social logo

rajwebhost / image-optimizer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from artisansweb/image-optimizer

0.0 0.0 0.0 5.36 MB

Image optimization using PHP

Home Page: https://artisansweb.net/image-optimization-using-artisansweb-image-optimizer-package

License: MIT License

PHP 100.00%

image-optimizer's Introduction

Image optimization using PHP

This library helps you to compress JPGs, PNGs, GIFs images on the fly. Apart from this package, you don't need to install any additional software or package to perform optimization task.

Installation

You can install the package via composer:

composer require artisansweb/image-optimizer

Under the hood, this package uses resmush.it service to compress the images. Alternatively, package using native PHP functions - imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagejpeg.

Usage

This package is straight-forward to use. All you need to do is pass source path of your image.

use ArtisansWeb\Optimizer;

$img = new Optimizer();

$source = 'SOURCE_PATH_OF_IMAGE';
$img->optimize($source);

Above code will optimize the image and replace the original image with the optimized version.

Optionally, you can also pass destination path where optimized version will stored.

$source = 'SOURCE_PATH_OF_IMAGE';
$destination = 'DESTINATION_PATH_OF_IMAGE';
$img->optimize($source, $destination);

Recommeded way of using this code is on image upload. The user should optimize image on upload which will result in better performance.

Let's say you want to store optimized version in the 'images' folder. You can use the below code for this purpose.

<?php
require_once "vendor/autoload.php";

use ArtisansWeb\Optimizer;

if (isset($_POST['submit'])) {
    $img = new Optimizer();
    move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$_FILES['file']['name']);
    $img->optimize('images/'.$_FILES['file']['name']);
}
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" name="submit" value="Submit" />
</form>

License

The MIT License (MIT). Please see License File for more information.

image-optimizer's People

Contributors

artisansweb avatar jfparis84 avatar sajidsynclarity avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.