setFileSize(868, 86); $slice->setCutSize(124, 86); $slice->create(5); // Manage Files Here // $slice->deletePieces(); * */ class Slicer { /** @var $_image The name of the image being slices */ private $_image; /** @var $_filename The random name for the temporary file */ private $_filename; private $_x; private $_y; /** @var $_sliceSizeX The width of each slice size */ private $_sliceSizeX; /** @var $_sliceSizeY The height of each slice size */ private $_sliceSizeY; /** @var $_totalSlices The total amount of slices */ private $_totalSlices; /** @var $_fileList The list of files */ private $_fileList = array(); /** * @desc Pass an image into here and create a random filename * @param $image The location of the image to slice */ public function __construct($image) { $this->_image = $image; $this->_filename = strtotime('now') . rand(100000, 999999); } /** * @desc Set the overall dimensions of the incoming file * @param $x The width of the input file * @param $y The height of the input file */ public function setFileSize($x, $y) { $this->_fileDimensionX = (int) $x; $this->_fileDimensionY = (int) $y; } /** * @desc Set the overall dimensions of each slice * @param $_sliceSizeX The width of a slice * @param $_sliceSizeY The height of a slice */ public function setCutSize($x, $y) { $this->_totalSlicesizeX = (int) $x; $this->_totalSlicesizeY = (int) $y; } /** * @desc Creates the files for slicing * @param $slices How many slices to create */ public function create($slices) { $this->_totalSlices = (int) $slices; $this->_checkSetup(); $i = 0; while ($i < $this->_totalSlices) { /** Create a new blank image */ $this->dest = imagecreatetruecolor($this->_totalSlicesizeX, $this->_totalSlicesizeY); $this->src = imagecreatefromjpeg($this->_image); $startCut = ($i * $this->_totalSlicesizeX); $endCut = ($i + 1) * $this->_totalSlicesizeX; /** Copy the image coordinates to the blank image */ imagecopy($this->dest, $this->src, 0, 0, $startCut, 0, $endCut, $this->_totalSlicesizeY); /** Create the unique piece */ imagegif($this->dest, $this->_filename . "_$i" . '.gif'); /** Add this to the fileList */ $this->_fileList[] = $this->_filename . "_$i" . '.gif'; /** Free up the memory */ imagedestroy($this->dest); imagedestroy($this->src); $i++; } } /** * @desc Deletes all the images that were just created based off the total * amount of images from $this->_totalSlices */ public function deleteAll() { $i = 0; while ($i < $this->_totalSlices) { unlink($this->_filename . "_$i" . '.gif'); $i++; } } /** * @desc Returns the names of all the new sliced file names * @return The file List */ public function getFileList() { return $this->_fileList; } /** * @desc Returns the filename (Not include the _1 or _2, etc) nor the extension */ public function getFilename() { return $this->_filename; } /** * @desc Checks the integrity of the settings */ private function _checkSetup() { if (!isset($this->_totalSlicesizeX) || !isset($this->_totalSlicesizeY)) { throw new Exception("You must call the setCutSize() method."); } if (isset($this->_fileDimensionX) && isset($this->_fileDimensionY)) { $size = getimagesize($this->_image); $width = $size[0]; $height = $size[1]; if ($width != $this->_fileDimensionX && $height != $this->_fileDimensionY) { throw new Exception("Image size must be: {$this->_fileDimensionX} x {$this->_fileDimensionY}"); } } } }