WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Image Class
Categories : PHP, PHP Classes, Multimedia, GD image library Click here to Update Your Picture
Alix Axel
Date : Jun 19th 2008
Grade : 4 of 5 (graded 3 times)
Viewed : 2061
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Alix Axel
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

This class uses the GD library to do standard image operations like resizing, convert image type, fetch image dimensions and so on. Works with PNG, JPG and GIF image types.

<?php

class Image
{
    private
$gd_info = array();
    private
$im = false;
    private
$info = array();

    function
Convert($type = 'jpg', $quality = null, $destination_folder = '.')
    {
        if (
is_resource($this->im))
        {
            return
$this->Transform($this->info['width'], $this->info['height'], $type, $quality, $destination_folder);
        }

        return
false;
    }

    function
GD_Info($flag = null)
    {
        if (
extension_loaded('gd'))
        {
           
$result = array();

            if (empty(
$this->gd_info))
            {
               
$this->gd_info = gd_info();

               
$this->gd_info['GD Bundled'] = true;

                if (
strpos(strtolower($this->gd_info['GD Version']), 'bundled') === false)
                {
                   
$this->gd_info['GD Bundled'] = false;
                }

               
preg_match('([\d\.]+)', $this->gd_info['GD Version'], $match);

               
$this->gd_info['GD Version'] = $match[0];
            }

            if (!empty(
$flag))
            {
                if (
array_key_exists($flag, $this->gd_info))
                {
                   
$result = $this->gd_info[$flag];
                }

                else
                {
                   
$result = false;
                }
            }

            return
$result;
        }

        return
false;
    }

