Tidbits @ Kassemi

A collection of opinions, thoughts, tricks and misc. information.

Saturday, August 27, 2005

 

PHP Image class

Hey everyone. I started to work with some image stuff today, and realized that I should allow members of this site that I'm working on to upload an image that doesn't meet the specs that my software needs. Now, that's an obvious thing that needs to be added to any site that lets it's users play with images, heck, any site that has a member base should allow the user to upload images (forums, community sites, comment sections, etc...). I'm not going to go into the uploading images aspect of PHP, at least today anyway, but I did just write something you might find useful... An image class... The main purpose is to allow a simple resize, but I'm sure you could write some classes that extend the functionality of this one.


/*
* Basic PHP Image class
* Copyright (C) 2005 James Kassemi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.

* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.

* You can view the LGPL here: http://www.gnu.org/licenses/lgpl.html
*
* Requirements:
*
* PHP with compiled gd library
*
* Description:
*
* Very simple class for dealing with images. Main purpose was for simple resize.
*
* Functions:
*
* set_image(string $path)
* start off by setting the path to the starter image.
*
* image_type()
* returns the type of the image in plain text.
*
* create_image_resource()
* creates a resource image based on the type of the image set using set_image()
*
* resize($width, $height, $unit='px', $dest_x=0, $dest_y=0, $src_x=0, $src_y=0)
* resizes the image to a new image resource, which it returns.
* width => desired width after resize.
* height => desired height after resize.
* unit => Can be set to either px or %, adjusts size accordingly.
* dest_x => the starting x pixel on the resized image...
* dest_y => the starting y pixel on the resized image...
* src_x => the x pixel in which to start grabbing the source image...
* src_y => the y pixel in which to start grabbing the source image...
*
* save($image, $filename, $quality=100, $type='')
* saves an image resource to a file
* image => the image resource to save
* filename => filename to save to.
* quality => for jpegs, adjust quality of file.
* type => specify the type (jpg, png, gif, wbmp)
* if none is specified, will use the type of the image set with set_image
*
* output($image, $type='', $quality='100', $header=true
* outputs an image resources content to the browser
* image => the image resource to output
* type => the image type to output
* quality => for jpegs, adjust quality of output.
* header => true or false... Output the mime type...
*
* Sample usage:
*
* Takes the PHP logo from the php.net site, and resizes it to 200px by 200px
*
*
include ('./class/image.php')
$image = new image();
$image->set_image('http://us2.php.net/images/php.gif');
$new = $image->resize(200, 200);
$image->output($new, 'jpg');
?>

*/

class image {

public $path;

function set_image($path){
$this->path = $path;
}

function image_type(){
$type = getimagesize($this->path);
$type = $type[2];
switch($type){
case 1:
return "gif";
case 2:
return "jpg";
case 3:
return "png";
case 4:
return "swf";
case 5:
return "psd";
case 6:
return "bmp";
case 7:
return "tiff(intel)";
case 8:
return "tiff(motorola)";
case 9:
return "jpc";
case 10:
return "jp2";
case 11:
return "jpx";
case 12:
return "jb2";
case 13:
return "swc";
case 14 :
return "iff";
case 15:
return "wbmp";
case 16:
return "xbm";
default:
return false;
}
}

function create_image_resource(){
/* This will convert the set image to the proper resource, so we can work with it. */
$type = $this->image_type();
switch($type){
case 'gif':
return imagecreatefromgif($this->path);
case 'jpg':
return imagecreatefromjpeg($this->path);
case 'png':
return imagecreatefrompng($this->path);
case 'wbmp':
return imagecreatefromwbmp($this->path);
default:
return false;
}
}

function resize($width, $height, $unit='px', $dest_x=0, $dest_y=0, $src_x=0, $src_y=0){
$image_old = $this->create_image_resource();
$size = getimagesize($this->path);
$old_width = $size[0];
$old_height = $size[1];
switch($unit){
case 'px':
$new_width = $width;
$new_height = $height;
break;
case '%':
$width = $width / 100;
$height = $height / 100;
$new_width = $old_width * $height;
$new_height = $old_height * $height;
break;
default:
return false;
}
$image_new = imagecreatetruecolor($new_width, $new_height);
$success = imagecopyresampled($image_new, $image_old, $dest_x, $dest_y, $src_x, $src_y, $new_width, $new_height,
$old_width, $old_height);
return $image_new;
}

function save($image, $filename, $quality='100', $type=''){
if($type=''){ $type=$this->image_type(); }
switch($type){
case 'gif':
imagegif($image, $filename);
return true;
case 'jpg':
imagejpeg($image, $filename);
return true;
case 'png':
imagepng($image, $filename);
return true;
case 'wbmp':
imagewbmp($image, $filename);
return true;
default:
return false;
}
}

function output($image, $type='', $quality='100', $header=true){
if($type==''){ $type=$this->image_type(); }
switch($type){
case 'gif':
if($header==true)
header('Content-type: image/gif');
imagegif($image);
break;
case 'jpg':
if($header==true)
header('Content-type: image/jpeg');
imagejpeg($image);
break;
case 'png':
if($header==true)
header('Content-type: image/png');
imagepng($image);
break;
case 'wbmp':
if($header==true)
header('Content-type: image/wbmp');
imagewbmp($image);
break;
default:
return false;
}
}

}



?>


Comments: Post a Comment



<< Home

Archives

August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006  

This page is powered by Blogger. Isn't yours?