OK, I’ve got a general imaging class that can be used for resizing, copying and cropping. I’ve cut it down quite a bit, it should work as is and is commented. If you understand PHP you should understand it.
It is fairly straight forward. We handle security separately, on upload and on a per case basis so be aware that this has limited security and I’ve cut it down for simplicity sakes.
<?php
class image
{
// ---------------------------------------------------------------------------------------------------
// Properties
// ---------------------------------------------------------------------------------------------------
private $preferences = array(); // Holds preferences
private $scale;
// ---------------------------------------------------------------------------------------------------
// __construct()
// ---------------------------------------------------------------------------------------------------
// Constructor
//
// Pass prefs into class
// ---------------------------------------------------------------------------------------------------
function __construct($prefs)
{
// Set the preferences
if (count($prefs) > 0)
{
$this->initialize($prefs);
}
return 0;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// initialize()
// ---------------------------------------------------------------------------------------------------
// Initialize the image managemer
//
// @param prefs array Array containing preferences
// ---------------------------------------------------------------------------------------------------
function initialize($prefs = array())
{
// Grab the default preferences
$default = $this->fetchDefaultPrefs();
// Overwrite defaults with supplied prefs
foreach($default as $key => $val)
{
if(isset($prefs[$key]))
{
// Set to supplied preference
$this->preferences[$key] = $prefs[$key];
}
else
{
// Set to default preference
$this->preferences[$key] = $val;
}
}
return 0;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// setPrefs()
// ---------------------------------------------------------------------------------------------------
// Sets preferences to preferences array
//
// @param prefs array Array containing preferences
// ---------------------------------------------------------------------------------------------------
function setPrefs($prefs = array())
{
if (count($prefs) == 0)
{
echo('No preferences have been passed to the image library.');
}
// Set the preferences
foreach($prefs as $key => $val)
{
if(isset($this->preferences[$key]))
{
// Set to supplied preference
$this->preferences[$key] = $prefs[$key];
}
else
{
// Set to default preference
$this->preferences[$key] = $val;
}
}
return 0;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// resizeImage()
// ---------------------------------------------------------------------------------------------------
// Method to resize images
// ---------------------------------------------------------------------------------------------------
public function resizeImage($prefs)
{
// Set the preferences
$this->setPrefs($prefs);
// Check that the source image exists
$this->checkSourceImage();
// Get the information for the source image
$width = $this->getWidth($this->preferences['sourcepath']);
$height = $this->getHeight($this->preferences['sourcepath']);
$aspect_ratio = $this->getAspectRatio($width, $height);
$mimetype = $this->getMimeType($this->preferences['sourcepath']);
// Figure out the new width and height
$new = $this->calculateNewDimensions($aspect_ratio, $width, $height);
$newwidth = $new['width'];
$newheight = $new['height'];
// Set the output method (so you can save as file or output as an image for viewing
if(empty($this->preferences['output']))
{
$this->preferences['output'] = 'file';
}
// Handle resizing using the GD or GD2 Library
if($this->preferences['imagelib'] == 'gd' || $this->preferences['imagelib'] == 'gd2')
{
$this->gdResizeImage($width, $height, $newwidth, $newheight, $mimetype, $this->preferences['output']);
}
else
{
// Could support other libs
}
return 0;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// gdResizeImage()
// ---------------------------------------------------------------------------------------------------
// Method to resize or crop images using gd and gd2 libraries
// ---------------------------------------------------------------------------------------------------
public function gdResizeImage($width, $height, $newwidth, $newheight, $mimetype, $output='file')
{
if($output != 'file' && $output != 'screen')
{
$output = 'file';
}
// Figure out our create and copy functions
if($this->preferences['imagelib'] == 'gd2' && function_exists('imagecreatetruecolor'))
{
$create = 'imagecreatetruecolor';
$copy = 'imagecopyresampled';
}
else
{
$create = 'imagecreate';
$copy = 'imagecopyresized';
}
// Figure out read function
if($mimetype == "image/pjpeg" || $mimetype == "image/jpeg" || $mimetype == "image/jpg")
{
$suffix = "jpeg";
$quality = $this->preferences['quality'];
}
if($mimetype == "image/gif")
{
$suffix = "gif";
$quality = $this->preferences['quality'];
}
if($mimetype == "image/png" || $mimetype == "image/x-png")
{
$suffix = "png";
$quality = $this->preferences['quality'] / 10;
}
// Set the read/write GD functions
$read = 'imagecreatefrom' . $suffix;
$write = 'image' . $suffix;
// Set x and y (for cropping)
if($this->preferences['crop'] == false)
{
$x = 0;
$y = 0;
}
else
{
$ratio = $width / $height;
$x = $this->preferences['crop_x'];
$y = $this->preferences['crop_y'];
if(empty($x))
{
$x = 0;
}
if(empty($y))
{
$y = 0;
}
if($width > $this->preferences['maxwidth'])
{
$scale = $this->preferences['maxwidth'] / $width;
}
else
{
$scale = 1;
}
$width = ceil($width * $scale);
$height = ceil($height * $scale);
}
$src_handle = $read($this->preferences['sourcepath']);
if($src_handle == false)
{
echo('Source Image does not exist.');
}
// Handle screen output
if($output == 'screen')
{
header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename="'.basename($this->preferences['sourcepath']).'"');
$tmpname = "";
$temp = $create($newwidth, $newheight);
$copy($temp, $src_handle, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);
$write($temp, $tmpname, $quality);
}
// Handle file output (write new image)
if($output == 'file')
{
// Create the destination image
$destination_handle = $create($newwidth, $newheight);
// Preserve transparency for PNG images
if($mimetype == 'image/png' || $mimetype == 'image/x-png')
{
imagealphablending($destination_handle, false);
imagesavealpha($destination_handle, true);
}
// Copy the image
$copy($destination_handle, $src_handle, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);
// Write the new image
$write($destination_handle, $this->preferences['newpath'], $quality);
// Kill the handles
imagedestroy($destination_handle);
imagedestroy($src_handle);
}
return 0;
}
// ---------------------------------------------------------------------------------------------------
// checkSourceImage()
// ---------------------------------------------------------------------------------------------------
// Checks to ensure the source image exists
// ---------------------------------------------------------------------------------------------------
public function checkSourceImage()
{
if(!file_exists($this->preferences['sourcepath']))
{
echo('Source file does not exist.');
}
else
{
return true;
}
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// getWidth()
// ---------------------------------------------------------------------------------------------------
// Returns the width of the supplied image
// ---------------------------------------------------------------------------------------------------
public function getWidth($path)
{
$size = getimagesize($path);
$width = $size[0];
return $width;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// getHeight()
// ---------------------------------------------------------------------------------------------------
// Returns the height of the supplied image
// ---------------------------------------------------------------------------------------------------
public function getHeight($path)
{
$size = getimagesize($path);
$height = $size[1];
return $height;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// getAspectRatio()
// ---------------------------------------------------------------------------------------------------
// Returns the aspect ratio of the supplied image (based on width and height)
// ---------------------------------------------------------------------------------------------------
public function getAspectRatio($width, $height)
{
// Image is square
if($height == $width)
{
$aspect_ratio = "square";
}
// Image is landscape
if($width > $height)
{
$aspect_ratio = "landscape";
}
// Image is portrait
if($height > $width)
{
$aspect_ratio = "portrait";
}
return $aspect_ratio;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// getMimeType()
// ---------------------------------------------------------------------------------------------------
// Returns the mimetype of the supplied image
// ---------------------------------------------------------------------------------------------------
public function getMimeType($path)
{
$data = getimagesize($path);
$mimetype = $data['mime'];
return $mimetype;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// calculateNewDimensions()
// ---------------------------------------------------------------------------------------------------
// Returns the new dimensions of an image
// ---------------------------------------------------------------------------------------------------
public function calculateNewDimensions($aspect_ratio, $width, $height)
{
$maxwidth = $this->preferences['maxwidth'];
$maxheight = $this->preferences['maxheight'];
// Fixed Size Scaling fixes the size of the image (no aspect ratio scaling)
if($this->preferences['fixedsize'] == true)
{
if($this->preferences['fixedsize_mode'] == 'downscale')
{
if($width > $maxwidth)
{
$dimensions['width'] = $maxwidth;
}
if($height > $maxheight)
{
$dimensions['height'] = $maxheight;
}
}
else
{
$dimensions['width'] = $maxwidth;
$dimensions['height'] = $maxheight;
}
return $dimensions;
}
// Set for square images
if($aspect_ratio == "square")
{
if($width > $maxwidth)
{
$dimensions['width'] = $maxwidth;
$dimensions['height'] = $maxwidth;
}
else
{
$dimensions['width'] = $width;
$dimensions['height'] = $height;
}
return $dimensions;
}
// For landscape images
if($aspect_ratio == "landscape")
{
if($width > $maxwidth)
{
$dimensions['width'] = $maxwidth;
}
else
{
$dimensions['width'] = $width;
}
$ratio = $height / $width;
$dimensions['height'] = ceil($dimensions['width'] * $ratio);
return $dimensions;
}
// For portrait images
if($aspect_ratio == "portrait")
{
if($height > $maxheight)
{
$dimensions['height'] = $maxheight;
}
else
{
$dimensions['height'] = $height;
}
$ratio = $width / $height;
$dimensions['width'] = ceil($dimensions['height'] * $ratio);
return $dimensions;
}
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
// fetchDefaultPrefs()
// ---------------------------------------------------------------------------------------------------
// Fetches default imaging preferences
// ---------------------------------------------------------------------------------------------------
private function fetchDefaultPrefs()
{
$prefs = array(
'haltonerror' => true, // Whether to halt on errors
'imagelib' => 'gd2', // Library to use (gd, gd2)
'librarypath' => '', // Path to the image library
'sourcepath' => '' // Path to the source image
);
return $prefs;
}
// ---------------------------------------------------------------------------------------------------
}
?>
To use the class (simplest way possible):
require('class_imaging.php');
// Perform upload (putting image into a source folder (original image)
...
// Set preferences (prefs are optional)
$prefs = array(
'sourcepath' => PATH_TO_SOURCE_IMAGE
);
// Instantiate the imge manager
$imageManager = new image($prefs);
// Lets try to create a thumbnail
// -------------------------------------------------------------------------------------------------------
$prefs = array(
'sourcepath' => PATH_TO_SOURCE_IMAGE,
'newpath' => PATH_TO_THUMBNAIL_IMAGE,
'maxwidth' => '300',
'maxheight' => '300',
'fixedsize' => false,
'fixedsize_mode' => '',
'quality' => '90'
);
$imageManager->resizeImage($prefs);
Of course you could do this twice;
- Grab source image, scale image to fit fullsize requirements
- Grab source image, scale to thumbnail requirements
Then you can either keep or delete the source image.
You could even use the output option so that you could just have the source image and create a script that scales and displays the image and instead of doing:
<img src="theimage.jpg">
Do
<img src="image.php?id=theimage">
- note, this code is not perfect, just simple.
ETA:
You pass preferences to the class when you instantiate it, then call the relevant method.
Resizing is as above; cropping would be:
$prefs = array(
'sourcepath' => sourceimage,
'newpath' => outputimage,
'maxwidth' => '800',
'maxheight' => '800',
'fixedsize' => false,
'fixedsize_mode' => '',
'quality' => '90',
'crop' => 'true',
'crop_x' => '200',
'crop_y' => '0'
);