    function
Info($flag = null, $image = null)
    {
        if (!empty(
$image))
        {
            if (
is_file($image))
            {
               
$image_info = getimagesize($image);

                if (
$image_info !== false)
                {
                   
$result = array();

                    list(
$result['width'], $result['height'], $result['type']) = $image_info;

                   
$image_types = array('gif', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tif', 'tif', 'jpc', 'jp2', 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 'xbm');

                    if ((
$result['type'] >= 1) && ($result['type'] <= 16))
                    {
                       
$result['type'] = $image_types[$result['type'] - 1];
                    }

                    if (!empty(
$flag))
                    {
                        if (
array_key_exists($flag, $result))
                        {
                           
$result = $result[$flag];
                        }

                        else
                        {
                           
$result = false;
                        }
                    }

                    return
$result;
                }
            }

            return
false;
        }

        if (
is_resource($this->im))
        {
           
$result = $this->info;

            if (!empty(
$flag))
            {
                if (
array_key_exists($flag, $this->info))
                {
                   
$result = $this->info[$flag];
                }

                else
                {
                   
$result = false;
                }
            }

            return
$result;
        }

        return
false;
    }

    function
Open($image)
    {
        if (!
extension_loaded('gd'))
        {
            return
false;
        }

        if (
is_resource($this->im))
        {
           
imagedestroy($this->im);
        }

       
$this->im = false;
       
$this->info = array();

        if (
is_file($image))
        {
           
$image_info = getimagesize($image);

            if (
$image_info !== false)
            {
                list(
$this->info['width'], $this->info['height'], $this->info['type']) = $image_info;

               
$image_types = array('gif', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tif', 'tif', 'jpc', 'jp2', 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 'xbm');

                if ((
$this->info['type'] >= 1) && ($this->info['type'] <= 16))
                {
                   
$this->info['type'] = $image_types[$this->info['type'] - 1];
                }

               
$this->info['hash'] = sha1_file($image);

                if (
$this->info['type'] == 'gif')
                {
                   
$this->im = imagecreatefromgif($image);
                }

                else if (
$this->info['type'] == 'jpg')
                {
                   
$this->im = imagecreatefromjpeg($image);
                }

                else if (
$this->info['type'] == 'png')
                {
                   
$this->im = imagecreatefrompng($image);

                   
/*$transparent = imagecolorallocate($this->im, 255, 255, 255);
                    imagefill($this->im, 0, 0, $transparent);
                    imagecolortransparent($this->im, $transparent);*/

                    ###################################

                   
echo imagecolortransparent($this->im);
                }

                if (!
is_resource($this->im))
                {
                   
$this->im = false;
                   
$this->info = array();
                }
            }
        }

        return
$this;
    }

    function
Path($path)
    {
        if (
file_exists($path))
        {
           
$path = str_replace('\\', '/', realpath($path));

            if (
is_dir($path))
            {
               
$path .= '/';
            }

            return
$path;
        }

        return
false;
    }

    function
Resize($width = null, $height = null, $quality = null, $destination_folder = '.')
    {
        if (
is_resource($this->im))
        {
            return
$this->Transform($width, $height, $this->info['type'], $quality, $destination_folder);
        }

        return
false;
    }

    function
Transform($width = null, $height = null, $type = null, $quality = null, $destination_folder = '.')
    {
        if (
is_resource($this->im))
        {
            if (!
in_array($this->info['type'], array('gif', 'jpg', 'png')))
            {
                return
false;
            }

           
$width = intval($width);
           
$height = intval($height);

            if ((empty(
$width)) && (empty($height)))
            {
                return
false;
            }

            if (empty(
$width))
            {
               
$width = round($height * $this->info['width'] / $this->info['height']);
            }

            if (empty(
$height))
            {
               
$height = round($width * $this->info['height'] / $this->info['width']);
            }

           
$type = strtolower($type);

            if ((empty(
$type)) || (!in_array($type, array('gif', 'jpg', 'png'))))
            {
               
$type = $this->info['type'];
            }

            if (
is_null($quality))
            {
               
$quality = 100;
            }

           
$quality = intval($quality);

            if (
$type == 'jpg')
            {
                if ((
$quality < 0) || ($quality > 100))
                {
                   
$quality = 100;
                }
            }

            else
            {
               
$quality = 100;
            }

           
$destination_folder = $this->Path($destination_folder);

            if (!
is_dir($destination_folder))
            {
                return
false;
            }

           
$destination_image = sha1($this->info['hash'] . $width . $height . $quality) . '.' . $type;

            if (
file_exists($destination_folder . $destination_image))
            {
                return
$destination_image;
            }

            if (
version_compare($this->GD_Info('GD Version'), '2.0.28', '>='))
            {
               
$image = imagecreatetruecolor($width, $height);

                if (
$this->GD_Info('GD Bundled') === true)
                {
                   
imageantialias($image, true);
                }

               
imagecopyresampled($image, $this->im, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);
            }

            else
            {
               
$image = imagecreate($width, $height);

                if (
$this->GD_Info('GD Bundled') === true)
                {
                   
imageantialias($image, true);
                }

               
imagecopyresized($image, $this->im, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);
            }

            if (
$type == 'gif')
            {
                if (
imagegif($image, $destination_folder . $destination_image) !== false)
                {
                   
imagedestroy($image);

                    return
$destination_image;
                }
            }

            else if (
$type == 'jpg')
            {
               
imageinterlace($image, true);

                if (
imagejpeg($image, $destination_folder . $destination_image, $quality) !== false)
                {
                   
imagedestroy($image);

                    return
$destination_image;
                }
            }

            else if (
$type == 'png')
            {
                if (
imagepng($image, $destination_folder . $destination_image) !== false)
                {
                   
imagedestroy($image);

                    return
$destination_image;
                }
            }

            return
false;
        }

        return
false;
    }
}

?>



A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
 Joshua Walcher wrote : 1784
If you're going to add a class again, would you add usage
 examples as comments in your code?  Unless, of course, 
you're just using this site as a depository for your own 
code and don't care about other users using the code.  If 
that's the case, no worries!
 
 Alix Axel wrote : 1785
It seems pretty straightforward to me, however for the sake of a few examples here they are:

<?php
$Image 
= new Image();

// Convert an image:
$Image->Open('/path/to/image.jpg')->Convert('png');

// Resize an image (proportional height):
$Image->Open('/path/to/image.jpg')->Resize(400);

// Resize an image (proportional width):
$Image->Open('/path/to/image.jpg')->Resize(null400);

// Resize an image (forced width and height):
$Image->Open('/path/to/image.jpg')->Resize(400400);
?>
 
 Joshua Walcher wrote : 1787
Thanks!
 
 m d wrote : 1789
Those examples work out pretty cool, except this: "f97faa4505727aebb7323ad2dc5b5a72879aae89"

Image name:"image.jpg"
$Image->Open('image.jpg')->Convert('png');
afterwards: "f97faa4505727aebb7323ad2dc5b5a72879aae89.png"

How to retain the filename or rather just append it with a "_" like this "image_.png"

Cheers
 
 Alix Axel wrote :1791
Hi, to do that you must change the following code:

<?php

// On the Open() method change this:
$this->info['hash'] = sha1_file($image);
// to:
$this->info['hash'] = pathinfo($imagePATHINFO_FILENAME) . '_';

// On the Transform() method change this:
$destination_image sha1($this->info['hash'] . $width $height $quality) . '.' $type;
// to:
$destination_image $this->info['hash'] . '.' $type;

?>

I haven't tested but it should work.