Ancient Image Thumb
<?php
/**
* Thumbnail Generator 0.4 (Requires GD 2.0)
*/
class Image_Thumbnail extends Image_Abstract
{
/** Source Dimensions */
private $_srcImageX;
private $_srcImageY;
/** Destination Image */
private $_dstImage;
/** Destination Dimensions */
private $_dstImageX;
private $_dstImageY;
/** Other Items */
private $_originalFileLocation;
private $_prefix;
private $_suffix;
private $_quality = 80;
private $_pathInfo;
/**
* The file to create a thumbnail out of
* @param string $filename The name (And Path if necessary) of the file
*/
public function setFile($filename)
{
$this->_originalFileLocation = $filename;
$this->_pathInfo = pathinfo($this->_originalFileLocation);
$this->_originalFileExt = $this->_pathInfo['extension'];
/** _ext might be overriden, it might not, but its used to compare in submit */
$this->_ext = '.' . $this->_pathInfo['extension'];
$this->_fileName = $this->_pathInfo['filename'];
return $this;
}
/**
* (Optional) Set the image quality for a JPEG, default is 80
* @param integer $quality A value of 0 through 100
*/
public function setQuality($quality)
{
$this->_quality = (int) $quality;
return $this;
}
/**
* (Optional) Set a prefix for the image, default is 'th_'
* @param string $prefix The prefix before the filename.
*/
public function setPrefix($prefix)
{
$this->_prefix = $prefix;
$this->_fileName = $this->_prefix . $this->_fileName;
return $this;
}
/**
* (Optional) Set a suffix for the image, default is '_th'
* @param string $suffix The suffix after the filename.
*/
public function setSuffix($suffix)
{
$this->_suffix = $suffix;
$this->_fileName = $this->_fileName . $this->_suffix;
return $this;
}
/**
* (Optional) Makes the output become a format of choice.
* @param string $ext The file extension: gif, jpg, png
*/
public function setFormat($ext)
{
/** Strip the period if there is one */
$this->_ext = trim(strtolower($ext));
$types = array('.jpg', 'jpeg', '.gif', '.png');
if (!in_array($this->_ext, $types))
throw new Exception('Your file format must be: ' . implode(', ', $types));
return $this;
}
/**
* This moves the file to a destination - It must be set BEFORE submitting!
* @param string $Dir The directory to move the new thumbnail to
*/
public function setDestination($dir)
{
// Handle these slashes the user might put in..
$dir = ltrim($dir, '/');
if (strpos($dir, -1) != '/') {
$dir .= '/';
}
$this->_destination = $dir;
return $this;
}
/**
* Set the thumbnail size,
* Depending on the proportion of the image factor this in:
* if the image is wider than the height, the X will match.
* if the image is taller than the width, the Y will match.
*
* @param <int> $x Desired width
* @param <int> $y Desired height
*/
public function setSize($x, $y)
{
$this->_getDimensions();
$this->_canvasSizeX = (int) $x;
$this->_canvasSizeY = (int) $y;
$this->_dstImageX = (int) $x;
$this->_dstImageY = (int) $y;
/**
* When a upload is way too tall or way too high, it doesn't resize properly,
* so we usually want to enable clipping, it's off by default
*/
/** Thanks to my brother Joe for helping me with math, I'm really bad! */
/** If it's landscape */
if ($this->_srcImageX > $this->_srcImageY) {
$this->_dstImageX = ($this->_srcImageX * $this->_dstImageY) / $this->_srcImageY;
}
/** If it's portrait */
if ($this->_srcImageX < $this->_srcImageY) {
$this->_dstImageY = ($this->_srcImageY * $this->_dstImageX) / $this->_srcImageX;
}
return $this;
}
/**
* Runs the operation and outputs the image
*/
public function submit()
{
$this->_checkExists($this->_originalFileLocation);
$this->_checkSet($this->_dstImageX);
$this->_checkSet($this->_dstImageY);
$this->_submitCalled = true;
/** Create a new blank image */
$this->_dstImage = imageCreateTrueColor($this->_canvasSizeX, $this->_canvasSizeY);
/** Copy the resampled image onto the blank */
imageCopyResampled($this->_dstImage, $this->_srcImage, 0, 0, 0, 0, $this->_dstImageX, $this->_dstImageY, $this->_srcImageX, $this->_srcImageY);
//imageCopyResampled($this->_dstImage, $this->_srcImage, 0, 0, 0, 0, 100, 100, $this->_srcImageX, $this->_srcImageY);
/** Save the file, also allow the result to be grabbed from a var */
switch ($this->_ext)
{
case '.jpg':
case '.jpeg':
imageJPEG($this->_dstImage, $this->_fileName . $this->_ext, $this->_quality);
break;
case '.png':
imagePNG($this->_dstImage, $this->_fileName . $this->_ext, 0, NULL);
break;
case '.gif':
imageGIF($this->_dstImage, $this->_fileName . $this->_ext, $this->_quality);
break;
}
/** If we need to move the location */
if (isset($this->_destination))
{
$wasMoved = rename($this->_fileName . $this->_ext, $this->_destination . $this->_fileName . $this->_ext);
/** If moving it fails, delete the thumnail */
if ($wasMoved == false)
{
throw new Exception("The thumbnail was created, but it couldnt be moved because
your folder: {$this->_destination} is not writable, so the thumbnail was erased
so you can give your directory CHMOD 0777 permission.");
$this->delete();
}
}
/** Finishing by removing from memory */
@imageDestroy($this->_srcImage);
@imageDestroy($this->_dstImage);
}
/**
* Grabs the image dimensions
*/
private function _getDimensions()
{
/** Grab the Source Dimensions */
switch ($this->_originalFileExt)
{
case 'jpg':
case 'jpeg':
$this->_srcImage = imageCreateFromJPEG($this->_originalFileLocation);
break;
case 'png':
$this->_srcImage = imageCreateFromPNG($this->_originalFileLocation);
imageAlphaBlending($this->_srcImage, false); // setting alpha blending on
imageSaveAlpha($this->_srcImage, true); // save alphablending setting (important)
break;
case 'gif':
$this->_srcImage = imageCreateFromGIF($this->_originalFileLocation);
break;
}
$this->_srcImageX = imagesX($this->_srcImage);
$this->_srcImageY = imagesY($this->_srcImage);
}
/**
* Returns data about the uploaded file
* @return array
*/
public function get()
{
if ($this->_submitCalled == true)
{
return array(
'dir' => $this->_destination,
'name' => $this->_fileName,
'ext' => $this->_ext
);
}
else
{
throw new Exception('You must call get() after you call submit()');
}
}
/**
* (Optional) Deletes the file, use in a Catch statement
*/
public function delete()
{
if ($this->_submitCalled == true)
{
if (file_exists($this->_destination . $this->_fileName . $this->_ext))
@unlink($this->_destination . $this->_fileName . $this->_ext);
}
}
}