Skip to content

Instantly share code, notes, and snippets.

@cornedor
Created June 27, 2013 11:02
Show Gist options
  • Save cornedor/5875642 to your computer and use it in GitHub Desktop.
Save cornedor/5875642 to your computer and use it in GitHub Desktop.
Crops images to a smaller or bigger size.
<?php
/**
* This classes allows you to create thumbnails. Input images will be resized,
* while the ratio keeps intact, and will cropped to the size given.
* @author Corné Dorrestijn
* @license http://www.gnu.org/copyleft/lesser.html GNU Library General Public License
* @package Default
*/
class Thumbnail
{
private $imageData, $newImage, $sourceWidth, $sourceHeight;
public $width, $height;
public $quality = 1;
/**
* Constructor
* @param string $url The url to the local file.
* @param integer $width The width of the thumbnail, default 100px.
* @param integer $height The height of the thumbnail, default 100px.
* @return void|String returns a string if there is a error.
*/
public function __construct($url, $width = 100, $height = 100)
{
if(!isset($url) && $url === undefined && $url === '') return "No image url given.";
if(!is_readable($url)) return 'Image "' . $url . '" file not readable.';
$this->width = $width;
$this->height = $height;
$this->imageData = imagecreatefromstring(file_get_contents($url));
list($this->sourceWidth, $this->sourceHeight) = getimagesize($url);
$this->redraw();
return;
}
/**
* Generates a jpeg image.
* @param boolean $redraw If set to true it will redraw the image.
* @param string $path If set it will save the image to the given path.
* @return void|String returns a string if there is a error.
*/
public function getJpeg($redraw = true, $path = '')
{
if($redraw) $this->redraw();
$q = round($this->quality * 99);
if($path !== '')
{
if(!imagejpeg($this->newImage, $path, $q))
{
return "Image NOT saved";
}
}
else
{
header ('Content-Type: image/jpeg');
imagejpeg($this->newImage, NULL, $q);
}
imagedestroy($this->newImage);
return;
}
/**
* Generates a png image.
* @param boolean $redraw If set to true it will redraw the image.
* @param string $path If set it will save the image to the given path.
* @return void|String returns a string if there is a error.
*/
public function getPng($redraw = true, $path = '')
{
if($redraw) $this->redraw();
$q = 9-round($this->quality * 9);
if($path !== '')
{
if(!imagepng($this->newImage, $path, $q))
{
return "Image NOT saved";
}
}
else
{
header ('Content-Type: image/png');
imagepng($this->newImage, NULL, $q);
}
imagedestroy($this->newImage);
return;
}
/**
* Generates a gif image.
* @param boolean $redraw If set to true it will redraw the image.
* @param string $path If set it will save the image to the given path.
* @return void|String returns a string if there is a error.
*/
public function getGif($redraw = true, $path = '')
{
if($redraw) $this->redraw();
if($path !== '')
{
if(!imagegif($this->newImage, $path))
{
return "Image NOT saved";
}
}
else
{
header ('Content-Type: image/gif');
imagegif($this->newImage);
}
imagedestroy($this->newImage);
return;
}
/**
* Draws the scaled and cropped image in $this->newImage.
* @return void.
*/
private function redraw()
{
$newWidth = $this->width;
$newHeight = $this->height;
$offsetX = 0;
$offsetY = 0;
$pr = $this->sourceWidth/$this->sourceHeight;
$tr = $this->width/$this->height;
if($pr > $tr)
{
$newWidth = round($this->height*$pr);
$offsetX = -round(($newWidth - $this->width) / 2);
}
else
{
$newHeight = round($this->width/$pr);
$offsetY = -round(($newHeight - $this->height) / 2);
}
$this->newImage = imagecreatetruecolor($this->width, $this->height);
imagecopyresampled($this->newImage, $this->imageData, $offsetX, $offsetY, 0, 0, $newWidth, $newHeight, $this->sourceWidth, $this->sourceHeight);
return;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment