class SimpleImage {
var $image;
var $image_type;
var $location;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
//// own stuff added 14/06/2010
function getType($filetype) {
if( $filetype == 'image/jpeg' ) {
$ext = 'jpg';
} elseif( $filetype == 'image/pjpeg' ) {
$ext = 'jpg';
} elseif( $filetype == 'image/gif' ) {
$ext = 'gif';
} elseif( $filetype == 'image/png' ) {
$ext = 'png';
}
return $ext;
}
function randomise(){
// Get a random set of 3 chars which we will append to the filename to prevent duplicate file names.
$keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
$length = 3;
$randkey = "";
for ($i=0;$i<$length;$i++) $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1);
// Set the name of the file (current time + the random value + . + the file extension)
$filename = time().$randkey;
return $filename;
}
function uploadimage($location, $filename, $filetype, $i) {
/*
print "location: ".$location;
print "name: ".$filename;
print "type: ".$filetype;
print "num: ".$i;
die('11');
*/
$ext = $this->getType($filetype);
$newfilename = $this->randomise();
if($ext <> ""){
$file = $newfilename.".".$ext;
$uploadfile = $location.$file;
// Move the file to the server. If move is successful store the file info to the database
if ((move_uploaded_file($_FILES["image"]["tmp_name"][$i], $uploadfile))or die("Couldn't copy the file!".$_FILES["image"]["tmp_name"][$i])){
return $file;
}
}
}
}
?>
<?php
// user like this
/*
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->resizeToWidth(250);
$image->scale(50);
$image->resizeToHeight(500);
$image->save('picture2.jpg');
*/
?>