PHP Classes

File: SimpleImage.php

Recommend this page to a friend!
  Classes of Juraj Puchký   Simple Image   SimpleImage.php   Download  
File: SimpleImage.php
Role: Class source
Content type: text/plain
Description: SimpleImage class
Class: Simple Image
Resize images preserving the original proportions
Author: By
Last change: Removed restricted access
Date: 10 years ago
Size: 2,561 bytes
 

Contents

Class file image Download
<?php
/**
 * SimpleImage
 * @description Class to manipulate with images
 * @version 1.0.0
 * @copyright (c) 2013 Juraj Puchký - Devtech
 * @license GPLv3
 * @autor Juraj Puchký - Devtech <sjurajpuchky@seznam.cz>
 *
 */

class SimpleImage {
    public static function
load($filename) {
       
$image_info = getimagesize ( $filename );
       
$image_type = $image_info [2];
       
$image = null;
       
        try {
            switch (
$image_type) {
                case
IMAGETYPE_JPEG :
                   
$image = imagecreatefromjpeg ( $filename );
                    break;
                case
IMAGETYPE_GIF :
                   
$image = imagecreatefromgif ( $filename );
                    break;
                case
IMAGETYPE_PNG :
                   
$image = imagecreatefrompng ( $filename );
                    break;
                default :
                    return
false;
            }
        } catch (
Exception $e ) {
           
error_log ( $e->getMessage () );
            return
false;
        }
        return
$image;
    }
    public static function
save($image, $filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null) {
        try {
            switch (
$image_type) {
                case
IMAGETYPE_JPEG :
                   
imagejpeg ( $image, $filename, $compression );
                    break;
                case
IMAGETYPE_GIF :
                   
imagegif ( $image, $filename );
                    break;
                case
IMAGETYPE_PNG :
                   
imagepng ( $image, $filename );
                    break;
                default :
                    return
false;
            }
            if (
$permissions !== null) {
               
chmod ( $filename, $permissions );
            }
        } catch (
Exception $e ) {
           
error_log ( $e->getMessage () );
            return
false;
        }
       
        return
true;
    }
    public static function
getWidth($image) {
        return
imagesx ( $image );
    }
    public static function
getHeight($image) {
        return
imagesy ( $image );
    }
    public static function
resizeToHeight($image, $height) {
       
$ratio = $height / self::getHeight ( $image );
       
$width = self::getWidth ( $image ) * $ratio;
        return
self::resize ( $image, $width, $height );
    }
    public static function
resizeToWidth($image, $width) {
       
$ratio = $width / self::getWidth ( $image );
       
$height = self::getheight ( $image ) * $ratio;
        return
self::resize ( $image, $width, $height );
    }
    public static function
scale($image, $scale) {
       
$width = self::getWidth ( $image ) * $scale / 100;
       
$height = self::getheight ( $image ) * $scale / 100;
        return
self::resize ( $image, $width, $height );
    }
    public static function
resize($image, $width, $height) {
       
$new_image = imagecreatetruecolor ( $width, $height );
       
imagecopyresampled ( $new_image, $image, 0, 0, 0, 0, $width, $height, self::getWidth ( $image ), self::getHeight ( $image ) );
        return
$new_image;
    }
}