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
Submit Site
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 : Class for processing image (resizing the image size to create thumb nails and small images from actual image without loosing the aspect ratio of the image)
Categories : PHP, PHP Classes, Graphics Click here to Update Your Picture
saji nair
Date : Mar 01st 2007
Grade : 5 of 5 (graded 2 times)
Viewed : 2177
File : 4593.php
Images : No Images for this code example.
Search : More code by saji nair
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This Class resizes the image without using any external library like Image magic. The aspect ratio of the image is maintained in the resized image. This maintains the quality of the image.

<?php
/**
**Image Processing class for resizing the image once the parameters are recorded. The image is resized but not saved yet.
**The image saving is done at the end where the object is created and called from
**Creating true color template for the new image to maintain the quality of the image
**/       
   
class imageProcessing{
        var
$imageSizeX;
        var
$imageSizeY;
        var
$resizeX;
        var
$resizeY;
        var
$reduction;
        var
$fileName;
        var
$msg;
        var
$image;
        var
$imageType;
       
/**
** Class constructor to initialize the processing
**/       
       
function imageProcessing($imgName,$red=300){
           
$this->reduction=$red;
            if(
is_file($imgName)){
                if(
file_exists($imgName)){
                   
$this->fileName=$imgName;
                   
$this->getSize();
                   
$this->setSize();
                    print
$this->imageSizeX."==>".$this->resizeX."<br>";
                    print
$this->imageSizeY."==>".$this->resizeY."<br>";
                   
$this->resizeIt();
                   
$this->saveImage($imgName);
                }else{
                   
$this->errorState(0);
                }
            }else{
               
$this->errorState(2);
            }
        }

/**
** Function to set target size for the final image
** This size is based on the aspect ratio of the actual image to maintain the image clarity
**/       
       
function setSize(){
            if(
$this->imageSizeY<=$this->imageSizeX){
               
$ratio=$this->imageSizeY/$this->imageSizeX;
               
$this->resizeX=$this->reduction;
               
$this->resizeY=round($ratio*$this->reduction);
            }else{
               
$ratio=$this->imageSizeX/$this->imageSizeY;
               
$this->resizeY=$this->reduction;
               
$this->resizeX=round($ratio*$this->reduction);
            }
        }
       
/**
** Function to identify the image and set the image quality for JPEG images
**
**/       
       
function saveImage($imgName){
            switch (
$this->imageType){
                case
"gif":
                   
imagegif($this->image,$imgName);
                    break;
                case
"jpg":
                   
imagejpeg($this->image,$imgName,100);
                    break;
                case
"png":
                   
imagepng($this->image,$imgName);
                    break;
            }
        }

       
/**
**Function for fetching the parameters of the image. Identifying image type.
**/           
       
function getSize(){
           
$imgParams=getimagesize($this->fileName);
           
$this->imageSizeX=$imgParams[0];
           
$this->imageSizeY=$imgParams[1];
           
            switch(
$imgParams[2]){
                case
1:
                   
$this->imageType="gif";
                   
$this->image = imagecreatefromgif($this->fileName);
                    break;

                case
2:
                   
$this->imageType="jpg";
                   
$this->image = imagecreatefromjpeg($this->fileName);
                    break;
               
                case
3:
                   
$this->imageType="png";
                   
$this->image = imagecreatefrompng($this->fileName);
                    break;
               
                default:
                   
$this->errorState(1);
            }
               
        }

/**
**Function for resizing the image once the parameters are recorded. The image is resized but not saved yet.
**Creating true color template for the new image to maintain the quality of the image
**/       
       
function resizeIt(){

            if(
$this->imageSizeX<=$this->resizeX){
               
$this->resizeX=$this->imageSizeX;
            }

            if(
$this->imageSizeY<=$this->resizeY){
               
$this->resizeY=$this->imageSizeY;
            }
           
$copy=imagecreatetruecolor($this->resizeX,$this->resizeY);
            if(
$copy){
                if(
imagecopyresampled($copy,$this->image,0,0,0,0,$this->resizeX,$this->resizeY,$this->imageSizeX,$this->imageSizeY)){
                    if(
imagedestroy($this->image)){
                       
$this->image=$copy;
                    }else{
                       
$this->errorState(6);
                    }
                }else{
                   
$this->errorState(4);
                }
            }else{
               
$this->errorState(5);
            }
        }
       
/**
**Function for error and displaying error messages.
**/           
       
function errorState($err){
           
            switch (
$err){
                case
0:
                   
$this->msg="File Not Found.";
                    break;
                case
1:
                   
$this->msg="Invalid File Type.";   
                    break;
                case
2:
                   
$this->msg="Invalid Input.";       
                    break;
                case
3:
                   
$this->msg="Could Not Save.";       
                    break;
                case
4:
                   
$this->msg="Could not Create Image.";       
                    break;
                case
5:
                   
$this->msg="Could not Create Blank File.";       
                    break;
                case
6:
                   
$this->msg="Unable to Destroy Old File.";       
                    break;
            }
           
            print
$this->msg;
            exit();
        }
    }


/***
** Object creation and processing the image
***/   
   
$img=new imageProcessing("Copy of DSC00083.jpg"); // replace with the image name or the variable containing the file name
?>



Render TTF Text to PNG. Text message, font, size, rotation, padding, color, background, and transparency can all be defined via URL.
Categories : PHP, PHP Classes, Graphics
HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout.
Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs
Three Cool Classes and One Trick
Categories : PHP, PHP Classes, Graphics, Email
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
Image Cache
Categories : Graphics, PHP Classes, PHP
HTML_Graphs provides a simple PHP interface for creating pure HTML charts.
Categories : Graphics, PHP, PHP Classes, Charts and Graphs
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
3dLib - a class for drawing in 3D space. Supported functions: Line, SetPixel, Polygon, FilledPolygon, etc. 3dChart() function has been added for one-call drawing of 3d charts. Support of mostly used 3d-transformations.
Categories : Graphics, Math., PHP Classes, PHP, Charts and Graphs
PHP interface class to the eBusiness Charts generatation remote service.
Categories : PHP, PHP Classes, Graphics, Charts and Graphs
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
A class to draw real 3D graphics with surface area
Categories : Graphics, PHP, PHP Classes
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