diff --git a/.htaccess b/.htaccess
index 0189d10..5332f1b 100644
--- a/.htaccess
+++ b/.htaccess
@@ -1,12 +1,12 @@
-
";
+echo "Account:\r\n";
+print_r($dropbox->GetAccountInfo());
+
+$files = $dropbox->GetFiles("",false);
+
+echo "\r\n\r\nFiles:\r\n";
+print_r(array_keys($files));
+
+if(!empty($files)) {
+ $file = reset($files);
+ $test_file = "test_download_".basename($file->path);
+
+ echo "\r\n\r\nMeta data of $file->path:\r\n";
+ print_r($dropbox->GetMetadata($file->path));
+
+ echo "\r\n\r\nDownloading $file->path:\r\n";
+ print_r($dropbox->DownloadFile($file, $test_file));
+
+ echo "\r\n\r\nUploading $test_file:\r\n";
+ print_r($dropbox->UploadFile($test_file));
+ echo "\r\n done!";
+
+ echo "\r\n\r\nRevisions of $test_file:\r\n";
+ print_r($dropbox->GetRevisions($test_file));
+}
+
+echo "\r\n\r\nSearching for JPG files:\r\n";
+$jpg_files = $dropbox->Search("/", ".jpg", 5);
+if(empty($jpg_files))
+ echo "Nothing found.";
+else {
+ print_r($jpg_files);
+ $jpg_file = reset($jpg_files);
+
+ echo "\r\n\r\nThumbnail of $jpg_file->path:\r\n";
+ $img_data = base64_encode($dropbox->GetThumbnail($jpg_file->path));
+ echo "
";
+}
+
+
+function store_token($token, $name)
+{
+ if(!file_put_contents("tokens/$name.token", serialize($token)))
+ die('
Could not store token! Make sure that the directory `tokens` exists and is writable!');
+}
+
+function load_token($name)
+{
+ if(!file_exists("tokens/$name.token")) return null;
+ return @unserialize(@file_get_contents("tokens/$name.token"));
+}
+
+function delete_token($name)
+{
+ @unlink("tokens/$name.token");
+}
+
+
+
+
+
+function enable_implicit_flush()
+{
+ @apache_setenv('no-gzip', 1);
+ @ini_set('zlib.output_compression', 0);
+ @ini_set('implicit_flush', 1);
+ for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
+ ob_implicit_flush(1);
+ echo "";
+}
\ No newline at end of file
diff --git a/DropBoxPhp/test_download_Apps b/DropBoxPhp/test_download_Apps
new file mode 100644
index 0000000..d538cda
--- /dev/null
+++ b/DropBoxPhp/test_download_Apps
@@ -0,0 +1 @@
+{"error": "File not found"}
\ No newline at end of file
diff --git a/DropBoxPhp/tokens/access.token b/DropBoxPhp/tokens/access.token
new file mode 100644
index 0000000..42b3ffc
--- /dev/null
+++ b/DropBoxPhp/tokens/access.token
@@ -0,0 +1 @@
+a:2:{s:1:"t";s:16:"2lfmugdr7rp3yp2q";s:1:"s";s:15:"i3nrl13aduhhufw";}
\ No newline at end of file
diff --git a/MySqlClass.php b/MySqlClass.php
index 26eebe3..663a5fc 100644
--- a/MySqlClass.php
+++ b/MySqlClass.php
@@ -1,89 +1,87 @@
-attiva)
- {
- $this->connessione = mysql_connect($this->nomehost,$this->nomeuser,$this->password);
- if ($this->connessione == FALSE)
- die(mysql_error());
- mysql_select_db($this->mydb, $this->connessione) or die ("Errore nella selezione del database. Verificare i parametri nel file config.inc.php");
- $this->attiva = true;
- }
- else{
- return true;
- }
-
- }
-
- public function executeQuery($queryStr)
- {
- $this->connetti();
-
- if (!$res = mysql_query($queryStr, $this->connessione)) die(mysql_error());
- return true;
- return false;
- }
-
- public function insertRecord($queryStr)
- {
- $this->connetti();
-
- if (!$res = mysql_query($queryStr, $this->connessione)) die(mysql_error());
- return mysql_insert_id();
- }
-
- public function queryToObject($queryStr)
- {
- $this->connetti();
-
- $sth = mysql_query($queryStr, $this->connessione) or die(mysql_error());
-
- $rows = array();
- while($r = mysql_fetch_assoc($sth)) {
- array_push($rows,array_map('utf8_encode', $r));
- }
- mysql_free_result($sth);
- return $rows;
- }
-
- // funzione per la chiusura della connessione
- public function disconnetti()
- {
- if($this->attiva)
- {
- if(mysql_close($this->connessione))
- {
- $this->attiva = false;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
-
- public function __destruct()
- {
- $this->disconnetti();
- }
- }
+attiva) {
+ $this->connessione = mysqli_connect($this->nomehost, $this->nomeuser, $this->password);
+ if ($this->connessione == FALSE)
+ die(mysqli_error());
+ mysqli_select_db($this->connessione, $this->mydb) or die("Errore nella selezione del database. Verificare i parametri nel file config.inc.php");
+ $this->attiva = true;
+ }
+ else {
+ return true;
+ }
+ }
+
+ public function executeQuery($queryStr) {
+ $this->connetti();
+
+ if (!$res = mysqli_query($this->connessione, $queryStr))
+ die(mysqli_error());
+ return true;
+ }
+
+ public function insertRecord($queryStr) {
+ $this->connetti();
+
+ if (!$res = mysqli_query($this->connessione, $queryStr))
+ die(mysqli_error());
+ return mysqli_insert_id($this->connessione);
+ }
+
+ public function queryToObject($queryStr, $encode = true) {
+ $this->connetti();
+
+ $sth = mysqli_query($this->connessione, $queryStr) or die(mysqli_error());
+
+ if($encode){
+ $rows = array();
+ while ($r = mysqli_fetch_assoc($sth)) {
+ array_push($rows, array_map('utf8_encode', $r));
+ }
+ mysqli_free_result($sth);
+ return $rows;
+ }
+ else
+ {
+ return mysqli_fetch_array($sth);
+ }
+ }
+
+ // funzione per la chiusura della connessione
+ public function disconnetti() {
+ if ($this->attiva) {
+ if (mysqli_close($this->connessione)) {
+ $this->attiva = false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ public function __destruct() {
+ $this->disconnetti();
+ }
+
+}
+
?>
\ No newline at end of file
diff --git a/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php b/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php
index eac77ca..34c1562 100644
--- a/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php
+++ b/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php
@@ -1,22 +1,22 @@
-width = imagesx($image);
- $this->height = imagesy($image);
- $this->image = $image;
- $this->layers = $this->layerLevels = $this->layerPositions = array();
- $this->clearStack();
- }
-
- /**
- * Clone method: use it if you want to reuse an existing ImageWorkshop object in another variable
- * This is important because img resource var references all the same image in PHP.
- * Example: $b = clone $a; (never do $b = $a;)
- */
- public function __clone()
- {
- $this->createNewVarFromBackgroundImage();
- }
-
- // Superimpose a sublayer
- // =========================================================
-
- /**
- * Add an existing ImageWorkshop sublayer and set it in the stack at a given level
- * Return an array containing the generated sublayer id in the stack and its corrected level:
- * array("layerLevel" => integer, "id" => integer)
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $layerLevel
- * @param ImageWorkshop $layer
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * @return array
- */
- public function addLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- return $this->indexLayer($layerLevel, $layer, $positionX, $positionY, $position);
- }
-
- /**
- * Add an existing ImageWorkshop sublayer and set it in the stack at the highest level
- * Return an array containing the generated sublayer id in the stack and the highest level:
- * array("layerLevel" => integer, "id" => integer)
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param ImageWorkshop $layer
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * @return array
- */
- public function addLayerOnTop($layer, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- return $this->indexLayer($this->highestLayerLevel + 1, $layer, $positionX, $positionY, $position);
- }
-
- /**
- * Add an existing ImageWorkshop sublayer and set it in the stack at level 1
- * Return an array containing the generated sublayer id in the stack and level 1:
- * array("layerLevel" => integer, "id" => integer)
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param ImageWorkshop $layer
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * @return array
- */
- public function addLayerBelow($layer, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- return $this->indexLayer(1, $layer, $positionX, $positionY, $position);
- }
-
- // Move a sublayer inside the stack
- // =========================================================
-
- /**
- * Move a sublayer on the top of a group stack
- * Return new sublayer level if success or false otherwise
- *
- * @param integer $layerId
- * @return mixed
- */
- public function moveTop($layerId)
- {
- return $this->moveTo($layerId, $this->highestLayerLevel, false);
- }
-
- /**
- * Move a sublayer to the level 1 of a group stack
- * Return new sublayer level if success or false otherwise
- *
- * @param integer $layerId
- * @param integer $level
- *
- * @return mixed
- */
- public function moveBottom($layerId)
- {
- return $this->moveTo($layerId, 1, true);
- }
-
- /**
- * Move a sublayer to the level $level of a group stack
- * Return new sublayer level if success or false if layer isn't found
- *
- * Set $insertUnderTargetedLayer true if you want to move the sublayer under the other sublayer at the targeted level,
- * or false to insert it on the top of the other sublayer at the targeted level
- *
- * @param integer $layerId
- * @param integer $level
- * @param boolean $insertUnderTargetedLayer
- *
- * @return mixed
- */
- public function moveTo($layerId, $level, $insertUnderTargetedLayer = true)
- {
- // if the sublayer exists in stack
- if ($this->isLayerInIndex($layerId)) {
-
- $layerOldLevel = $this->getLayerLevel($layerId);
-
- if ($level < 1) {
- $level = 1;
- $insertUnderTargetedLayer = true;
- }
-
- if ($level > $this->highestLayerLevel) {
-
- $level = $this->highestLayerLevel;
- $insertUnderTargetedLayer = false;
- }
-
- // Not the same level than the current level
- if ($layerOldLevel != $level) {
-
- $isUnderAndNewLevelHigher = $isUnderAndNewLevelLower = $isOnTopAndNewLevelHigher = $isOnTopAndNewLevelLower = false;
-
- if ($insertUnderTargetedLayer) { // Under level
-
- if ($level > $layerOldLevel) { // new level higher
-
- $incrementorStartingValue = $layerOldLevel;
- $stopLoopWhenSmallerThan = $level;
- $isUnderAndNewLevelHigher = true;
-
- } else { // new level lower
-
- $incrementorStartingValue = $level;
- $stopLoopWhenSmallerThan = $layerOldLevel;
- $isUnderAndNewLevelLower = true;
- }
-
- } else { // on the top
-
- if ($level > $layerOldLevel) { // new level higher
-
- $incrementorStartingValue = $layerOldLevel;
- $stopLoopWhenSmallerThan = $level;
- $isOnTopAndNewLevelHigher = true;
-
- } else { // new level lower
-
- $incrementorStartingValue = $level;
- $stopLoopWhenSmallerThan = $layerOldLevel;
- $isOnTopAndNewLevelLower = true;
- }
- }
-
- ksort($this->layerLevels);
- $layerLevelsTmp = $this->layerLevels;
-
- if ($isOnTopAndNewLevelLower) {
- $level++;
- }
-
- for ($i = $incrementorStartingValue; $i < $stopLoopWhenSmallerThan; $i++) {
-
- if ($isUnderAndNewLevelHigher || $isOnTopAndNewLevelHigher) {
-
- $this->layerLevels[$i] = $layerLevelsTmp[$i + 1];
-
- } else {
-
- $this->layerLevels[$i + 1] = $layerLevelsTmp[$i];
- }
- }
-
- unset($layerLevelsTmp);
-
- if ($isUnderAndNewLevelHigher) {
- $level--;
- }
-
- $this->layerLevels[$level] = $layerId;
-
- return $level;
-
- } else {
- return $level;
- }
- }
-
- return false;
- }
-
- /**
- * Move up a sublayer in the stack (level +1)
- * Return new sublayer level if success, false otherwise
- *
- * @param integer $layerId
- *
- * @return mixed
- */
- public function moveUp($layerId)
- {
- if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
- $layerOldLevel = $this->getLayerLevel($layerId);
- return $this->moveTo($layerId, $layerOldLevel + 1, false);
- }
-
- return false;
- }
-
- /**
- * Move down a sublayer in the stack (level -1)
- * Return new sublayer level if success, false otherwise
- *
- * @param integer $layerId
- *
- * @return mixed
- */
- public function moveDown($layerId)
- {
- if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
- $layerOldLevel = $this->getLayerLevel($layerId);
- return $this->moveTo($layerId, $layerOldLevel - 1, true);
- }
-
- return false;
- }
-
- // Merge layers
- // =========================================================
-
- /**
- * Merge a sublayer with another sublayer below it in the stack
- * Note: the result layer will conserve the given id
- * Return true if success or false if layer isn't found or doesn't have a layer under it in the stack
- *
- * @param integer $layerId
- *
- * @return boolean
- */
- public function mergeDown($layerId)
- {
- // if the layer exists in document
- if ($this->isLayerInIndex($layerId)) {
-
- $layerLevel = $this->getLayerLevel($layerId);
- $layerPositions = $this->getLayerPositions($layerId);
- $layer = $this->getLayer($layerId);
- $layerWidth = $layer->getWidth();
- $layerHeight = $layer->getHeight();
- $layerPositionX = $this->layerPositions[$layerId]['x'];
- $layerPositionY = $this->layerPositions[$layerId]['y'];
-
- if ($layerLevel > 1) {
-
- $underLayerId = $this->layerLevels[$layerLevel - 1];
- $underLayer = $this->getLayer($underLayerId);
- $underLayerWidth = $underLayer->getWidth();
- $underLayerHeight = $underLayer->getHeight();
- $underLayerPositionX = $this->layerPositions[$underLayerId]['x'];
- $underLayerPositionY = $this->layerPositions[$underLayerId]['y'];
-
- $totalWidthLayer = $layerWidth + $layerPositionX;
- $totalHeightLayer = $layerHeight + $layerPositionY;
-
- $totalWidthUnderLayer = $underLayerWidth + $underLayerPositionX;
- $totalHeightUnderLayer = $underLayerHeight + $underLayerPositionY;
-
- $minLayerPositionX = $layerPositionX;
-
- if ($layerPositionX > $underLayerPositionX) {
- $minLayerPositionX = $underLayerPositionX;
- }
-
- $minLayerPositionY = $layerPositionY;
-
- if ($layerPositionY > $underLayerPositionY) {
- $minLayerPositionY = $underLayerPositionY;
- }
-
- if ($totalWidthLayer > $totalWidthUnderLayer) {
- $layerTmpWidth = $totalWidthLayer - $minLayerPositionX;
- } else {
- $layerTmpWidth = $totalWidthUnderLayer - $minLayerPositionX;
- }
-
- if ($totalHeightLayer > $totalHeightUnderLayer) {
- $layerTmpHeight = $totalHeightLayer - $minLayerPositionY;
- } else {
- $layerTmpHeight = $totalHeightUnderLayer - $minLayerPositionY;
- }
-
- $layerTmp = ImageWorkshop::initVirginLayer($layerTmpWidth, $layerTmpHeight);
-
- $layerTmp->addLayer(1, $underLayer, $underLayerPositionX - $minLayerPositionX, $underLayerPositionY - $minLayerPositionY);
- $layerTmp->addLayer(2, $layer, $layerPositionX - $minLayerPositionX, $layerPositionY - $minLayerPositionY);
-
- // Update layers
- $layerTmp->mergeAll();
- $this->layers[$underLayerId] = clone $layerTmp;
- $this->changePosition($underLayerId, $minLayerPositionX, $minLayerPositionX);
-
- } else {
-
- $layerTmp = ImageWorkshop::initFromResourceVar($this->image);
- $layerTmp->addLayer(1, $layer, $layerPositionX, $layerPositionY);
-
- $this->image = $layerTmp->getResult(); // Update background image
- }
-
- unset($layerTmp);
- $this->remove($layerId); // Remove the merged layer from the stack
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Merge sublayers in the stack on the layer background
- */
- public function mergeAll()
- {
- $this->image = $this->getResult();
- $this->clearStack();
- }
-
- /**
- * Paste an image on the layer
- * You can specify the position left (in pixels) and the position top (in pixels) of the added image relatives to the layer
- * Otherwise, it will be set at 0 and 0
- *
- * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default
- * @param resource $image
- * @param integer $positionX
- * @param integer $positionY
- */
- public function pasteImage($unit = self::UNIT_PIXEL, $image, $positionX = 0, $positionY = 0)
- {
- if ($unit == self::UNIT_PERCENT) {
-
- $positionX = round(($positionX / 100) * $this->width);
- $positionY = round(($positionY / 100) * $this->height);
- }
-
- imagecopy($this->image, $image, $positionX, $positionY, 0, 0, $image->getWidth(), $image->getHeight());
- }
-
- // Change sublayer positions
- // =========================================================
-
- /**
- * Change the position of a sublayer for new positions
- *
- * @param integer $layerId
- * @param integer $newPosX
- * @param integer $newPosY
- *
- * @return boolean
- */
- public function changePosition($layerId, $newPosX = null, $newPosY = null)
- {
- // if the sublayer exists in the stack
- if ($this->isLayerInIndex($layerId)) {
-
- if ($newPosX !== null) {
- $this->layerPositions[$layerId]['x'] = $newPosX;
- }
-
- if ($newPosY !== null) {
- $this->layerPositions[$layerId]['y'] = $newPosY;
- }
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Apply a translation on a sublayer that change its positions
- *
- * @param integer $layerId
- * @param integer $addedPosX
- * @param integer $addedPosY
- *
- * @return mixed (array of new positions or false if fail)
- */
- public function applyTranslation($layerId, $addedPosX = null, $addedPosY = null)
- {
- // if the sublayer exists in the stack
- if ($this->isLayerInIndex($layerId)) {
-
- if ($addedPosX !== null) {
- $this->layerPositions[$layerId]['x'] += $addedPosX;
- }
-
- if ($addedPosY !== null) {
- $this->layerPositions[$layerId]['y'] += $addedPosY;
- }
-
- return $this->layerPositions[$layerId];
- }
-
- return false;
- }
-
- // Removing sublayers
- // =========================================================
-
- /**
- * Delete a layer (return true if success, false if no sublayer is found)
- *
- * @param integer $layerId
- *
- * @return boolean
- */
- public function remove($layerId)
- {
- // if the layer exists in document
- if ($this->isLayerInIndex($layerId)) {
-
- $layerToDeleteLevel = $this->getLayerLevel($layerId);
-
- // delete
- $this->layers[$layerId]->delete();
- unset($this->layers[$layerId]);
- unset($this->layerLevels[$layerToDeleteLevel]);
- unset($this->layerPositions[$layerId]);
-
- // One or plural layers are sub of the deleted layer
- if (array_key_exists(($layerToDeleteLevel + 1), $this->layerLevels)) {
-
- ksort($this->layerLevels);
-
- $layerLevelsTmp = $this->layerLevels;
-
- $maxOldestLevel = 1;
- foreach ($layerLevelsTmp as $levelTmp => $layerIdTmp) {
-
- if ($levelTmp > $layerToDeleteLevel) {
- $this->layerLevels[($levelTmp - 1)] = $layerIdTmp;
- }
-
- $maxOldestLevel++;
- }
- unset($layerLevelsTmp);
- unset($this->layerLevels[$maxOldestLevel]);
- }
-
- $this->highestLayerLevel--;
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Reset the layer stack
- *
- * @boolean $deleteSubImgVar Delete sublayers image resource var
- */
- public function clearStack($deleteSubImgVar = true)
- {
- if ($deleteSubImgVar) {
- foreach ($this->layers as $layer) {
- $layer->delete();
- }
- }
-
- unset($this->layers);
- unset($this->layerLevels);
- unset($this->layerPositions);
-
- $this->lastLayerId = 0;
- $this->layers = array();
- $this->layerLevels = array();
- $this->layerPositions = array();
- $this->highestLayerLevel = 0;
- }
-
- // Perform an action
- // =========================================================
-
- /**
- * Resize the layer by specifying pixel
- *
- * @param integer $newWidth
- * @param integer $newHeight
- * @param boolean $converseProportion
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
- */
- public function resizeInPixel($newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
- {
- $this->resize(self::UNIT_PIXEL, $newWidth, $newHeight, $converseProportion, $positionX, $positionY, $position);
- }
-
- /**
- * Resize the layer by specifying a percent
- *
- * @param float $percentWidth
- * @param float $percentHeight
- * @param boolean $converseProportion
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
- */
- public function resizeInPercent($percentWidth = null, $percentHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
- {
- $this->resize(self::UNIT_PERCENT, $percentWidth, $percentHeight, $converseProportion, $positionX, $positionY, $position);
- }
-
- /**
- * Resize the layer to fit a bounding box by specifying pixel
- *
- * @param integer $width
- * @param integer $height
- * @param boolean $converseProportion
- */
- public function resizeToFit($width, $height, $converseProportion = false)
- {
- if ($this->getWidth() <= $width && $this->getHeight() <= $height) {
- return;
- }
-
- if (!$converseProportion) {
- $width = min($width, $this->getWidth());
- $height = min($height, $this->getHeight());
- }
-
- $this->resize(self::UNIT_PIXEL, $width, $height, $converseProportion ? 2 : false);
- }
-
- /**
- * Resize the layer
- *
- * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default
- * @param mixed $newWidth (integer or float)
- * @param mixed $newHeight (integer or float)
- * @param boolean $converseProportion
- * @param mixed $positionX (integer or float)
- * @param mixed $positionY (integer or float)
- * @param string $position
- *
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
- */
- public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
- {
- if (is_numeric($newWidth) || is_numeric($newHeight)) {
-
- if ($unit == self::UNIT_PERCENT) {
-
- if ($newWidth) {
- $newWidth = round(($newWidth / 100) * $this->width);
- }
-
- if ($newHeight) {
- $newHeight = round(($newHeight / 100) * $this->height);
- }
- }
-
- if (is_numeric($newWidth) && $newWidth <= 0) {
- $newWidth = 1;
- }
-
- if (is_numeric($newHeight) && $newHeight <= 0) {
- $newHeight = 1;
- }
-
- if ($converseProportion) { // Proportion are conserved
-
- if ($newWidth && $newHeight) { // Proportions + $newWidth + $newHeight
-
- if ($this->getWidth() > $this->getHeight()) {
-
- $this->resizeInPixel($newWidth, null, true);
-
- if ($this->getHeight() > $newHeight) {
- $this->resizeInPixel(null, $newHeight, true);
- }
-
- } else {
-
- $this->resizeInPixel(null, $newHeight, true);
-
- if ($this->getWidth() > $newWidth) {
- $this->resizeInPixel($newWidth, null, true);
- }
- }
-
- if ($converseProportion !== 2 && ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight)) {
-
- $layerTmp = ImageWorkshop::initVirginLayer($newWidth, $newHeight);
-
- $layerTmp->addLayer(1, $this, $positionX, $positionY, $position);
-
- // Reset part of stack
-
- unset($this->image);
- unset($this->layerLevels);
- unset($this->layerPositions);
- unset($this->layers);
-
- // Update current object
-
- $this->width = $layerTmp->getWidth();
- $this->height = $layerTmp->getHeight();
- $this->layerLevels = $layerTmp->layers[1]->getLayerLevels();
- $this->layerPositions = $layerTmp->layers[1]->getLayerPositions();
- $this->layers = $layerTmp->layers[1]->getLayers();
- $this->lastLayerId = $layerTmp->layers[1]->getLastLayerId();
- $this->highestLayerLevel = $layerTmp->layers[1]->getHighestLayerLevel();
-
- $translations = $layerTmp->getLayerPositions(1);
-
- foreach ($this->layers as $id => $layer) {
- $this->applyTranslation($id, $translations['x'], $translations['y']);
- }
-
- $layerTmp->layers[1]->clearStack(false);
- $this->image = $layerTmp->getResult();
- unset($layerTmp);
- }
-
- return;
-
- } elseif ($newWidth) {
-
- $widthResizePercent = $newWidth / ($this->width / 100);
- $newHeight = round(($widthResizePercent / 100) * $this->height);
- $heightResizePercent = $widthResizePercent;
-
- } elseif ($newHeight) {
-
- $heightResizePercent = $newHeight / ($this->height / 100);
- $newWidth = round(($heightResizePercent / 100) * $this->width);
- $widthResizePercent = $heightResizePercent;
- }
-
- } elseif (($newWidth && !$newHeight) || (!$newWidth && $newHeight)) { // New width OR new height is given
-
- if ($newWidth) {
-
- $widthResizePercent = $newWidth / ($this->width / 100);
- $heightResizePercent = 100;
- $newHeight = $this->height;
-
- } else {
-
- $heightResizePercent = $newHeight / ($this->height / 100);
- $widthResizePercent = 100;
- $newWidth = $this->width;
- }
-
- } else { // New width AND new height are given
-
- $widthResizePercent = $newWidth / ($this->width / 100);
- $heightResizePercent = $newHeight / ($this->height / 100);
- }
-
- // Update the layer positions in the stack
-
- foreach ($this->layerPositions as $layerId => $layerPosition) {
-
- $newPosX = round(($widthResizePercent / 100) * $layerPosition['x']);
- $newPosY = round(($heightResizePercent / 100) * $layerPosition['y']);
-
- $this->changePosition($layerId, $newPosX, $newPosY);
- }
-
- // Resize layers in the stack
-
- $layers = $this->layers;
-
- foreach ($layers as $key => $layer) {
- $layer->resizeInPercent($widthResizePercent, $heightResizePercent);
- $this->layers[$key] = $layer;
- }
-
- $this->resizeBackground($newWidth, $newHeight); // Resize the layer
- }
- }
-
- /**
- * Resize the layer by its largest side by specifying pixel
- *
- * @param integer $newLargestSideWidth
- * @param boolean $converseProportion
- */
- public function resizeByLargestSideInPixel($newLargestSideWidth, $converseProportion = false)
- {
- $this->resizeByLargestSide(self::UNIT_PIXEL, $newLargestSideWidth, $converseProportion);
- }
-
- /**
- * Resize the layer by its largest side by specifying percent
- *
- * @param integer $newLargestSideWidth percent
- * @param boolean $converseProportion
- */
- public function resizeByLargestSideInPercent($newLargestSideWidth, $converseProportion = false)
- {
- $this->resizeByLargestSide(self::UNIT_PERCENT, $newLargestSideWidth, $converseProportion);
- }
-
- /**
- * Resize the layer by its largest side
- *
- * @param string $unit
- * @param integer $newLargestSideWidth percent
- * @param boolean $converseProportion
- */
- public function resizeByLargestSide($unit = self::UNIT_PIXEL, $newLargestSideWidth, $converseProportion = false)
- {
- if ($unit == self::UNIT_PERCENT) {
- $newLargestSideWidth = round(($newLargestSideWidth / 100) * $this->getLargestSideWidth());
- }
-
- if ($this->getWidth() > $this->getHeight()) {
- $this->resizeInPixel($newLargestSideWidth, null, $converseProportion);
- } else {
- $this->resizeInPixel(null, $newLargestSideWidth, $converseProportion);
- }
- }
-
- /**
- * Resize the layer by its narrow side by specifying pixel
- *
- * @param integer $newNarrowSideWidth
- * @param boolean $converseProportion
- */
- public function resizeByNarrowSideInPixel($newNarrowSideWidth, $converseProportion = false)
- {
- $this->resizeByNarrowSide(self::UNIT_PIXEL, $newNarrowSideWidth, $converseProportion);
- }
-
- /**
- * Resize the layer by its narrow side by specifying percent
- *
- * @param integer $newNarrowSideWidth percent
- * @param boolean $converseProportion
- */
- public function resizeByNarrowSideInPercent($newNarrowSideWidth, $converseProportion = false)
- {
- $this->resizeByNarrowSide(self::UNIT_PERCENT, $newNarrowSideWidth, $converseProportion);
- }
-
- /**
- * Resize the layer by its narrow side
- *
- * @param string $unit
- * @param integer $newNarrowSideWidth
- * @param boolean $converseProportion
- */
- public function resizeByNarrowSide($unit = self::UNIT_PIXEL, $newNarrowSideWidth, $converseProportion = false)
- {
- if ($unit == self::UNIT_PERCENT) {
- $newNarrowSideWidth = round(($newNarrowSideWidth / 100) * $this->getNarrowSideWidth());
- }
-
- if ($this->getWidth() < $this->getHeight()) {
- $this->resizeInPixel($newNarrowSideWidth, null, $converseProportion);
- } else {
- $this->resizeInPixel(null, $newNarrowSideWidth, $converseProportion);
- }
- }
-
- /**
- * Crop the document by specifying pixels
- *
- * $backgroundColor: can be set transparent (The script will be longer to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $width
- * @param integer $height
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- */
- public function cropInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- $this->crop(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position);
- }
-
- /**
- * Crop the document by specifying percent
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param float $percentWidth
- * @param float $percentHeight
- * @param float $positionXPercent
- * @param float $positionYPercent
- * @param string $position
- */
- public function cropInPercent($percentWidth = 0, $percentHeight = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
- {
- $this->crop(self::UNIT_PERCENT, $percentWidth, $percentHeight, $positionXPercent, $positionYPercent, $position);
- }
-
- /**
- * Crop the document
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param string $unit
- * @param mixed $width (integer or float)
- * @param mixed $height (integer or float)
- * @param mixed $positionX (integer or float)
- * @param mixed $positionY (integer or float)
- * @param string $position
- */
- public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- if ($width < 0 || $height < 0) {
- throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED);
- }
-
- if ($unit == self::UNIT_PERCENT) {
-
- $width = round(($width / 100) * $this->width);
- $height = round(($height / 100) * $this->height);
-
- $positionX = round(($positionX / 100) * $this->width);
- $positionY = round(($positionY / 100) * $this->height);
- }
-
- if (($width != $this->width || $positionX == 0) || ($height != $this->height || $positionY == 0)) {
-
- if ($width == 0) {
- $width = 1;
- }
-
- if ($height == 0) {
- $height = 1;
- }
-
- $layerTmp = ImageWorkshop::initVirginLayer($width, $height);
- $layerClone = ImageWorkshop::initVirginLayer($this->width, $this->height);
-
- imagedestroy($layerClone->image);
- $layerClone->image = $this->image;
-
- $layerTmp->addLayer(1, $layerClone, -$positionX, -$positionY, $position);
-
- $newPos = $layerTmp->getLayerPositions();
- $layerNewPosX = $newPos[1]['x'];
- $layerNewPosY = $newPos[1]['y'];
-
- // update the layer
- $this->width = $layerTmp->getWidth();
- $this->height = $layerTmp->getHeight();
- $this->image = $layerTmp->getResult();
- unset($layerTmp);
- unset($layerClone);
-
- $this->updateLayerPositionsAfterCropping($layerNewPosX, $layerNewPosY);
- }
- }
-
- /**
- * Crop the document to a specific aspect ratio by specifying a shift in pixel
- *
- * $backgroundColor: can be set transparent (The script will be longer to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $width
- * @param integer $height
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- */
- public function cropToAspectRatioInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- $this->cropToAspectRatio(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position);
- }
-
- /**
- * Crop the document to a specific aspect ratio by specifying a shift in percent
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $width
- * @param integer $height
- * @param float $positionXPercent
- * @param float $positionYPercent
- * @param string $position
- */
- public function cropToAspectRatioInPercent($width = 0, $height = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
- {
- $this->cropToAspectRatio(self::UNIT_PERCENT, $width, $height, $positionXPercent, $positionYPercent, $position);
- }
-
- /**
- * Crop the document to a specific aspect ratio
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param string $unit
- * @param integer $width (integer or float)
- * @param integer $height (integer or float)
- * @param mixed $positionX (integer or float)
- * @param mixed $positionY (integer or float)
- * @param string $position
- */
- public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- if ($width < 0 || $height < 0) {
- throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED);
- }
-
- if ($width == 0) {
- $width = 1;
- }
-
- if ($height == 0) {
- $height = 1;
- }
-
- if ($this->width / $this->height <= $width / $height) {
- $newWidth = $this->width;
- $newHeight = round($height * ($this->width / $width));
- } else {
- $newWidth = round($width * ($this->height / $height));
- $newHeight = $this->height;
- }
-
- if ($unit == self::UNIT_PERCENT) {
- $positionX = round(($positionX / 100) * ($this->width - $newWidth));
- $positionY = round(($positionY / 100) * ($this->height - $newHeight));
- }
-
- $this->cropInPixel($newWidth, $newHeight, $positionX, $positionY, $position);
- }
-
- /**
- * Crop the maximum possible from left top ("LT"), "RT"... by specifying a shift in pixel
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $width
- * @param integer $height
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- */
- public function cropMaximumInPixel($positionX = 0, $positionY = 0, $position = 'LT')
- {
- $this->cropMaximum(self::UNIT_PIXEL, $positionX, $positionY, $position);
- }
-
- /**
- * Crop the maximum possible from left top ("LT"), "RT"... by specifying a shift in percent
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param integer $width
- * @param integer $height
- * @param integer $positionXPercent
- * @param integer $positionYPercent
- * @param string $position
- */
- public function cropMaximumInPercent($positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
- {
- $this->cropMaximum(self::UNIT_PERCENT, $positionXPercent, $positionYPercent, $position);
- }
-
- /**
- * Crop the maximum possible from left top
- *
- * $backgroundColor can be set transparent (but script could be long to execute)
- * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
- *
- * @param string $unit
- * @param integer $width
- * @param integer $height
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- */
- public function cropMaximum($unit = self::UNIT_PIXEL, $positionX = 0, $positionY = 0, $position = 'LT')
- {
- $narrowSide = $this->getNarrowSideWidth();
-
- if ($unit == self::UNIT_PERCENT) {
- $positionX = round(($positionX / 100) * $this->width);
- $positionY = round(($positionY / 100) * $this->height);
- }
-
- $this->cropInPixel($narrowSide, $narrowSide, $positionX, $positionY, $position);
- }
-
- /**
- * Rotate the layer (in degree)
- *
- * @param float $degrees
- */
- public function rotate($degrees)
- {
- if ($degrees != 0) {
-
- if ($degrees < -360 || $degrees > 360) {
- $degrees = $degrees % 360;
- }
-
- if ($degrees < 0 && $degrees >= -360) {
- $degrees = 360 + $degrees;
- }
-
- // Rotate the layer background image
- $imageRotated = imagerotate($this->image, -$degrees, -1);
- imagealphablending($imageRotated, true);
- imagesavealpha($imageRotated, true);
-
- unset($this->image);
-
- $this->image = $imageRotated;
-
- $oldWidth = $this->width;
- $oldHeight = $this->height;
-
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
-
- foreach ($this->layers as $layerId => $layer) {
-
- $layerSelfOldCenterPosition = array(
- 'x' => $layer->width / 2,
- 'y' => $layer->height / 2,
- );
-
- $smallImageCenter = array(
- 'x' => $layerSelfOldCenterPosition['x'] + $this->layerPositions[$layerId]['x'],
- 'y' => $layerSelfOldCenterPosition['y'] + $this->layerPositions[$layerId]['y'],
- );
-
- $this->layers[$layerId]->rotate($degrees);
-
- $ro = sqrt(pow($smallImageCenter['x'], 2) + pow($smallImageCenter['y'], 2));
-
- $teta = (acos($smallImageCenter['x'] / $ro)) * 180 / pi();
-
- $a = $ro * cos(($teta + $degrees) * pi() / 180);
- $b = $ro * sin(($teta + $degrees) * pi() / 180);
-
- if ($degrees > 0 && $degrees <= 90) {
-
- $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $oldHeight * sin(($degrees * pi()) / 180);
- $newPositionY = $b - ($this->layers[$layerId]->height / 2);
-
- } elseif ($degrees > 90 && $degrees <= 180) {
-
- $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $this->width;
- $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $oldHeight * (-cos(($degrees) * pi() / 180));
-
- } elseif ($degrees > 180 && $degrees <= 270) {
-
- $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $oldWidth * (-cos(($degrees) * pi() / 180));
- $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $this->height;
-
- } else {
-
- $newPositionX = $a - ($this->layers[$layerId]->width / 2);
- $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $oldWidth * (-sin(($degrees) * pi() / 180));
- }
-
- $this->layerPositions[$layerId] = array(
- 'x' => $newPositionX,
- 'y' => $newPositionY,
- );
- }
- }
- }
-
- /**
- * Change the opacity of the layer
- * $recursive: apply it on sublayers
- *
- * @param integer $opacity
- * @param boolean $recursive
- */
- public function opacity($opacity, $recursive = true)
- {
- if ($recursive) {
-
- $layers = $this->layers;
-
- foreach ($layers as $key => $layer) {
- $layer->opacity($opacity, true);
- $this->layers[$key] = $layer;
- }
- }
-
- $transparentImage = ImageWorkshopLib::generateImage($this->getWidth(), $this->getHeight());
-
- ImageWorkshopLib::imageCopyMergeAlpha($transparentImage, $this->image, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $opacity);
-
- unset($this->image);
- $this->image = $transparentImage;
- unset($transparentImage);
- }
-
- /**
- * Apply a filter on the layer
- * Be careful: some filters can damage transparent images, use it sparingly ! (A good pratice is to use mergeAll on your layer before applying a filter)
- *
- * @param int $filterType (http://www.php.net/manual/en/function.imagefilter.php)
- * @param int $arg1
- * @param int $arg2
- * @param int $arg3
- * @param int $arg4
- * @param boolean $recursive
- */
- public function applyFilter($filterType, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null, $recursive = false)
- {
- if ($filterType == IMG_FILTER_COLORIZE) {
- imagefilter($this->image, $filterType, $arg1, $arg2, $arg3, $arg4);
- } elseif ($filterType == IMG_FILTER_BRIGHTNESS || $filterType == IMG_FILTER_CONTRAST || $filterType == IMG_FILTER_SMOOTH) {
- imagefilter($this->image, $filterType, $arg1);
- } elseif ($filterType == IMG_FILTER_PIXELATE) {
- imagefilter($this->image, $filterType, $arg1, $arg2);
- } else {
- imagefilter($this->image, $filterType);
- }
-
- if ($recursive) {
-
- $layers = $this->layers;
-
- foreach($layers as $layerId => $layer) {
- $this->layers[$layerId]->applyFilter($filterType, $arg1, $arg2, $arg3, $arg4, true);
- }
- }
- }
-
- /**
- * Apply horizontal or vertical flip (Transformation)
- *
- * @param string $type
- */
- public function flip($type = 'horizontal')
- {
- $layers = $this->layers;
-
- foreach ($layers as $key => $layer) {
-
- $layer->flip($type);
- $this->layers[$key] = $layer;
- }
-
- $temp = ImageWorkshopLib::generateImage($this->width, $this->height);
-
- if ($type == 'horizontal') {
-
- imagecopyresampled($temp, $this->image, 0, 0, $this->width - 1, 0, $this->width, $this->height, -$this->width, $this->height);
- $this->image = $temp;
-
- foreach ($this->layerPositions as $layerId => $layerPositions) {
-
- $this->changePosition($layerId, $this->width - $this->layers[$layerId]->getWidth() - $layerPositions['x'], $layerPositions['y']);
- }
-
- } elseif ($type == 'vertical') {
-
- imagecopyresampled($temp, $this->image, 0, 0, 0, $this->height - 1, $this->width, $this->height, $this->width, -$this->height);
- $this->image = $temp;
-
- foreach ($this->layerPositions as $layerId => $layerPositions) {
-
- $this->changePosition($layerId, $layerPositions['x'], $this->height - $this->layers[$layerId]->getHeight() - $layerPositions['y']);
- }
- }
-
- unset($temp);
- }
-
- /**
- * Add a text on the background image of the layer using a default font registered in GD
- *
- * @param string $text
- * @param integer $font
- * @param string $color
- * @param integer $positionX
- * @param integer $positionY
- * @param string $align
- */
- public function writeText($text, $font = 1, $color = 'ffffff', $positionX = 0, $positionY = 0, $align = 'horizontal')
- {
- $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color);
- $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']);
-
- if ($align == 'horizontal') {
- imagestring($this->image, $font, $positionX, $positionY, $text, $textColor);
- } else {
- imagestringup($this->image, $font, $positionX, $positionY, $text, $textColor);
- }
- }
-
- /**
- * Add a text on the background image of the layer using a font localized at $fontPath
- * Return the text coordonates
- *
- * @param string $text
- * @param integer $fontPath
- * @param integer $fontSize
- * @param string $color
- * @param integer $positionX
- * @param integer $positionY
- * @param integer $fontRotation
- *
- * @return array
- */
- public function write($text, $fontPath, $fontSize = 13, $color = 'ffffff', $positionX = 0, $positionY = 0, $fontRotation = 0)
- {
- if (!file_exists($fontPath)) {
- throw new ImageWorkshopLayerException('Can\'t find a font file at this path : "'.$fontPath.'".', static::ERROR_FONT_NOT_FOUND);
- }
-
- $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color);
- $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']);
-
- return imagettftext($this->image, $fontSize, $fontRotation, $positionX, $positionY, $textColor, $fontPath, $text);
- }
-
- // Manage the result
- // =========================================================
-
- /**
- * Return a merged resource image
- *
- * $backgroundColor is really usefull if you want to save a JPG or GIF, because the transparency of the background
- * would be remove for a colored background, so you should choose a color like "ffffff" (white)
- *
- * @param string $backgroundColor
- *
- * @return resource
- */
- public function getResult($backgroundColor = null)
- {
- $imagesToMerge = array();
- ksort($this->layerLevels);
-
- foreach ($this->layerLevels as $layerLevel => $layerId) {
-
- $imagesToMerge[$layerLevel] = $this->layers[$layerId]->getResult();
-
- // Layer positions
- if ($this->layerPositions[$layerId]['x'] != 0 || $this->layerPositions[$layerId]['y'] != 0) {
-
- $virginLayoutImageTmp = ImageWorkshopLib::generateImage($this->width, $this->height);
- ImageWorkshopLib::mergeTwoImages($virginLayoutImageTmp, $imagesToMerge[$layerLevel], $this->layerPositions[$layerId]['x'], $this->layerPositions[$layerId]['y'], 0, 0);
- $imagesToMerge[$layerLevel] = $virginLayoutImageTmp;
- unset($virginLayoutImageTmp);
- }
- }
-
- $iterator = 1;
- $mergedImage = $this->image;
- ksort($imagesToMerge);
-
- foreach ($imagesToMerge as $imageLevel => $image) {
- ImageWorkshopLib::mergeTwoImages($mergedImage, $image);
- $iterator++;
- }
-
- $opacity = 127;
-
- if ($backgroundColor && $backgroundColor != 'transparent') {
- $opacity = 0;
- }
-
- $backgroundImage = ImageWorkshopLib::generateImage($this->width, $this->height, $backgroundColor, $opacity);
- ImageWorkshopLib::mergeTwoImages($backgroundImage, $mergedImage);
- $mergedImage = $backgroundImage;
- unset($backgroundImage);
-
- return $mergedImage;
- }
-
- /**
- * Save the resulting image at the specified path
- *
- * $backgroundColor is really usefull if you want to save a JPG or GIF, because the transparency of the background
- * would be remove for a colored background, so you should choose a color like "ffffff" (white)
- *
- * If the file already exists, it will be override !
- *
- * $imageQuality is useless for GIF
- *
- * Ex: $folder = __DIR__."/../web/images/2012"
- * $imageName = "butterfly.jpg"
- * $createFolders = true
- * $imageQuality = 95
- * $backgroundColor = "ffffff"
- *
- * @param string $folder
- * @param string $imageName
- * @param boolean $createFolders
- * @param string $backgroundColor
- * @param integer $imageQuality
- * @param boolean $interlace
- */
- public function save($folder, $imageName, $createFolders = true, $backgroundColor = null, $imageQuality = 75, $interlace = false)
- {
- if (!is_file($folder)) {
-
- if (is_dir($folder) || $createFolders) {
-
- // Creating the folders if they don't exist
- if (!is_dir($folder) && $createFolders) {
- $oldUmask = umask(0);
- mkdir($folder, 0777, true);
- umask($oldUmask);
- chmod($folder, 0777);
- }
-
- $extension = explode('.', $imageName);
- $extension = strtolower($extension[count($extension) - 1]);
-
- $filename = $folder.'/'.$imageName;
-
- if (($extension == 'jpg' || $extension == 'jpeg' || $extension == 'gif') && (!$backgroundColor || $backgroundColor == 'transparent')) {
- $backgroundColor = 'ffffff';
- }
-
- $image = $this->getResult($backgroundColor);
-
- imageinterlace($image, (int) $interlace);
-
- if ($extension == 'jpg' || $extension == 'jpeg') {
-
- imagejpeg($image, $filename, $imageQuality);
- unset($image);
-
- } elseif ($extension == 'gif') {
-
- imagegif($image, $filename);
- unset($image);
-
- } elseif ($extension == 'png') {
-
- $imageQuality = $imageQuality / 10;
- $imageQuality -= 1;
-
- imagepng($image, $filename, $imageQuality);
- unset($image);
- }
- }
- }
- }
-
- // Checkers
- // =========================================================
-
- /**
- * Check if a sublayer exists in the stack for a given id
- *
- * @param integer $layerId
- *
- * @return boolean
- */
- public function isLayerInIndex($layerId)
- {
- if (array_key_exists($layerId, $this->layers)) {
- return true;
- }
-
- return false;
- }
-
- // Getter / Setter
- // =========================================================
-
- /**
- * Return the narrow side width of the layer
- *
- * @return integer
- */
- public function getNarrowSideWidth()
- {
- $narrowSideWidth = $this->getWidth();
-
- if ($this->getHeight() < $narrowSideWidth) {
- $narrowSideWidth = $this->getHeight();
- }
-
- return $narrowSideWidth;
- }
-
- /**
- * Return the largest side width of the layer
- *
- * @return integer
- */
- public function getLargestSideWidth()
- {
- $largestSideWidth = $this->getWidth();
-
- if ($this->getHeight() > $largestSideWidth) {
- $largestSideWidth = $this->getHeight();
- }
-
- return $largestSideWidth;
- }
-
- /**
- * Get the level of a sublayer
- * Return sublayer level if success or false if layer isn't found
- *
- * @param integer $layerId
- *
- * @return mixed (integer or boolean)
- */
- public function getLayerLevel($layerId)
- {
- if ($this->isLayerInIndex($layerId)) { // if the layer exists in document
- return array_search($layerId, $this->layerLevels);
- }
-
- return false;
- }
-
- /**
- * Get a sublayer in the stack
- * Don't forget to use clone method: $b = clone $a->getLayer(3);
- *
- * @param integer $layerId
- *
- * @return ImageWorkshop
- */
- public function getLayer($layerId)
- {
- return $this->layers[$layerId];
- }
-
- /**
- * Getter width
- *
- * @return integer
- */
- public function getWidth()
- {
- return $this->width;
- }
-
- /**
- * Getter height
- *
- * @return integer
- */
- public function getHeight()
- {
- return $this->height;
- }
-
- /**
- * Getter image
- *
- * @return resource
- */
- public function getImage()
- {
- return $this->image;
- }
-
- /**
- * Getter layers
- *
- * @return array
- */
- public function getLayers()
- {
- return $this->layers;
- }
-
- /**
- * Getter layerLevels
- *
- * @return array
- */
- public function getLayerLevels()
- {
- return $this->layerLevels;
- }
-
- /**
- * Getter layerPositions
- *
- * Get all the positions of the sublayers,
- * or when specifying $layerId, get the position of this sublayer
- *
- * @param integer $layerId
- *
- * @return mixed (array or boolean)
- */
- public function getLayerPositions($layerId = null)
- {
- if (!$layerId) {
-
- return $this->layerPositions;
-
- } elseif ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
-
- return $this->layerPositions[$layerId];
- }
-
- return false;
- }
-
- /**
- * Getter highestLayerLevel
- *
- * @return array
- */
- public function getHighestLayerLevel()
- {
- return $this->highestLayerLevel;
- }
-
- /**
- * Getter lastLayerId
- *
- * @return array
- */
- public function getLastLayerId()
- {
- return $this->lastLayerId;
- }
-
- // Internals
- // =========================================================
-
- /**
- * Delete the current object
- */
- public function delete()
- {
- imagedestroy($this->image);
- $this->clearStack();
- }
-
- /**
- * Create a new background image var from the old background image var
- */
- public function createNewVarFromBackgroundImage()
- {
- $virginImage = ImageWorkshopLib::generateImage($this->getWidth(), $this->getHeight()); // New background image
-
- ImageWorkshopLib::mergeTwoImages($virginImage, $this->image, 0, 0, 0, 0);
- unset($this->image);
-
- $this->image = $virginImage;
- unset($virginImage);
-
- $layers = $this->layers;
-
- foreach($layers as $layerId => $layer) {
- $this->layers[$layerId] = clone $this->layers[$layerId];
- }
- }
-
- /**
- * Index a sublayer in the layer stack
- * Return an array containing the generated sublayer id and its final level:
- * array("layerLevel" => integer, "id" => integer)
- *
- * @param integer $layerLevel
- * @param ImageWorkshopLayer $layer
- * @param integer $positionX
- * @param integer $positionY
- * @param string $position
- *
- * @return array
- */
- protected function indexLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $position)
- {
- // Choose an id for the added layer
- $layerId = $this->lastLayerId + 1;
-
- // Clone $layer to duplicate image resource var
- $layer = clone $layer;
-
- // Add the layer in the stack
- $this->layers[$layerId] = $layer;
-
- // Add the layer positions in the main layer
- $this->layerPositions[$layerId] = ImageWorkshopLib::calculatePositions($this->getWidth(), $this->getHeight(), $layer->getWidth(), $layer->getHeight(), $positionX, $positionY, $position);
-
- // Update the lastLayerId of the workshop
- $this->lastLayerId = $layerId;
-
- // Add the layer level in the stack
- $layerLevel = $this->indexLevelInDocument($layerLevel, $layerId);
-
- return array(
- 'layerLevel' => $layerLevel,
- 'id' => $layerId,
- );
- }
-
- /**
- * Index a layer level and update the layers levels in the document
- * Return the corrected level of the layer
- *
- * @param integer $layerLevel
- * @param integer $layerId
- *
- * @return integer
- */
- protected function indexLevelInDocument($layerLevel, $layerId)
- {
- if (array_key_exists($layerLevel, $this->layerLevels)) { // Level already exists
-
- ksort($this->layerLevels); // All layers after this level and the layer which have this level are updated
- $layerLevelsTmp = $this->layerLevels;
-
- foreach ($layerLevelsTmp as $levelTmp => $layerIdTmp) {
- if ($levelTmp >= $layerLevel) {
- $this->layerLevels[$levelTmp + 1] = $layerIdTmp;
- }
- }
-
- unset($layerLevelsTmp);
-
- } else { // Level isn't taken
- if ($this->highestLayerLevel < $layerLevel) { // If given level is too high, proceed adjustement
- $layerLevel = $this->highestLayerLevel + 1;
- }
- }
-
- $this->layerLevels[$layerLevel] = $layerId;
- $this->highestLayerLevel = max(array_flip($this->layerLevels)); // Update $highestLayerLevel
-
- return $layerLevel;
- }
-
- /**
- * Update the positions of layers in the stack after cropping
- *
- * @param integer $positionX
- * @param integer $positionY
- */
- public function updateLayerPositionsAfterCropping($positionX, $positionY)
- {
- foreach ($this->layers as $layerId => $layer) {
-
- $oldLayerPosX = $this->layerPositions[$layerId]['x'];
- $oldLayerPosY = $this->layerPositions[$layerId]['y'];
-
- $newLayerPosX = $oldLayerPosX + $positionX;
- $newLayerPosY = $oldLayerPosY + $positionY;
-
- $this->changePosition($layerId, $newLayerPosX, $newLayerPosY);
- }
- }
-
- /**
- * Resize the background of a layer
- *
- * @param integer $newWidth
- * @param integer $newHeight
- */
- public function resizeBackground($newWidth, $newHeight)
- {
- $oldWidth = $this->width;
- $oldHeight = $this->height;
-
- $this->width = $newWidth;
- $this->height = $newHeight;
-
- $virginLayoutImage = ImageWorkshopLib::generateImage($this->width, $this->height);
-
- imagecopyresampled($virginLayoutImage, $this->image, 0, 0, 0, 0, $this->width, $this->height, $oldWidth, $oldHeight);
-
- unset($this->image);
- $this->image = $virginLayoutImage;
- }
-
- // Deprecated, don't use anymore
- // =========================================================
-
- /**
- * @deprecated
- */
- public function resizeInPourcent($percentWidth = null, $percentHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
- {
- throw new ImageWorkshopLayerException('Method resizeInPourcent() was renamed resizeInPercent(). Use resizeInPercent() instead.', static::METHOD_DEPRECATED);
- }
-
- /**
- * @deprecated
- */
- public function resizeByLargestSideInPourcent($newLargestSideWidth, $converseProportion = false)
- {
- throw new ImageWorkshopLayerException('Method resizeByLargestSideInPourcent() was renamed resizeByLargestSideInPercent(). Use resizeByLargestSideInPercent() instead.', static::METHOD_DEPRECATED);
- }
-
- /**
- * @deprecated
- */
- public function resizeByNarrowSideInPourcent($newNarrowSideWidth, $converseProportion = false)
- {
- throw new ImageWorkshopLayerException('Method resizeByNarrowSideInPourcent() was renamed resizeByNarrowSideInPercent(). Use resizeByNarrowSideInPercent() instead.', static::METHOD_DEPRECATED);
- }
-
- /**
- * @deprecated
- */
- public function cropInPourcent($percentWidth = 0, $percentHeight = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
- {
- throw new ImageWorkshopLayerException('Method cropInPourcent() was renamed cropInPercent(). Use cropInPercent() instead.', static::METHOD_DEPRECATED);
- }
-
- /**
- * @deprecated
- */
- public function cropMaximumInPourcent($positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
- {
- throw new ImageWorkshopLayerException('Method cropMaximumInPourcent() was renamed cropMaximumInPercent(). Use cropMaximumInPercent() instead.', static::METHOD_DEPRECATED);
- }
-}
+width = imagesx($image);
+ $this->height = imagesy($image);
+ $this->image = $image;
+ $this->layers = $this->layerLevels = $this->layerPositions = array();
+ $this->clearStack();
+ }
+
+ /**
+ * Clone method: use it if you want to reuse an existing ImageWorkshop object in another variable
+ * This is important because img resource var references all the same image in PHP.
+ * Example: $b = clone $a; (never do $b = $a;)
+ */
+ public function __clone()
+ {
+ $this->createNewVarFromBackgroundImage();
+ }
+
+ // Superimpose a sublayer
+ // =========================================================
+
+ /**
+ * Add an existing ImageWorkshop sublayer and set it in the stack at a given level
+ * Return an array containing the generated sublayer id in the stack and its corrected level:
+ * array("layerLevel" => integer, "id" => integer)
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $layerLevel
+ * @param ImageWorkshop $layer
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * @return array
+ */
+ public function addLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ return $this->indexLayer($layerLevel, $layer, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Add an existing ImageWorkshop sublayer and set it in the stack at the highest level
+ * Return an array containing the generated sublayer id in the stack and the highest level:
+ * array("layerLevel" => integer, "id" => integer)
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param ImageWorkshop $layer
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * @return array
+ */
+ public function addLayerOnTop($layer, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ return $this->indexLayer($this->highestLayerLevel + 1, $layer, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Add an existing ImageWorkshop sublayer and set it in the stack at level 1
+ * Return an array containing the generated sublayer id in the stack and level 1:
+ * array("layerLevel" => integer, "id" => integer)
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param ImageWorkshop $layer
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * @return array
+ */
+ public function addLayerBelow($layer, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ return $this->indexLayer(1, $layer, $positionX, $positionY, $position);
+ }
+
+ // Move a sublayer inside the stack
+ // =========================================================
+
+ /**
+ * Move a sublayer on the top of a group stack
+ * Return new sublayer level if success or false otherwise
+ *
+ * @param integer $layerId
+ * @return mixed
+ */
+ public function moveTop($layerId)
+ {
+ return $this->moveTo($layerId, $this->highestLayerLevel, false);
+ }
+
+ /**
+ * Move a sublayer to the level 1 of a group stack
+ * Return new sublayer level if success or false otherwise
+ *
+ * @param integer $layerId
+ * @param integer $level
+ *
+ * @return mixed
+ */
+ public function moveBottom($layerId)
+ {
+ return $this->moveTo($layerId, 1, true);
+ }
+
+ /**
+ * Move a sublayer to the level $level of a group stack
+ * Return new sublayer level if success or false if layer isn't found
+ *
+ * Set $insertUnderTargetedLayer true if you want to move the sublayer under the other sublayer at the targeted level,
+ * or false to insert it on the top of the other sublayer at the targeted level
+ *
+ * @param integer $layerId
+ * @param integer $level
+ * @param boolean $insertUnderTargetedLayer
+ *
+ * @return mixed
+ */
+ public function moveTo($layerId, $level, $insertUnderTargetedLayer = true)
+ {
+ // if the sublayer exists in stack
+ if ($this->isLayerInIndex($layerId)) {
+
+ $layerOldLevel = $this->getLayerLevel($layerId);
+
+ if ($level < 1) {
+ $level = 1;
+ $insertUnderTargetedLayer = true;
+ }
+
+ if ($level > $this->highestLayerLevel) {
+
+ $level = $this->highestLayerLevel;
+ $insertUnderTargetedLayer = false;
+ }
+
+ // Not the same level than the current level
+ if ($layerOldLevel != $level) {
+
+ $isUnderAndNewLevelHigher = $isUnderAndNewLevelLower = $isOnTopAndNewLevelHigher = $isOnTopAndNewLevelLower = false;
+
+ if ($insertUnderTargetedLayer) { // Under level
+
+ if ($level > $layerOldLevel) { // new level higher
+
+ $incrementorStartingValue = $layerOldLevel;
+ $stopLoopWhenSmallerThan = $level;
+ $isUnderAndNewLevelHigher = true;
+
+ } else { // new level lower
+
+ $incrementorStartingValue = $level;
+ $stopLoopWhenSmallerThan = $layerOldLevel;
+ $isUnderAndNewLevelLower = true;
+ }
+
+ } else { // on the top
+
+ if ($level > $layerOldLevel) { // new level higher
+
+ $incrementorStartingValue = $layerOldLevel;
+ $stopLoopWhenSmallerThan = $level;
+ $isOnTopAndNewLevelHigher = true;
+
+ } else { // new level lower
+
+ $incrementorStartingValue = $level;
+ $stopLoopWhenSmallerThan = $layerOldLevel;
+ $isOnTopAndNewLevelLower = true;
+ }
+ }
+
+ ksort($this->layerLevels);
+ $layerLevelsTmp = $this->layerLevels;
+
+ if ($isOnTopAndNewLevelLower) {
+ $level++;
+ }
+
+ for ($i = $incrementorStartingValue; $i < $stopLoopWhenSmallerThan; $i++) {
+
+ if ($isUnderAndNewLevelHigher || $isOnTopAndNewLevelHigher) {
+
+ $this->layerLevels[$i] = $layerLevelsTmp[$i + 1];
+
+ } else {
+
+ $this->layerLevels[$i + 1] = $layerLevelsTmp[$i];
+ }
+ }
+
+ unset($layerLevelsTmp);
+
+ if ($isUnderAndNewLevelHigher) {
+ $level--;
+ }
+
+ $this->layerLevels[$level] = $layerId;
+
+ return $level;
+
+ } else {
+ return $level;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Move up a sublayer in the stack (level +1)
+ * Return new sublayer level if success, false otherwise
+ *
+ * @param integer $layerId
+ *
+ * @return mixed
+ */
+ public function moveUp($layerId)
+ {
+ if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
+ $layerOldLevel = $this->getLayerLevel($layerId);
+ return $this->moveTo($layerId, $layerOldLevel + 1, false);
+ }
+
+ return false;
+ }
+
+ /**
+ * Move down a sublayer in the stack (level -1)
+ * Return new sublayer level if success, false otherwise
+ *
+ * @param integer $layerId
+ *
+ * @return mixed
+ */
+ public function moveDown($layerId)
+ {
+ if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
+ $layerOldLevel = $this->getLayerLevel($layerId);
+ return $this->moveTo($layerId, $layerOldLevel - 1, true);
+ }
+
+ return false;
+ }
+
+ // Merge layers
+ // =========================================================
+
+ /**
+ * Merge a sublayer with another sublayer below it in the stack
+ * Note: the result layer will conserve the given id
+ * Return true if success or false if layer isn't found or doesn't have a layer under it in the stack
+ *
+ * @param integer $layerId
+ *
+ * @return boolean
+ */
+ public function mergeDown($layerId)
+ {
+ // if the layer exists in document
+ if ($this->isLayerInIndex($layerId)) {
+
+ $layerLevel = $this->getLayerLevel($layerId);
+ $layerPositions = $this->getLayerPositions($layerId);
+ $layer = $this->getLayer($layerId);
+ $layerWidth = $layer->getWidth();
+ $layerHeight = $layer->getHeight();
+ $layerPositionX = $this->layerPositions[$layerId]['x'];
+ $layerPositionY = $this->layerPositions[$layerId]['y'];
+
+ if ($layerLevel > 1) {
+
+ $underLayerId = $this->layerLevels[$layerLevel - 1];
+ $underLayer = $this->getLayer($underLayerId);
+ $underLayerWidth = $underLayer->getWidth();
+ $underLayerHeight = $underLayer->getHeight();
+ $underLayerPositionX = $this->layerPositions[$underLayerId]['x'];
+ $underLayerPositionY = $this->layerPositions[$underLayerId]['y'];
+
+ $totalWidthLayer = $layerWidth + $layerPositionX;
+ $totalHeightLayer = $layerHeight + $layerPositionY;
+
+ $totalWidthUnderLayer = $underLayerWidth + $underLayerPositionX;
+ $totalHeightUnderLayer = $underLayerHeight + $underLayerPositionY;
+
+ $minLayerPositionX = $layerPositionX;
+
+ if ($layerPositionX > $underLayerPositionX) {
+ $minLayerPositionX = $underLayerPositionX;
+ }
+
+ $minLayerPositionY = $layerPositionY;
+
+ if ($layerPositionY > $underLayerPositionY) {
+ $minLayerPositionY = $underLayerPositionY;
+ }
+
+ if ($totalWidthLayer > $totalWidthUnderLayer) {
+ $layerTmpWidth = $totalWidthLayer - $minLayerPositionX;
+ } else {
+ $layerTmpWidth = $totalWidthUnderLayer - $minLayerPositionX;
+ }
+
+ if ($totalHeightLayer > $totalHeightUnderLayer) {
+ $layerTmpHeight = $totalHeightLayer - $minLayerPositionY;
+ } else {
+ $layerTmpHeight = $totalHeightUnderLayer - $minLayerPositionY;
+ }
+
+ $layerTmp = ImageWorkshop::initVirginLayer($layerTmpWidth, $layerTmpHeight);
+
+ $layerTmp->addLayer(1, $underLayer, $underLayerPositionX - $minLayerPositionX, $underLayerPositionY - $minLayerPositionY);
+ $layerTmp->addLayer(2, $layer, $layerPositionX - $minLayerPositionX, $layerPositionY - $minLayerPositionY);
+
+ // Update layers
+ $layerTmp->mergeAll();
+ $this->layers[$underLayerId] = clone $layerTmp;
+ $this->changePosition($underLayerId, $minLayerPositionX, $minLayerPositionX);
+
+ } else {
+
+ $layerTmp = ImageWorkshop::initFromResourceVar($this->image);
+ $layerTmp->addLayer(1, $layer, $layerPositionX, $layerPositionY);
+
+ $this->image = $layerTmp->getResult(); // Update background image
+ }
+
+ unset($layerTmp);
+ $this->remove($layerId); // Remove the merged layer from the stack
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Merge sublayers in the stack on the layer background
+ */
+ public function mergeAll()
+ {
+ $this->image = $this->getResult();
+ $this->clearStack();
+ }
+
+ /**
+ * Paste an image on the layer
+ * You can specify the position left (in pixels) and the position top (in pixels) of the added image relatives to the layer
+ * Otherwise, it will be set at 0 and 0
+ *
+ * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default
+ * @param resource $image
+ * @param integer $positionX
+ * @param integer $positionY
+ */
+ public function pasteImage($unit = self::UNIT_PIXEL, $image, $positionX = 0, $positionY = 0)
+ {
+ if ($unit == self::UNIT_PERCENT) {
+
+ $positionX = round(($positionX / 100) * $this->width);
+ $positionY = round(($positionY / 100) * $this->height);
+ }
+
+ imagecopy($this->image, $image, $positionX, $positionY, 0, 0, $image->getWidth(), $image->getHeight());
+ }
+
+ // Change sublayer positions
+ // =========================================================
+
+ /**
+ * Change the position of a sublayer for new positions
+ *
+ * @param integer $layerId
+ * @param integer $newPosX
+ * @param integer $newPosY
+ *
+ * @return boolean
+ */
+ public function changePosition($layerId, $newPosX = null, $newPosY = null)
+ {
+ // if the sublayer exists in the stack
+ if ($this->isLayerInIndex($layerId)) {
+
+ if ($newPosX !== null) {
+ $this->layerPositions[$layerId]['x'] = $newPosX;
+ }
+
+ if ($newPosY !== null) {
+ $this->layerPositions[$layerId]['y'] = $newPosY;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Apply a translation on a sublayer that change its positions
+ *
+ * @param integer $layerId
+ * @param integer $addedPosX
+ * @param integer $addedPosY
+ *
+ * @return mixed (array of new positions or false if fail)
+ */
+ public function applyTranslation($layerId, $addedPosX = null, $addedPosY = null)
+ {
+ // if the sublayer exists in the stack
+ if ($this->isLayerInIndex($layerId)) {
+
+ if ($addedPosX !== null) {
+ $this->layerPositions[$layerId]['x'] += $addedPosX;
+ }
+
+ if ($addedPosY !== null) {
+ $this->layerPositions[$layerId]['y'] += $addedPosY;
+ }
+
+ return $this->layerPositions[$layerId];
+ }
+
+ return false;
+ }
+
+ // Removing sublayers
+ // =========================================================
+
+ /**
+ * Delete a layer (return true if success, false if no sublayer is found)
+ *
+ * @param integer $layerId
+ *
+ * @return boolean
+ */
+ public function remove($layerId)
+ {
+ // if the layer exists in document
+ if ($this->isLayerInIndex($layerId)) {
+
+ $layerToDeleteLevel = $this->getLayerLevel($layerId);
+
+ // delete
+ $this->layers[$layerId]->delete();
+ unset($this->layers[$layerId]);
+ unset($this->layerLevels[$layerToDeleteLevel]);
+ unset($this->layerPositions[$layerId]);
+
+ // One or plural layers are sub of the deleted layer
+ if (array_key_exists(($layerToDeleteLevel + 1), $this->layerLevels)) {
+
+ ksort($this->layerLevels);
+
+ $layerLevelsTmp = $this->layerLevels;
+
+ $maxOldestLevel = 1;
+ foreach ($layerLevelsTmp as $levelTmp => $layerIdTmp) {
+
+ if ($levelTmp > $layerToDeleteLevel) {
+ $this->layerLevels[($levelTmp - 1)] = $layerIdTmp;
+ }
+
+ $maxOldestLevel++;
+ }
+ unset($layerLevelsTmp);
+ unset($this->layerLevels[$maxOldestLevel]);
+ }
+
+ $this->highestLayerLevel--;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Reset the layer stack
+ *
+ * @boolean $deleteSubImgVar Delete sublayers image resource var
+ */
+ public function clearStack($deleteSubImgVar = true)
+ {
+ if ($deleteSubImgVar) {
+ foreach ($this->layers as $layer) {
+ $layer->delete();
+ }
+ }
+
+ unset($this->layers);
+ unset($this->layerLevels);
+ unset($this->layerPositions);
+
+ $this->lastLayerId = 0;
+ $this->layers = array();
+ $this->layerLevels = array();
+ $this->layerPositions = array();
+ $this->highestLayerLevel = 0;
+ }
+
+ // Perform an action
+ // =========================================================
+
+ /**
+ * Resize the layer by specifying pixel
+ *
+ * @param integer $newWidth
+ * @param integer $newHeight
+ * @param boolean $converseProportion
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
+ */
+ public function resizeInPixel($newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
+ {
+ $this->resize(self::UNIT_PIXEL, $newWidth, $newHeight, $converseProportion, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Resize the layer by specifying a percent
+ *
+ * @param float $percentWidth
+ * @param float $percentHeight
+ * @param boolean $converseProportion
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
+ */
+ public function resizeInPercent($percentWidth = null, $percentHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
+ {
+ $this->resize(self::UNIT_PERCENT, $percentWidth, $percentHeight, $converseProportion, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Resize the layer to fit a bounding box by specifying pixel
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param boolean $converseProportion
+ */
+ public function resizeToFit($width, $height, $converseProportion = false)
+ {
+ if ($this->getWidth() <= $width && $this->getHeight() <= $height) {
+ return;
+ }
+
+ if (!$converseProportion) {
+ $width = min($width, $this->getWidth());
+ $height = min($height, $this->getHeight());
+ }
+
+ $this->resize(self::UNIT_PIXEL, $width, $height, $converseProportion ? 2 : false);
+ }
+
+ /**
+ * Resize the layer
+ *
+ * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default
+ * @param mixed $newWidth (integer or float)
+ * @param mixed $newHeight (integer or float)
+ * @param boolean $converseProportion
+ * @param mixed $positionX (integer or float)
+ * @param mixed $positionY (integer or float)
+ * @param string $position
+ *
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
+ */
+ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
+ {
+ if (is_numeric($newWidth) || is_numeric($newHeight)) {
+
+ if ($unit == self::UNIT_PERCENT) {
+
+ if ($newWidth) {
+ $newWidth = round(($newWidth / 100) * $this->width);
+ }
+
+ if ($newHeight) {
+ $newHeight = round(($newHeight / 100) * $this->height);
+ }
+ }
+
+ if (is_numeric($newWidth) && $newWidth <= 0) {
+ $newWidth = 1;
+ }
+
+ if (is_numeric($newHeight) && $newHeight <= 0) {
+ $newHeight = 1;
+ }
+
+ if ($converseProportion) { // Proportion are conserved
+
+ if ($newWidth && $newHeight) { // Proportions + $newWidth + $newHeight
+
+ if ($this->getWidth() > $this->getHeight()) {
+
+ $this->resizeInPixel($newWidth, null, true);
+
+ if ($this->getHeight() > $newHeight) {
+ $this->resizeInPixel(null, $newHeight, true);
+ }
+
+ } else {
+
+ $this->resizeInPixel(null, $newHeight, true);
+
+ if ($this->getWidth() > $newWidth) {
+ $this->resizeInPixel($newWidth, null, true);
+ }
+ }
+
+ if ($converseProportion !== 2 && ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight)) {
+
+ $layerTmp = ImageWorkshop::initVirginLayer($newWidth, $newHeight);
+
+ $layerTmp->addLayer(1, $this, $positionX, $positionY, $position);
+
+ // Reset part of stack
+
+ unset($this->image);
+ unset($this->layerLevels);
+ unset($this->layerPositions);
+ unset($this->layers);
+
+ // Update current object
+
+ $this->width = $layerTmp->getWidth();
+ $this->height = $layerTmp->getHeight();
+ $this->layerLevels = $layerTmp->layers[1]->getLayerLevels();
+ $this->layerPositions = $layerTmp->layers[1]->getLayerPositions();
+ $this->layers = $layerTmp->layers[1]->getLayers();
+ $this->lastLayerId = $layerTmp->layers[1]->getLastLayerId();
+ $this->highestLayerLevel = $layerTmp->layers[1]->getHighestLayerLevel();
+
+ $translations = $layerTmp->getLayerPositions(1);
+
+ foreach ($this->layers as $id => $layer) {
+ $this->applyTranslation($id, $translations['x'], $translations['y']);
+ }
+
+ $layerTmp->layers[1]->clearStack(false);
+ $this->image = $layerTmp->getResult();
+ unset($layerTmp);
+ }
+
+ return;
+
+ } elseif ($newWidth) {
+
+ $widthResizePercent = $newWidth / ($this->width / 100);
+ $newHeight = round(($widthResizePercent / 100) * $this->height);
+ $heightResizePercent = $widthResizePercent;
+
+ } elseif ($newHeight) {
+
+ $heightResizePercent = $newHeight / ($this->height / 100);
+ $newWidth = round(($heightResizePercent / 100) * $this->width);
+ $widthResizePercent = $heightResizePercent;
+ }
+
+ } elseif (($newWidth && !$newHeight) || (!$newWidth && $newHeight)) { // New width OR new height is given
+
+ if ($newWidth) {
+
+ $widthResizePercent = $newWidth / ($this->width / 100);
+ $heightResizePercent = 100;
+ $newHeight = $this->height;
+
+ } else {
+
+ $heightResizePercent = $newHeight / ($this->height / 100);
+ $widthResizePercent = 100;
+ $newWidth = $this->width;
+ }
+
+ } else { // New width AND new height are given
+
+ $widthResizePercent = $newWidth / ($this->width / 100);
+ $heightResizePercent = $newHeight / ($this->height / 100);
+ }
+
+ // Update the layer positions in the stack
+
+ foreach ($this->layerPositions as $layerId => $layerPosition) {
+
+ $newPosX = round(($widthResizePercent / 100) * $layerPosition['x']);
+ $newPosY = round(($heightResizePercent / 100) * $layerPosition['y']);
+
+ $this->changePosition($layerId, $newPosX, $newPosY);
+ }
+
+ // Resize layers in the stack
+
+ $layers = $this->layers;
+
+ foreach ($layers as $key => $layer) {
+ $layer->resizeInPercent($widthResizePercent, $heightResizePercent);
+ $this->layers[$key] = $layer;
+ }
+
+ $this->resizeBackground($newWidth, $newHeight); // Resize the layer
+ }
+ }
+
+ /**
+ * Resize the layer by its largest side by specifying pixel
+ *
+ * @param integer $newLargestSideWidth
+ * @param boolean $converseProportion
+ */
+ public function resizeByLargestSideInPixel($newLargestSideWidth, $converseProportion = false)
+ {
+ $this->resizeByLargestSide(self::UNIT_PIXEL, $newLargestSideWidth, $converseProportion);
+ }
+
+ /**
+ * Resize the layer by its largest side by specifying percent
+ *
+ * @param integer $newLargestSideWidth percent
+ * @param boolean $converseProportion
+ */
+ public function resizeByLargestSideInPercent($newLargestSideWidth, $converseProportion = false)
+ {
+ $this->resizeByLargestSide(self::UNIT_PERCENT, $newLargestSideWidth, $converseProportion);
+ }
+
+ /**
+ * Resize the layer by its largest side
+ *
+ * @param string $unit
+ * @param integer $newLargestSideWidth percent
+ * @param boolean $converseProportion
+ */
+ public function resizeByLargestSide($unit = self::UNIT_PIXEL, $newLargestSideWidth, $converseProportion = false)
+ {
+ if ($unit == self::UNIT_PERCENT) {
+ $newLargestSideWidth = round(($newLargestSideWidth / 100) * $this->getLargestSideWidth());
+ }
+
+ if ($this->getWidth() > $this->getHeight()) {
+ $this->resizeInPixel($newLargestSideWidth, null, $converseProportion);
+ } else {
+ $this->resizeInPixel(null, $newLargestSideWidth, $converseProportion);
+ }
+ }
+
+ /**
+ * Resize the layer by its narrow side by specifying pixel
+ *
+ * @param integer $newNarrowSideWidth
+ * @param boolean $converseProportion
+ */
+ public function resizeByNarrowSideInPixel($newNarrowSideWidth, $converseProportion = false)
+ {
+ $this->resizeByNarrowSide(self::UNIT_PIXEL, $newNarrowSideWidth, $converseProportion);
+ }
+
+ /**
+ * Resize the layer by its narrow side by specifying percent
+ *
+ * @param integer $newNarrowSideWidth percent
+ * @param boolean $converseProportion
+ */
+ public function resizeByNarrowSideInPercent($newNarrowSideWidth, $converseProportion = false)
+ {
+ $this->resizeByNarrowSide(self::UNIT_PERCENT, $newNarrowSideWidth, $converseProportion);
+ }
+
+ /**
+ * Resize the layer by its narrow side
+ *
+ * @param string $unit
+ * @param integer $newNarrowSideWidth
+ * @param boolean $converseProportion
+ */
+ public function resizeByNarrowSide($unit = self::UNIT_PIXEL, $newNarrowSideWidth, $converseProportion = false)
+ {
+ if ($unit == self::UNIT_PERCENT) {
+ $newNarrowSideWidth = round(($newNarrowSideWidth / 100) * $this->getNarrowSideWidth());
+ }
+
+ if ($this->getWidth() < $this->getHeight()) {
+ $this->resizeInPixel($newNarrowSideWidth, null, $converseProportion);
+ } else {
+ $this->resizeInPixel(null, $newNarrowSideWidth, $converseProportion);
+ }
+ }
+
+ /**
+ * Crop the document by specifying pixels
+ *
+ * $backgroundColor: can be set transparent (The script will be longer to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ */
+ public function cropInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ $this->crop(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Crop the document by specifying percent
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param float $percentWidth
+ * @param float $percentHeight
+ * @param float $positionXPercent
+ * @param float $positionYPercent
+ * @param string $position
+ */
+ public function cropInPercent($percentWidth = 0, $percentHeight = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
+ {
+ $this->crop(self::UNIT_PERCENT, $percentWidth, $percentHeight, $positionXPercent, $positionYPercent, $position);
+ }
+
+ /**
+ * Crop the document
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param string $unit
+ * @param mixed $width (integer or float)
+ * @param mixed $height (integer or float)
+ * @param mixed $positionX (integer or float)
+ * @param mixed $positionY (integer or float)
+ * @param string $position
+ */
+ public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ if ($width < 0 || $height < 0) {
+ throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED);
+ }
+
+ if ($unit == self::UNIT_PERCENT) {
+
+ $width = round(($width / 100) * $this->width);
+ $height = round(($height / 100) * $this->height);
+
+ $positionX = round(($positionX / 100) * $this->width);
+ $positionY = round(($positionY / 100) * $this->height);
+ }
+
+ if (($width != $this->width || $positionX == 0) || ($height != $this->height || $positionY == 0)) {
+
+ if ($width == 0) {
+ $width = 1;
+ }
+
+ if ($height == 0) {
+ $height = 1;
+ }
+
+ $layerTmp = ImageWorkshop::initVirginLayer($width, $height);
+ $layerClone = ImageWorkshop::initVirginLayer($this->width, $this->height);
+
+ imagedestroy($layerClone->image);
+ $layerClone->image = $this->image;
+
+ $layerTmp->addLayer(1, $layerClone, -$positionX, -$positionY, $position);
+
+ $newPos = $layerTmp->getLayerPositions();
+ $layerNewPosX = $newPos[1]['x'];
+ $layerNewPosY = $newPos[1]['y'];
+
+ // update the layer
+ $this->width = $layerTmp->getWidth();
+ $this->height = $layerTmp->getHeight();
+ $this->image = $layerTmp->getResult();
+ unset($layerTmp);
+ unset($layerClone);
+
+ $this->updateLayerPositionsAfterCropping($layerNewPosX, $layerNewPosY);
+ }
+ }
+
+ /**
+ * Crop the document to a specific aspect ratio by specifying a shift in pixel
+ *
+ * $backgroundColor: can be set transparent (The script will be longer to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ */
+ public function cropToAspectRatioInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ $this->cropToAspectRatio(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Crop the document to a specific aspect ratio by specifying a shift in percent
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param float $positionXPercent
+ * @param float $positionYPercent
+ * @param string $position
+ */
+ public function cropToAspectRatioInPercent($width = 0, $height = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
+ {
+ $this->cropToAspectRatio(self::UNIT_PERCENT, $width, $height, $positionXPercent, $positionYPercent, $position);
+ }
+
+ /**
+ * Crop the document to a specific aspect ratio
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param string $unit
+ * @param integer $width (integer or float)
+ * @param integer $height (integer or float)
+ * @param mixed $positionX (integer or float)
+ * @param mixed $positionY (integer or float)
+ * @param string $position
+ */
+ public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ if ($width < 0 || $height < 0) {
+ throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED);
+ }
+
+ if ($width == 0) {
+ $width = 1;
+ }
+
+ if ($height == 0) {
+ $height = 1;
+ }
+
+ if ($this->width / $this->height <= $width / $height) {
+ $newWidth = $this->width;
+ $newHeight = round($height * ($this->width / $width));
+ } else {
+ $newWidth = round($width * ($this->height / $height));
+ $newHeight = $this->height;
+ }
+
+ if ($unit == self::UNIT_PERCENT) {
+ $positionX = round(($positionX / 100) * ($this->width - $newWidth));
+ $positionY = round(($positionY / 100) * ($this->height - $newHeight));
+ }
+
+ $this->cropInPixel($newWidth, $newHeight, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Crop the maximum possible from left top ("LT"), "RT"... by specifying a shift in pixel
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ */
+ public function cropMaximumInPixel($positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ $this->cropMaximum(self::UNIT_PIXEL, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Crop the maximum possible from left top ("LT"), "RT"... by specifying a shift in percent
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param integer $positionXPercent
+ * @param integer $positionYPercent
+ * @param string $position
+ */
+ public function cropMaximumInPercent($positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
+ {
+ $this->cropMaximum(self::UNIT_PERCENT, $positionXPercent, $positionYPercent, $position);
+ }
+
+ /**
+ * Crop the maximum possible from left top
+ *
+ * $backgroundColor can be set transparent (but script could be long to execute)
+ * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
+ *
+ * @param string $unit
+ * @param integer $width
+ * @param integer $height
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ */
+ public function cropMaximum($unit = self::UNIT_PIXEL, $positionX = 0, $positionY = 0, $position = 'LT')
+ {
+ $narrowSide = $this->getNarrowSideWidth();
+
+ if ($unit == self::UNIT_PERCENT) {
+ $positionX = round(($positionX / 100) * $this->width);
+ $positionY = round(($positionY / 100) * $this->height);
+ }
+
+ $this->cropInPixel($narrowSide, $narrowSide, $positionX, $positionY, $position);
+ }
+
+ /**
+ * Rotate the layer (in degree)
+ *
+ * @param float $degrees
+ */
+ public function rotate($degrees)
+ {
+ if ($degrees != 0) {
+
+ if ($degrees < -360 || $degrees > 360) {
+ $degrees = $degrees % 360;
+ }
+
+ if ($degrees < 0 && $degrees >= -360) {
+ $degrees = 360 + $degrees;
+ }
+
+ // Rotate the layer background image
+ $imageRotated = imagerotate($this->image, -$degrees, -1);
+ imagealphablending($imageRotated, true);
+ imagesavealpha($imageRotated, true);
+
+ unset($this->image);
+
+ $this->image = $imageRotated;
+
+ $oldWidth = $this->width;
+ $oldHeight = $this->height;
+
+ $this->width = imagesx($this->image);
+ $this->height = imagesy($this->image);
+
+ foreach ($this->layers as $layerId => $layer) {
+
+ $layerSelfOldCenterPosition = array(
+ 'x' => $layer->width / 2,
+ 'y' => $layer->height / 2,
+ );
+
+ $smallImageCenter = array(
+ 'x' => $layerSelfOldCenterPosition['x'] + $this->layerPositions[$layerId]['x'],
+ 'y' => $layerSelfOldCenterPosition['y'] + $this->layerPositions[$layerId]['y'],
+ );
+
+ $this->layers[$layerId]->rotate($degrees);
+
+ $ro = sqrt(pow($smallImageCenter['x'], 2) + pow($smallImageCenter['y'], 2));
+
+ $teta = (acos($smallImageCenter['x'] / $ro)) * 180 / pi();
+
+ $a = $ro * cos(($teta + $degrees) * pi() / 180);
+ $b = $ro * sin(($teta + $degrees) * pi() / 180);
+
+ if ($degrees > 0 && $degrees <= 90) {
+
+ $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $oldHeight * sin(($degrees * pi()) / 180);
+ $newPositionY = $b - ($this->layers[$layerId]->height / 2);
+
+ } elseif ($degrees > 90 && $degrees <= 180) {
+
+ $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $this->width;
+ $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $oldHeight * (-cos(($degrees) * pi() / 180));
+
+ } elseif ($degrees > 180 && $degrees <= 270) {
+
+ $newPositionX = $a - ($this->layers[$layerId]->width / 2) + $oldWidth * (-cos(($degrees) * pi() / 180));
+ $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $this->height;
+
+ } else {
+
+ $newPositionX = $a - ($this->layers[$layerId]->width / 2);
+ $newPositionY = $b - ($this->layers[$layerId]->height / 2) + $oldWidth * (-sin(($degrees) * pi() / 180));
+ }
+
+ $this->layerPositions[$layerId] = array(
+ 'x' => $newPositionX,
+ 'y' => $newPositionY,
+ );
+ }
+ }
+ }
+
+ /**
+ * Change the opacity of the layer
+ * $recursive: apply it on sublayers
+ *
+ * @param integer $opacity
+ * @param boolean $recursive
+ */
+ public function opacity($opacity, $recursive = true)
+ {
+ if ($recursive) {
+
+ $layers = $this->layers;
+
+ foreach ($layers as $key => $layer) {
+ $layer->opacity($opacity, true);
+ $this->layers[$key] = $layer;
+ }
+ }
+
+ $transparentImage = ImageWorkshopLib::generateImage($this->getWidth(), $this->getHeight());
+
+ ImageWorkshopLib::imageCopyMergeAlpha($transparentImage, $this->image, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $opacity);
+
+ unset($this->image);
+ $this->image = $transparentImage;
+ unset($transparentImage);
+ }
+
+ /**
+ * Apply a filter on the layer
+ * Be careful: some filters can damage transparent images, use it sparingly ! (A good pratice is to use mergeAll on your layer before applying a filter)
+ *
+ * @param int $filterType (http://www.php.net/manual/en/function.imagefilter.php)
+ * @param int $arg1
+ * @param int $arg2
+ * @param int $arg3
+ * @param int $arg4
+ * @param boolean $recursive
+ */
+ public function applyFilter($filterType, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null, $recursive = false)
+ {
+ if ($filterType == IMG_FILTER_COLORIZE) {
+ imagefilter($this->image, $filterType, $arg1, $arg2, $arg3, $arg4);
+ } elseif ($filterType == IMG_FILTER_BRIGHTNESS || $filterType == IMG_FILTER_CONTRAST || $filterType == IMG_FILTER_SMOOTH) {
+ imagefilter($this->image, $filterType, $arg1);
+ } elseif ($filterType == IMG_FILTER_PIXELATE) {
+ imagefilter($this->image, $filterType, $arg1, $arg2);
+ } else {
+ imagefilter($this->image, $filterType);
+ }
+
+ if ($recursive) {
+
+ $layers = $this->layers;
+
+ foreach($layers as $layerId => $layer) {
+ $this->layers[$layerId]->applyFilter($filterType, $arg1, $arg2, $arg3, $arg4, true);
+ }
+ }
+ }
+
+ /**
+ * Apply horizontal or vertical flip (Transformation)
+ *
+ * @param string $type
+ */
+ public function flip($type = 'horizontal')
+ {
+ $layers = $this->layers;
+
+ foreach ($layers as $key => $layer) {
+
+ $layer->flip($type);
+ $this->layers[$key] = $layer;
+ }
+
+ $temp = ImageWorkshopLib::generateImage($this->width, $this->height);
+
+ if ($type == 'horizontal') {
+
+ imagecopyresampled($temp, $this->image, 0, 0, $this->width - 1, 0, $this->width, $this->height, -$this->width, $this->height);
+ $this->image = $temp;
+
+ foreach ($this->layerPositions as $layerId => $layerPositions) {
+
+ $this->changePosition($layerId, $this->width - $this->layers[$layerId]->getWidth() - $layerPositions['x'], $layerPositions['y']);
+ }
+
+ } elseif ($type == 'vertical') {
+
+ imagecopyresampled($temp, $this->image, 0, 0, 0, $this->height - 1, $this->width, $this->height, $this->width, -$this->height);
+ $this->image = $temp;
+
+ foreach ($this->layerPositions as $layerId => $layerPositions) {
+
+ $this->changePosition($layerId, $layerPositions['x'], $this->height - $this->layers[$layerId]->getHeight() - $layerPositions['y']);
+ }
+ }
+
+ unset($temp);
+ }
+
+ /**
+ * Add a text on the background image of the layer using a default font registered in GD
+ *
+ * @param string $text
+ * @param integer $font
+ * @param string $color
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $align
+ */
+ public function writeText($text, $font = 1, $color = 'ffffff', $positionX = 0, $positionY = 0, $align = 'horizontal')
+ {
+ $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color);
+ $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']);
+
+ if ($align == 'horizontal') {
+ imagestring($this->image, $font, $positionX, $positionY, $text, $textColor);
+ } else {
+ imagestringup($this->image, $font, $positionX, $positionY, $text, $textColor);
+ }
+ }
+
+ /**
+ * Add a text on the background image of the layer using a font localized at $fontPath
+ * Return the text coordonates
+ *
+ * @param string $text
+ * @param integer $fontPath
+ * @param integer $fontSize
+ * @param string $color
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param integer $fontRotation
+ *
+ * @return array
+ */
+ public function write($text, $fontPath, $fontSize = 13, $color = 'ffffff', $positionX = 0, $positionY = 0, $fontRotation = 0)
+ {
+ if (!file_exists($fontPath)) {
+ throw new ImageWorkshopLayerException('Can\'t find a font file at this path : "'.$fontPath.'".', static::ERROR_FONT_NOT_FOUND);
+ }
+
+ $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color);
+ $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']);
+
+ return imagettftext($this->image, $fontSize, $fontRotation, $positionX, $positionY, $textColor, $fontPath, $text);
+ }
+
+ // Manage the result
+ // =========================================================
+
+ /**
+ * Return a merged resource image
+ *
+ * $backgroundColor is really usefull if you want to save a JPG or GIF, because the transparency of the background
+ * would be remove for a colored background, so you should choose a color like "ffffff" (white)
+ *
+ * @param string $backgroundColor
+ *
+ * @return resource
+ */
+ public function getResult($backgroundColor = null)
+ {
+ $imagesToMerge = array();
+ ksort($this->layerLevels);
+
+ foreach ($this->layerLevels as $layerLevel => $layerId) {
+
+ $imagesToMerge[$layerLevel] = $this->layers[$layerId]->getResult();
+
+ // Layer positions
+ if ($this->layerPositions[$layerId]['x'] != 0 || $this->layerPositions[$layerId]['y'] != 0) {
+
+ $virginLayoutImageTmp = ImageWorkshopLib::generateImage($this->width, $this->height);
+ ImageWorkshopLib::mergeTwoImages($virginLayoutImageTmp, $imagesToMerge[$layerLevel], $this->layerPositions[$layerId]['x'], $this->layerPositions[$layerId]['y'], 0, 0);
+ $imagesToMerge[$layerLevel] = $virginLayoutImageTmp;
+ unset($virginLayoutImageTmp);
+ }
+ }
+
+ $iterator = 1;
+ $mergedImage = $this->image;
+ ksort($imagesToMerge);
+
+ foreach ($imagesToMerge as $imageLevel => $image) {
+ ImageWorkshopLib::mergeTwoImages($mergedImage, $image);
+ $iterator++;
+ }
+
+ $opacity = 127;
+
+ if ($backgroundColor && $backgroundColor != 'transparent') {
+ $opacity = 0;
+ }
+
+ $backgroundImage = ImageWorkshopLib::generateImage($this->width, $this->height, $backgroundColor, $opacity);
+ ImageWorkshopLib::mergeTwoImages($backgroundImage, $mergedImage);
+ $mergedImage = $backgroundImage;
+ unset($backgroundImage);
+
+ return $mergedImage;
+ }
+
+ /**
+ * Save the resulting image at the specified path
+ *
+ * $backgroundColor is really usefull if you want to save a JPG or GIF, because the transparency of the background
+ * would be remove for a colored background, so you should choose a color like "ffffff" (white)
+ *
+ * If the file already exists, it will be override !
+ *
+ * $imageQuality is useless for GIF
+ *
+ * Ex: $folder = __DIR__."/../web/images/2012"
+ * $imageName = "butterfly.jpg"
+ * $createFolders = true
+ * $imageQuality = 95
+ * $backgroundColor = "ffffff"
+ *
+ * @param string $folder
+ * @param string $imageName
+ * @param boolean $createFolders
+ * @param string $backgroundColor
+ * @param integer $imageQuality
+ * @param boolean $interlace
+ */
+ public function save($folder, $imageName, $createFolders = true, $backgroundColor = null, $imageQuality = 75, $interlace = false)
+ {
+ if (!is_file($folder)) {
+
+ if (is_dir($folder) || $createFolders) {
+
+ // Creating the folders if they don't exist
+ if (!is_dir($folder) && $createFolders) {
+ $oldUmask = umask(0);
+ mkdir($folder, 0777, true);
+ umask($oldUmask);
+ chmod($folder, 0777);
+ }
+
+ $extension = explode('.', $imageName);
+ $extension = strtolower($extension[count($extension) - 1]);
+
+ $filename = $folder.'/'.$imageName;
+
+ if (($extension == 'jpg' || $extension == 'jpeg' || $extension == 'gif') && (!$backgroundColor || $backgroundColor == 'transparent')) {
+ $backgroundColor = 'ffffff';
+ }
+
+ $image = $this->getResult($backgroundColor);
+
+ imageinterlace($image, (int) $interlace);
+
+ if ($extension == 'jpg' || $extension == 'jpeg') {
+
+ imagejpeg($image, $filename, $imageQuality);
+ unset($image);
+
+ } elseif ($extension == 'gif') {
+
+ imagegif($image, $filename);
+ unset($image);
+
+ } elseif ($extension == 'png') {
+
+ $imageQuality = $imageQuality / 10;
+ $imageQuality -= 1;
+
+ imagepng($image, $filename, $imageQuality);
+ unset($image);
+ }
+ }
+ }
+ }
+
+ // Checkers
+ // =========================================================
+
+ /**
+ * Check if a sublayer exists in the stack for a given id
+ *
+ * @param integer $layerId
+ *
+ * @return boolean
+ */
+ public function isLayerInIndex($layerId)
+ {
+ if (array_key_exists($layerId, $this->layers)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ // Getter / Setter
+ // =========================================================
+
+ /**
+ * Return the narrow side width of the layer
+ *
+ * @return integer
+ */
+ public function getNarrowSideWidth()
+ {
+ $narrowSideWidth = $this->getWidth();
+
+ if ($this->getHeight() < $narrowSideWidth) {
+ $narrowSideWidth = $this->getHeight();
+ }
+
+ return $narrowSideWidth;
+ }
+
+ /**
+ * Return the largest side width of the layer
+ *
+ * @return integer
+ */
+ public function getLargestSideWidth()
+ {
+ $largestSideWidth = $this->getWidth();
+
+ if ($this->getHeight() > $largestSideWidth) {
+ $largestSideWidth = $this->getHeight();
+ }
+
+ return $largestSideWidth;
+ }
+
+ /**
+ * Get the level of a sublayer
+ * Return sublayer level if success or false if layer isn't found
+ *
+ * @param integer $layerId
+ *
+ * @return mixed (integer or boolean)
+ */
+ public function getLayerLevel($layerId)
+ {
+ if ($this->isLayerInIndex($layerId)) { // if the layer exists in document
+ return array_search($layerId, $this->layerLevels);
+ }
+
+ return false;
+ }
+
+ /**
+ * Get a sublayer in the stack
+ * Don't forget to use clone method: $b = clone $a->getLayer(3);
+ *
+ * @param integer $layerId
+ *
+ * @return ImageWorkshop
+ */
+ public function getLayer($layerId)
+ {
+ return $this->layers[$layerId];
+ }
+
+ /**
+ * Getter width
+ *
+ * @return integer
+ */
+ public function getWidth()
+ {
+ return $this->width;
+ }
+
+ /**
+ * Getter height
+ *
+ * @return integer
+ */
+ public function getHeight()
+ {
+ return $this->height;
+ }
+
+ /**
+ * Getter image
+ *
+ * @return resource
+ */
+ public function getImage()
+ {
+ return $this->image;
+ }
+
+ /**
+ * Getter layers
+ *
+ * @return array
+ */
+ public function getLayers()
+ {
+ return $this->layers;
+ }
+
+ /**
+ * Getter layerLevels
+ *
+ * @return array
+ */
+ public function getLayerLevels()
+ {
+ return $this->layerLevels;
+ }
+
+ /**
+ * Getter layerPositions
+ *
+ * Get all the positions of the sublayers,
+ * or when specifying $layerId, get the position of this sublayer
+ *
+ * @param integer $layerId
+ *
+ * @return mixed (array or boolean)
+ */
+ public function getLayerPositions($layerId = null)
+ {
+ if (!$layerId) {
+
+ return $this->layerPositions;
+
+ } elseif ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack
+
+ return $this->layerPositions[$layerId];
+ }
+
+ return false;
+ }
+
+ /**
+ * Getter highestLayerLevel
+ *
+ * @return array
+ */
+ public function getHighestLayerLevel()
+ {
+ return $this->highestLayerLevel;
+ }
+
+ /**
+ * Getter lastLayerId
+ *
+ * @return array
+ */
+ public function getLastLayerId()
+ {
+ return $this->lastLayerId;
+ }
+
+ // Internals
+ // =========================================================
+
+ /**
+ * Delete the current object
+ */
+ public function delete()
+ {
+ imagedestroy($this->image);
+ $this->clearStack();
+ }
+
+ /**
+ * Create a new background image var from the old background image var
+ */
+ public function createNewVarFromBackgroundImage()
+ {
+ $virginImage = ImageWorkshopLib::generateImage($this->getWidth(), $this->getHeight()); // New background image
+
+ ImageWorkshopLib::mergeTwoImages($virginImage, $this->image, 0, 0, 0, 0);
+ unset($this->image);
+
+ $this->image = $virginImage;
+ unset($virginImage);
+
+ $layers = $this->layers;
+
+ foreach($layers as $layerId => $layer) {
+ $this->layers[$layerId] = clone $this->layers[$layerId];
+ }
+ }
+
+ /**
+ * Index a sublayer in the layer stack
+ * Return an array containing the generated sublayer id and its final level:
+ * array("layerLevel" => integer, "id" => integer)
+ *
+ * @param integer $layerLevel
+ * @param ImageWorkshopLayer $layer
+ * @param integer $positionX
+ * @param integer $positionY
+ * @param string $position
+ *
+ * @return array
+ */
+ protected function indexLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $position)
+ {
+ // Choose an id for the added layer
+ $layerId = $this->lastLayerId + 1;
+
+ // Clone $layer to duplicate image resource var
+ $layer = clone $layer;
+
+ // Add the layer in the stack
+ $this->layers[$layerId] = $layer;
+
+ // Add the layer positions in the main layer
+ $this->layerPositions[$layerId] = ImageWorkshopLib::calculatePositions($this->getWidth(), $this->getHeight(), $layer->getWidth(), $layer->getHeight(), $positionX, $positionY, $position);
+
+ // Update the lastLayerId of the workshop
+ $this->lastLayerId = $layerId;
+
+ // Add the layer level in the stack
+ $layerLevel = $this->indexLevelInDocument($layerLevel, $layerId);
+
+ return array(
+ 'layerLevel' => $layerLevel,
+ 'id' => $layerId,
+ );
+ }
+
+ /**
+ * Index a layer level and update the layers levels in the document
+ * Return the corrected level of the layer
+ *
+ * @param integer $layerLevel
+ * @param integer $layerId
+ *
+ * @return integer
+ */
+ protected function indexLevelInDocument($layerLevel, $layerId)
+ {
+ if (array_key_exists($layerLevel, $this->layerLevels)) { // Level already exists
+
+ ksort($this->layerLevels); // All layers after this level and the layer which have this level are updated
+ $layerLevelsTmp = $this->layerLevels;
+
+ foreach ($layerLevelsTmp as $levelTmp => $layerIdTmp) {
+ if ($levelTmp >= $layerLevel) {
+ $this->layerLevels[$levelTmp + 1] = $layerIdTmp;
+ }
+ }
+
+ unset($layerLevelsTmp);
+
+ } else { // Level isn't taken
+ if ($this->highestLayerLevel < $layerLevel) { // If given level is too high, proceed adjustement
+ $layerLevel = $this->highestLayerLevel + 1;
+ }
+ }
+
+ $this->layerLevels[$layerLevel] = $layerId;
+ $this->highestLayerLevel = max(array_flip($this->layerLevels)); // Update $highestLayerLevel
+
+ return $layerLevel;
+ }
+
+ /**
+ * Update the positions of layers in the stack after cropping
+ *
+ * @param integer $positionX
+ * @param integer $positionY
+ */
+ public function updateLayerPositionsAfterCropping($positionX, $positionY)
+ {
+ foreach ($this->layers as $layerId => $layer) {
+
+ $oldLayerPosX = $this->layerPositions[$layerId]['x'];
+ $oldLayerPosY = $this->layerPositions[$layerId]['y'];
+
+ $newLayerPosX = $oldLayerPosX + $positionX;
+ $newLayerPosY = $oldLayerPosY + $positionY;
+
+ $this->changePosition($layerId, $newLayerPosX, $newLayerPosY);
+ }
+ }
+
+ /**
+ * Resize the background of a layer
+ *
+ * @param integer $newWidth
+ * @param integer $newHeight
+ */
+ public function resizeBackground($newWidth, $newHeight)
+ {
+ $oldWidth = $this->width;
+ $oldHeight = $this->height;
+
+ $this->width = $newWidth;
+ $this->height = $newHeight;
+
+ $virginLayoutImage = ImageWorkshopLib::generateImage($this->width, $this->height);
+
+ imagecopyresampled($virginLayoutImage, $this->image, 0, 0, 0, 0, $this->width, $this->height, $oldWidth, $oldHeight);
+
+ unset($this->image);
+ $this->image = $virginLayoutImage;
+ }
+
+ // Deprecated, don't use anymore
+ // =========================================================
+
+ /**
+ * @deprecated
+ */
+ public function resizeInPourcent($percentWidth = null, $percentHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
+ {
+ throw new ImageWorkshopLayerException('Method resizeInPourcent() was renamed resizeInPercent(). Use resizeInPercent() instead.', static::METHOD_DEPRECATED);
+ }
+
+ /**
+ * @deprecated
+ */
+ public function resizeByLargestSideInPourcent($newLargestSideWidth, $converseProportion = false)
+ {
+ throw new ImageWorkshopLayerException('Method resizeByLargestSideInPourcent() was renamed resizeByLargestSideInPercent(). Use resizeByLargestSideInPercent() instead.', static::METHOD_DEPRECATED);
+ }
+
+ /**
+ * @deprecated
+ */
+ public function resizeByNarrowSideInPourcent($newNarrowSideWidth, $converseProportion = false)
+ {
+ throw new ImageWorkshopLayerException('Method resizeByNarrowSideInPourcent() was renamed resizeByNarrowSideInPercent(). Use resizeByNarrowSideInPercent() instead.', static::METHOD_DEPRECATED);
+ }
+
+ /**
+ * @deprecated
+ */
+ public function cropInPourcent($percentWidth = 0, $percentHeight = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
+ {
+ throw new ImageWorkshopLayerException('Method cropInPourcent() was renamed cropInPercent(). Use cropInPercent() instead.', static::METHOD_DEPRECATED);
+ }
+
+ /**
+ * @deprecated
+ */
+ public function cropMaximumInPourcent($positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
+ {
+ throw new ImageWorkshopLayerException('Method cropMaximumInPourcent() was renamed cropMaximumInPercent(). Use cropMaximumInPercent() instead.', static::METHOD_DEPRECATED);
+ }
+}
diff --git a/PHPImageWorkshop/Core/ImageWorkshopLib.php b/PHPImageWorkshop/Core/ImageWorkshopLib.php
index ed928ee..c650218 100644
--- a/PHPImageWorkshop/Core/ImageWorkshopLib.php
+++ b/PHPImageWorkshop/Core/ImageWorkshopLib.php
@@ -1,299 +1,299 @@
- $layerPositionX,
- 'y' => $layerPositionY,
- );
- }
-
- /**
- * Convert Hex color to RGB color format
- *
- * @param string $hex
- *
- * @return array
- */
- public static function convertHexToRGB($hex)
- {
- return array(
- 'R' => (int) base_convert(substr($hex, 0, 2), 16, 10),
- 'G' => (int) base_convert(substr($hex, 2, 2), 16, 10),
- 'B' => (int) base_convert(substr($hex, 4, 2), 16, 10),
- );
- }
-
- /**
- * Generate a new image resource var
- *
- * @param integer $width
- * @param integer $height
- * @param string $color
- * @param integer $opacity
- *
- * @return resource
- */
- public static function generateImage($width = 100, $height = 100, $color = 'ffffff', $opacity = 127)
- {
- $RGBColors = ImageWorkshopLib::convertHexToRGB($color);
-
- $image = imagecreatetruecolor($width, $height);
- imagesavealpha($image, true);
- $color = imagecolorallocatealpha($image, $RGBColors['R'], $RGBColors['G'], $RGBColors['B'], $opacity);
- imagefill($image, 0, 0, $color);
-
- return $image;
- }
-
- /**
- * Return dimension of a text
- *
- * @param $fontSize
- * @param $fontAngle
- * @param $fontFile
- * @param $text
- *
- * @return array or boolean
- */
- public static function getTextBoxDimension($fontSize, $fontAngle, $fontFile, $text)
- {
- if (!file_exists($fontFile)) {
- throw new ImageWorkshopLibException('Can\'t find a font file at this path : "'.$fontFile.'".', static::ERROR_FONT_NOT_FOUND);
- }
-
- $box = imagettfbbox($fontSize, $fontAngle, $fontFile, $text);
-
- if (!$box) {
-
- return false;
- }
-
- $minX = min(array($box[0], $box[2], $box[4], $box[6]));
- $maxX = max(array($box[0], $box[2], $box[4], $box[6]));
- $minY = min(array($box[1], $box[3], $box[5], $box[7]));
- $maxY = max(array($box[1], $box[3], $box[5], $box[7]));
- $width = ($maxX - $minX);
- $height = ($maxY - $minY);
- $left = abs($minX) + $width;
- $top = abs($minY) + $height;
-
- // to calculate the exact bounding box, we write the text in a large image
- $img = @imagecreatetruecolor($width << 2, $height << 2);
- $white = imagecolorallocate($img, 255, 255, 255);
- $black = imagecolorallocate($img, 0, 0, 0);
- imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), $black);
-
- // for ensure that the text is completely in the image
- imagettftext($img, $fontSize, $fontAngle, $left, $top, $white, $fontFile, $text);
-
- // start scanning (0=> black => empty)
- $rleft = $w4 = $width<<2;
- $rright = 0;
- $rbottom = 0;
- $rtop = $h4 = $height<<2;
-
- for ($x = 0; $x < $w4; $x++) {
-
- for ($y = 0; $y < $h4; $y++) {
-
- if (imagecolorat($img, $x, $y)) {
-
- $rleft = min($rleft, $x);
- $rright = max($rright, $x);
- $rtop = min($rtop, $y);
- $rbottom = max($rbottom, $y);
- }
- }
- }
-
- imagedestroy($img);
-
- return array(
- 'left' => $left - $rleft,
- 'top' => $top - $rtop,
- 'width' => $rright - $rleft + 1,
- 'height' => $rbottom - $rtop + 1,
- );
- }
-
- /**
- * Copy an image on another one and converse transparency
- *
- * @param resource $destImg
- * @param resource $srcImg
- * @param integer $destX
- * @param integer $destY
- * @param integer $srcX
- * @param integer $srcY
- * @param integer $srcW
- * @param integer $srcH
- * @param integer $pct
- */
- public static function imageCopyMergeAlpha(&$destImg, &$srcImg, $destX, $destY, $srcX, $srcY, $srcW, $srcH, $pct = 0)
- {
- $destX = (int) $destX;
- $destY = (int) $destY;
- $srcX = (int) $srcX;
- $srcY = (int) $srcY;
- $srcW = (int) $srcW;
- $srcH = (int) $srcH;
- $pct = (int) $pct;
- $destW = imageSX($destImg);
- $destH = imageSY($destImg);
-
- for ($y = 0; $y < $srcH + $srcY; $y++) {
-
- for ($x = 0; $x < $srcW + $srcX; $x++) {
-
- if ($x + $destX >= 0 && $x + $destX < $destW && $x + $srcX >= 0 && $x + $srcX < $srcW && $y + $destY >= 0 && $y + $destY < $destH && $y + $srcY >= 0 && $y + $srcY < $srcH) {
-
- $destPixel = imageColorsForIndex($destImg, imageColorat($destImg, $x + $destX, $y + $destY));
- $srcImgColorat = imageColorat($srcImg, $x + $srcX, $y + $srcY);
-
- if ($srcImgColorat >= 0) {
-
- $srcPixel = imageColorsForIndex($srcImg, $srcImgColorat);
-
- $srcAlpha = 1 - ($srcPixel['alpha'] / 127);
- $destAlpha = 1 - ($destPixel['alpha'] / 127);
- $opacity = $srcAlpha * $pct / 100;
-
- if ($destAlpha >= $opacity) {
- $alpha = $destAlpha;
- }
-
- if ($destAlpha < $opacity) {
- $alpha = $opacity;
- }
-
- if ($alpha > 1) {
- $alpha = 1;
- }
-
- if ($opacity > 0) {
-
- $destRed = round((($destPixel['red'] * $destAlpha * (1 - $opacity))));
- $destGreen = round((($destPixel['green'] * $destAlpha * (1 - $opacity))));
- $destBlue = round((($destPixel['blue'] * $destAlpha * (1 - $opacity))));
- $srcRed = round((($srcPixel['red'] * $opacity)));
- $srcGreen = round((($srcPixel['green'] * $opacity)));
- $srcBlue = round((($srcPixel['blue'] * $opacity)));
- $red = round(($destRed + $srcRed ) / ($destAlpha * (1 - $opacity) + $opacity));
- $green = round(($destGreen + $srcGreen) / ($destAlpha * (1 - $opacity) + $opacity));
- $blue = round(($destBlue + $srcBlue ) / ($destAlpha * (1 - $opacity) + $opacity));
-
- if ($red > 255) {
- $red = 255;
- }
-
- if ($green > 255) {
- $green = 255;
- }
-
- if ($blue > 255) {
- $blue = 255;
- }
-
- $alpha = round((1 - $alpha) * 127);
- $color = imageColorAllocateAlpha($destImg, $red, $green, $blue, $alpha);
- imageSetPixel($destImg, $x + $destX, $y + $destY, $color);
- }
- }
- }
- }
- }
- }
-
- /**
- * Merge two image var
- *
- * @param resource $destinationImage
- * @param resource $sourceImage
- * @param integer $destinationPosX
- * @param integer $destinationPosY
- * @param integer $sourcePosX
- * @param integer $sourcePosY
- */
- public static function mergeTwoImages(&$destinationImage, $sourceImage, $destinationPosX = 0, $destinationPosY = 0, $sourcePosX = 0, $sourcePosY = 0)
- {
- imageCopy($destinationImage, $sourceImage, $destinationPosX, $destinationPosY, $sourcePosX, $sourcePosY, imageSX($sourceImage), imageSY($sourceImage));
- }
+ $layerPositionX,
+ 'y' => $layerPositionY,
+ );
+ }
+
+ /**
+ * Convert Hex color to RGB color format
+ *
+ * @param string $hex
+ *
+ * @return array
+ */
+ public static function convertHexToRGB($hex)
+ {
+ return array(
+ 'R' => (int) base_convert(substr($hex, 0, 2), 16, 10),
+ 'G' => (int) base_convert(substr($hex, 2, 2), 16, 10),
+ 'B' => (int) base_convert(substr($hex, 4, 2), 16, 10),
+ );
+ }
+
+ /**
+ * Generate a new image resource var
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param string $color
+ * @param integer $opacity
+ *
+ * @return resource
+ */
+ public static function generateImage($width = 100, $height = 100, $color = 'ffffff', $opacity = 127)
+ {
+ $RGBColors = ImageWorkshopLib::convertHexToRGB($color);
+
+ $image = imagecreatetruecolor($width, $height);
+ imagesavealpha($image, true);
+ $color = imagecolorallocatealpha($image, $RGBColors['R'], $RGBColors['G'], $RGBColors['B'], $opacity);
+ imagefill($image, 0, 0, $color);
+
+ return $image;
+ }
+
+ /**
+ * Return dimension of a text
+ *
+ * @param $fontSize
+ * @param $fontAngle
+ * @param $fontFile
+ * @param $text
+ *
+ * @return array or boolean
+ */
+ public static function getTextBoxDimension($fontSize, $fontAngle, $fontFile, $text)
+ {
+ if (!file_exists($fontFile)) {
+ throw new ImageWorkshopLibException('Can\'t find a font file at this path : "'.$fontFile.'".', static::ERROR_FONT_NOT_FOUND);
+ }
+
+ $box = imagettfbbox($fontSize, $fontAngle, $fontFile, $text);
+
+ if (!$box) {
+
+ return false;
+ }
+
+ $minX = min(array($box[0], $box[2], $box[4], $box[6]));
+ $maxX = max(array($box[0], $box[2], $box[4], $box[6]));
+ $minY = min(array($box[1], $box[3], $box[5], $box[7]));
+ $maxY = max(array($box[1], $box[3], $box[5], $box[7]));
+ $width = ($maxX - $minX);
+ $height = ($maxY - $minY);
+ $left = abs($minX) + $width;
+ $top = abs($minY) + $height;
+
+ // to calculate the exact bounding box, we write the text in a large image
+ $img = @imagecreatetruecolor($width << 2, $height << 2);
+ $white = imagecolorallocate($img, 255, 255, 255);
+ $black = imagecolorallocate($img, 0, 0, 0);
+ imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), $black);
+
+ // for ensure that the text is completely in the image
+ imagettftext($img, $fontSize, $fontAngle, $left, $top, $white, $fontFile, $text);
+
+ // start scanning (0=> black => empty)
+ $rleft = $w4 = $width<<2;
+ $rright = 0;
+ $rbottom = 0;
+ $rtop = $h4 = $height<<2;
+
+ for ($x = 0; $x < $w4; $x++) {
+
+ for ($y = 0; $y < $h4; $y++) {
+
+ if (imagecolorat($img, $x, $y)) {
+
+ $rleft = min($rleft, $x);
+ $rright = max($rright, $x);
+ $rtop = min($rtop, $y);
+ $rbottom = max($rbottom, $y);
+ }
+ }
+ }
+
+ imagedestroy($img);
+
+ return array(
+ 'left' => $left - $rleft,
+ 'top' => $top - $rtop,
+ 'width' => $rright - $rleft + 1,
+ 'height' => $rbottom - $rtop + 1,
+ );
+ }
+
+ /**
+ * Copy an image on another one and converse transparency
+ *
+ * @param resource $destImg
+ * @param resource $srcImg
+ * @param integer $destX
+ * @param integer $destY
+ * @param integer $srcX
+ * @param integer $srcY
+ * @param integer $srcW
+ * @param integer $srcH
+ * @param integer $pct
+ */
+ public static function imageCopyMergeAlpha(&$destImg, &$srcImg, $destX, $destY, $srcX, $srcY, $srcW, $srcH, $pct = 0)
+ {
+ $destX = (int) $destX;
+ $destY = (int) $destY;
+ $srcX = (int) $srcX;
+ $srcY = (int) $srcY;
+ $srcW = (int) $srcW;
+ $srcH = (int) $srcH;
+ $pct = (int) $pct;
+ $destW = imageSX($destImg);
+ $destH = imageSY($destImg);
+
+ for ($y = 0; $y < $srcH + $srcY; $y++) {
+
+ for ($x = 0; $x < $srcW + $srcX; $x++) {
+
+ if ($x + $destX >= 0 && $x + $destX < $destW && $x + $srcX >= 0 && $x + $srcX < $srcW && $y + $destY >= 0 && $y + $destY < $destH && $y + $srcY >= 0 && $y + $srcY < $srcH) {
+
+ $destPixel = imageColorsForIndex($destImg, imageColorat($destImg, $x + $destX, $y + $destY));
+ $srcImgColorat = imageColorat($srcImg, $x + $srcX, $y + $srcY);
+
+ if ($srcImgColorat >= 0) {
+
+ $srcPixel = imageColorsForIndex($srcImg, $srcImgColorat);
+
+ $srcAlpha = 1 - ($srcPixel['alpha'] / 127);
+ $destAlpha = 1 - ($destPixel['alpha'] / 127);
+ $opacity = $srcAlpha * $pct / 100;
+
+ if ($destAlpha >= $opacity) {
+ $alpha = $destAlpha;
+ }
+
+ if ($destAlpha < $opacity) {
+ $alpha = $opacity;
+ }
+
+ if ($alpha > 1) {
+ $alpha = 1;
+ }
+
+ if ($opacity > 0) {
+
+ $destRed = round((($destPixel['red'] * $destAlpha * (1 - $opacity))));
+ $destGreen = round((($destPixel['green'] * $destAlpha * (1 - $opacity))));
+ $destBlue = round((($destPixel['blue'] * $destAlpha * (1 - $opacity))));
+ $srcRed = round((($srcPixel['red'] * $opacity)));
+ $srcGreen = round((($srcPixel['green'] * $opacity)));
+ $srcBlue = round((($srcPixel['blue'] * $opacity)));
+ $red = round(($destRed + $srcRed ) / ($destAlpha * (1 - $opacity) + $opacity));
+ $green = round(($destGreen + $srcGreen) / ($destAlpha * (1 - $opacity) + $opacity));
+ $blue = round(($destBlue + $srcBlue ) / ($destAlpha * (1 - $opacity) + $opacity));
+
+ if ($red > 255) {
+ $red = 255;
+ }
+
+ if ($green > 255) {
+ $green = 255;
+ }
+
+ if ($blue > 255) {
+ $blue = 255;
+ }
+
+ $alpha = round((1 - $alpha) * 127);
+ $color = imageColorAllocateAlpha($destImg, $red, $green, $blue, $alpha);
+ imageSetPixel($destImg, $x + $destX, $y + $destY, $color);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Merge two image var
+ *
+ * @param resource $destinationImage
+ * @param resource $sourceImage
+ * @param integer $destinationPosX
+ * @param integer $destinationPosY
+ * @param integer $sourcePosX
+ * @param integer $sourcePosY
+ */
+ public static function mergeTwoImages(&$destinationImage, $sourceImage, $destinationPosX = 0, $destinationPosY = 0, $sourcePosX = 0, $sourcePosY = 0)
+ {
+ imageCopy($destinationImage, $sourceImage, $destinationPosX, $destinationPosY, $sourcePosX, $sourcePosY, imageSX($sourceImage), imageSY($sourceImage));
+ }
}
\ No newline at end of file
diff --git a/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php b/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php
index 81ed653..e1925f5 100644
--- a/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php
+++ b/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php
@@ -1,38 +1,38 @@
-code}]: {$this->message}\n";
- }
+code}]: {$this->message}\n";
+ }
}
\ No newline at end of file
diff --git a/PHPImageWorkshop/Exception/ImageWorkshopException.php b/PHPImageWorkshop/Exception/ImageWorkshopException.php
index c2efa97..7663c93 100644
--- a/PHPImageWorkshop/Exception/ImageWorkshopException.php
+++ b/PHPImageWorkshop/Exception/ImageWorkshopException.php
@@ -1,22 +1,22 @@
-write($text, $fontPath, $fontSize, $fontColor, $textDimensions['left'], $textDimensions['top'], $textRotation);
-
- return $layer;
- }
-
- /**
- * Initialize a new virgin layer
- *
- * @param integer $width
- * @param integer $height
- * @param string $backgroundColor
- *
- * @return ImageWorkshopLayer
- */
- public static function initVirginLayer($width = 100, $height = 100, $backgroundColor = null)
- {
- $opacity = 0;
-
- if (!$backgroundColor || $backgroundColor == 'transparent') {
- $opacity = 127;
- $backgroundColor = 'ffffff';
- }
-
- return new ImageWorkshopLayer(ImageWorkshopLib::generateImage($width, $height, $backgroundColor, $opacity));
- }
-
- /**
- * Initialize a layer from a resource image var
- *
- * @param \resource $image
- *
- * @return ImageWorkshopLayer
- */
- public static function initFromResourceVar($image)
- {
- return new ImageWorkshopLayer($image);
- }
-
- /**
- * Initialize a layer from a string (obtains with file_get_contents, cURL...)
- *
- * This not recommanded to initialize JPEG string with this method, GD displays bugs !
- *
- * @param string $imageString
- *
- * @return ImageWorkshopLayer
- */
- public static function initFromString($imageString)
- {
- if (!$image = @imageCreateFromString($imageString)) {
- throw new ImageWorkshopException('Can\'t generate an image from the given string.', static::ERROR_CREATE_IMAGE_FROM_STRING);
- }
-
- return new ImageWorkshopLayer($image);
- }
+write($text, $fontPath, $fontSize, $fontColor, $textDimensions['left'], $textDimensions['top'], $textRotation);
+
+ return $layer;
+ }
+
+ /**
+ * Initialize a new virgin layer
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param string $backgroundColor
+ *
+ * @return ImageWorkshopLayer
+ */
+ public static function initVirginLayer($width = 100, $height = 100, $backgroundColor = null)
+ {
+ $opacity = 0;
+
+ if (!$backgroundColor || $backgroundColor == 'transparent') {
+ $opacity = 127;
+ $backgroundColor = 'ffffff';
+ }
+
+ return new ImageWorkshopLayer(ImageWorkshopLib::generateImage($width, $height, $backgroundColor, $opacity));
+ }
+
+ /**
+ * Initialize a layer from a resource image var
+ *
+ * @param \resource $image
+ *
+ * @return ImageWorkshopLayer
+ */
+ public static function initFromResourceVar($image)
+ {
+ return new ImageWorkshopLayer($image);
+ }
+
+ /**
+ * Initialize a layer from a string (obtains with file_get_contents, cURL...)
+ *
+ * This not recommanded to initialize JPEG string with this method, GD displays bugs !
+ *
+ * @param string $imageString
+ *
+ * @return ImageWorkshopLayer
+ */
+ public static function initFromString($imageString)
+ {
+ if (!$image = @imageCreateFromString($imageString)) {
+ throw new ImageWorkshopException('Can\'t generate an image from the given string.', static::ERROR_CREATE_IMAGE_FROM_STRING);
+ }
+
+ return new ImageWorkshopLayer($image);
+ }
}
\ No newline at end of file
diff --git a/config.inc.php b/config.inc.php
index 0251c1a..b6284cc 100644
--- a/config.inc.php
+++ b/config.inc.php
@@ -1,8 +1,10 @@
-get('/photos/thumbnail/:imageID(/:createImgTag)', function ($imageID, $createImgTag = 0) use ($app) {
- $mysqlconnetion = new MysqlClass;
-
- $retObj = $mysqlconnetion->queryToObject("select type_format, image_thumbnail from immagini where id=" . $imageID, false);
- $mysqlconnetion->disconnetti();
-
- if($createImgTag)
- echo '
';
- else
- {
- $app->contentType($retObj["type_format"]);
- echo $retObj['image_thumbnail'];
- }
-});
-
-$app->get('/photos/:imageID(/:createImgTag)', function ($imageID, $createImgTag = 0) use ($app) {
- $mysqlconnetion = new MysqlClass;
-
- $retObj = $mysqlconnetion->queryToObject("select type_format, image from immagini where id=" . $imageID, false);
- $mysqlconnetion->disconnetti();
-
- if($createImgTag)
- echo '
';
-});
-
-$app->put('/photos/publish/:imageID', function ($imageID) use ($app) {
- $mysqlconnetion = new MysqlClass;
-
- $query = "update immagini set published = 1, published_date = NOW() where ProfiloID = " . $imageID;
- $mysqlconnetion->disconnetti();
- $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-});
-
-$app->post('/photos', function () use ($app) {
- $idRicette = $app->request()->post('ricetta_id');
- $profileID = $app->request()->post('keyStore');
- $imageFileName = $_FILES['image']["tmp_name"];
-
- $layer = ImageWorkshop::initFromPath($imageFileName);
- $layer->resizeByLargestSideInPixel(640, true);
-
- $layer->save(dirname($imageFileName), basename($imageFileName));
-
- $imgData = addslashes(file_get_contents($imageFileName));
-
- $layer->resizeByLargestSideInPixel(300, true);
-
- $layer->save(dirname($imageFileName), basename($imageFileName));
-
- $ThumbImageData = addslashes(file_get_contents($imageFileName));
-
-// istanza della classe
- $mysqlconnetion = new MysqlClass;
-//$mysqlconneti on->connetti();
- $query = "insert into immagini(id_ricette, type_format, image_thumbnail, image, from_profile_id, uploaded_date) " .
- "values(" . $idRicette . ", '" . image_type_to_mime_type($image->image_type) .
- "', '" . $ThumbImageData . "', '" . $imgData . "', '" . $profileID . "', NOW())";
- $newID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- echo $newID;
-});
-
-$app->put('/photos/:imageID', function ($imageID) use ($app) {
- $imageFileName = $_FILES['image']["tmp_name"];
-
- $layer = ImageWorkshop::initFromPath($imageFileName);
- $layer->resizeByLargestSideInPixel(640, true);
-
- $layer->save(dirname($imageFileName), basename($imageFileName));
-
- $imgData = addslashes(file_get_contents($imageFileName));
-
- $layer->resizeByLargestSideInPixel(300, true);
-
- $layer->save(dirname($imageFileName), basename($imageFileName));
-
- $ThumbImageData = addslashes(file_get_contents($imageFileName));
-
-// istanza della classe
- $mysqlconnetion = new MysqlClass;
-//$mysqlconneti on->connetti();
- $query = "update immagini set (type_format = '" . image_type_to_mime_type($image->image_type) . "', " .
- "image_thumbnail = '" . $ThumbImageData . "', " .
- "image = '" . $imgData . "' where id=" . $imageID;
-
- $newID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- return $newID;
-});
+get('/photos/thumbnail/:imageID(/:createImgTag)', function ($imageID, $createImgTag = 0) use ($app, $dirRicetteDropBox) {
+ $mysqlconnetion = new MysqlClass;
+
+ $retObj = $mysqlconnetion->queryToObject("select type_format, id_ricette from immagini where id=" . $imageID, false);
+ $mysqlconnetion->disconnetti();
+
+ $ext = "";
+ if ($retObj["type_format"] == "image/jpeg") {
+ $ext = "jpg";
+ } else if ($retObj["type_format"] == "image/png") {
+ $ext = "png";
+ }
+
+ $folder = $dirRicetteDropBox . "/" . $retObj["id_ricette"];
+
+ $imageFileName = $imageID . "_thumb_ricetta." . $ext;
+
+ $dropBoxObj = new myDropBox();
+
+ if ($createImgTag) {
+ echo '
';
+ }
+});
+
+$app->get('/photos/:imageID(/:createImgTag)', function ($imageID, $createImgTag = 0) use ($app, $dirRicetteDropBox) {
+ $mysqlconnetion = new MysqlClass;
+
+ $retObj = $mysqlconnetion->queryToObject("select type_format, id_ricette from immagini where id=" . $imageID, false);
+ $mysqlconnetion->disconnetti();
+ $ext = "";
+ if ($retObj["type_format"] == "image/jpeg") {
+ $ext = "jpg";
+ } else if ($retObj["type_format"] == "image/png") {
+ $ext = "png";
+ }
+
+ $folder = $dirRicetteDropBox . "/" . $retObj["id_ricette"];
+
+ $imageFileName = $imageID . "_full_ricetta." . $ext;
+
+ $dropBoxObj = new myDropBox();
+
+ if ($createImgTag) {
+ echo '
';
+ }
+});
+
+$app->put('/photos/publish/:imageID', function ($imageID) use ($app) {
+ $mysqlconnetion = new MysqlClass;
+
+ $query = "update immagini set published = 1, published_date = NOW() where id = " . $imageID;
+
+ $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+});
+
+$app->post('/photos', function () use ($app, $dirRicetteDropBox) {
+ $idRicette = $app->request()->post('ricetta_id');
+ $profileID = $app->request()->post('keyStore');
+ $tmpFileName = $_FILES['image']["tmp_name"];
+ $layer = ImageWorkshop::initFromPath($tmpFileName);
+ // istanza della classe
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconneti on->connetti();
+ $query = "insert into immagini(id_ricette, type_format, from_profile_id, uploaded_date) " .
+ "values(" . $idRicette . ", '" . image_type_to_mime_type(exif_imagetype($tmpFileName)) .
+ "', '" . $profileID . "', NOW())";
+ $newID = $mysqlconnetion->insertRecord($query);
+ $ext = image_type_to_extension(exif_imagetype($tmpFileName), FALSE);
+
+ $imageFileName = $newID . "_full_ricetta." . $ext;
+
+ $thumbFileName = $newID . "_thumb_ricetta." . $ext;
+
+ $layer->resizeByLargestSideInPixel(640, true);
+
+ $layer->save(dirname($tmpFileName), $imageFileName);
+
+ $layer->resizeByLargestSideInPixel(300, true);
+
+ $layer->save(dirname($tmpFileName), $thumbFileName);
+
+ $dropBoxObj = new myDropBox();
+
+ $folder = $dirRicetteDropBox . "/" . $idRicette;
+
+ try {
+ $dropBoxObj->CreateFolder($folder);
+ } catch (DropboxException $ex) {
+
+ }
+ $fullPath = dirname($tmpFileName) . "/" . $imageFileName;
+ echo $fullPath . "\n";
+ $thumbPath = dirname($tmpFileName) . "/" . $thumbFileName;
+ echo $thumbPath . "\n";
+ $dropBoxObj->UploadFile($fullPath, $folder . "/" . $imageFileName);
+
+ $dropBoxObj->UploadFile($thumbPath, $folder . "/" . $thumbFileName);
+
+ $mysqlconnetion->disconnetti();
+
+ echo $newID;
+});
+
+$app->put('/photos/:imageID', function ($imageID) use ($app, $dirRicetteDropBox) {
+ $tmpFileName = $_FILES['image']["tmp_name"];
+ $layer = ImageWorkshop::initFromPath($tmpFileName);
+// istanza della classe
+ $mysqlconnetion = new MysqlClass;
+//$mysqlconneti on->connetti();
+ $query = "update immagini set (type_format = '" . image_type_to_mime_type(exif_imagetype($tmpFileName)) . "', " .
+ " where id=" . $imageID;
+
+ $newID = $mysqlconnetion->insertRecord($query);
+
+ $imageFileName = $imageID . "_full_ricetta";
+
+ $thumbFileName = $imageID . "_thumb_ricetta";
+
+ $layer->resizeByLargestSideInPixel(640, true);
+
+ $layer->save(dirname($tmpFileName), $imageFileName);
+
+ $layer->resizeByLargestSideInPixel(300, true);
+
+ $layer->save(dirname($tmpFileName), $thumbFileName);
+
+ $dropBoxObj = new myDropBox();
+
+ $folder = $dirRicetteDropBox . "/" . $idRicette;
+
+ $dropBoxObj->UploadFile(dirname($tmpFileName) . DIRECTORY_SEPARATOR . $imageFileName, $folder . "\\" . $imageFileName);
+
+ $dropBoxObj->UploadFile(dirname($tmpFileName) . DIRECTORY_SEPARATOR . $thumbFileName, $folder . "\\" . $thumbFileName);
+
+ $mysqlconnetion->disconnetti();
+
+ return $newID;
+});
diff --git a/include.php b/include.php
index b400eb4..aae1e0e 100644
--- a/include.php
+++ b/include.php
@@ -14,7 +14,10 @@ $app = new \Slim\Slim();
$app->hook('slim.before.router', function () use ($app, $allowedHost) {
$currentRefererRequest = $app->request()->getReferer();
- $currentRefererRequest = substr(substr($currentRefererRequest, 7), 0, strpos(substr($currentRefererRequest, 7), '/'));
+ $currentRefererRequest = substr($currentRefererRequest, 7); //Senza http://
+ $indexDoublePoint = strpos($currentRefererRequest, ':');
+ $indexFirstSlash = strpos($currentRefererRequest, '/');
+ $currentRefererRequest = substr($currentRefererRequest, 0, $indexDoublePoint > 0 && $indexDoublePoint < $indexFirstSlash ? $indexDoublePoint : $indexFirstSlash );
if(!in_array($currentRefererRequest, $allowedHost))
{
$app->halt(500, "Generic error occurred");
diff --git a/management.php b/management.php
index e4165ae..81a0767 100644
--- a/management.php
+++ b/management.php
@@ -1,165 +1,165 @@
-get('/categories', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- //$mysqlconneti on->connetti();
- $retObj = $mysqlconnetion->queryToObject("select ID as category_id, NAME as name from categorie order by name");
- $mysqlconnetion->disconnetti();
-
- returnJsonWithDecode($app, $callbackFn, $retObj);
-});
-
-$app->get('/typeingredients', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $retObj = $mysqlconnetion->queryToObject("select ID as tipo_ingredienti_id, name from tipo_ingredienti order by name");
- $mysqlconnetion->disconnetti();
-
- returnJsonWithDecode($app, $callbackFn, $retObj);
-});
-
-$app->get('/typeqtys', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $retObj = $mysqlconnetion->queryToObject("select ID as tipo_quantita_id, name from tipo_quantita");
- $mysqlconnetion->disconnetti();
-
- returnJsonWithDecode($app, $callbackFn, $retObj);
-});
-
-$app->get('/categoryitems/:catID', function ($categoryID) use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $query = "select ID as ricetta_id, titolo, autore, valutazione, difficolta from ricette where ID_CATEGORIA = " . $categoryID . " order by titolo, autore";
- $retObj = $mysqlconnetion->queryToObject($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retObj);
-});
-
-$app->get('/ricetta/body/:itemID', function ($itemID) use ($app) {
- $callbackFn = $app->request()->get('callback');
- // istanza della classe
- $mysqlconnetion = new MysqlClass;
- $query = "SELECT id_categoria, categorie.name AS categoria_name, ricette.ID AS ricetta_id, titolo, procedimento, autore, link_fonte, link_youtube, valutazione, difficolta FROM ricette INNER JOIN categorie ON categorie.ID = ricette.id_categoria WHERE ricette.ID = " . $itemID;
- $retObj = $mysqlconnetion->queryToObject($query);
-
- $query2 = "select ingredienti.id_tipo_ingredienti, tipo_ingredienti.name as nome_ingrediente, quantita, ingredienti.id_tipo_quantita, tipo_quantita.Name as unita, note, posizione from ingredienti " .
- "inner join tipo_ingredienti on ingredienti.ID_TIPO_INGREDIENTI = tipo_ingredienti.ID " .
- "left outer join tipo_quantita on ingredienti.ID_TIPO_QUANTITA = tipo_quantita.ID " .
- "where ingredienti.ID_RICETTE = " . $itemID . " order by ingredienti.posizione";
-
- $retObj2 = $mysqlconnetion->queryToObject($query2);
-
- $retObj[0]["titolo"] = html_entity_decode($retObj[0]["titolo"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
- $retObj[0]["procedimento"] = html_entity_decode($retObj[0]["procedimento"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
- $retObj[0]["ingredienti"] = $retObj2;
-
- $mysqlconnetion->disconnetti();
- returnJson($app, $callbackFn, $retObj);
-});
-
-$app->post('/typeingredients', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $json_data_body = json_decode($app->request()->post('bodydata'));
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $query = "insert into tipo_ingredienti(Name) values ('" . $json_data_body->name . "')";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retNewID);
-});
-
-$app->post('/ricetta/body', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $json_data_body = json_decode($app->request()->post('bodydata'));
- $retValue["result"] = true;
- $retValue["message"] = "";
- $mysqlconnetion = new MysqlClass;
- // istanza della classe
- try {
- $retNewID = 0;
- if ($json_data_body->ricettaID != "") {
- $query = "update ricette SET ID_CATEGORIA = " . $json_data_body->categoria_id .
- ", titolo = '" . htmlentities($json_data_body->titolo, null, "UTF-8") .
- "', procedimento = '" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) .
- "', autore = '" . str_replace("'", "''", $json_data_body->autore) .
- "', link_fonte = '" . $json_data_body->linkFonte .
- "', Link_youtube = '" . $json_data_body->linkVideo .
- "', Difficolta = '" . $json_data_body->difficolta .
- "' where ID = " . $json_data_body->ricettaID;
-
- $mysqlconnetion->executeQuery($query);
- $queryDelete = "DELETE FROM ingredienti where ID_RICETTE = " . $json_data_body->ricettaID;
- $mysqlconnetion->executeQuery($queryDelete);
- $retNewID = $json_data_body->ricettaID;
- } else {
-
- $query = "insert into ricette(ID_CATEGORIA, titolo, procedimento, autore, link_fonte, Link_youtube,Difficolta) values (" .
- $json_data_body->categoria_id . ",'" . htmlentities($json_data_body->titolo, null, "UTF-8") . "','" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) .
- "','" . str_replace("'", "''", $json_data_body->autore) . "','" .
- $json_data_body->linkFonte . "','" . $json_data_body->linkVideo . "', " . $json_data_body->difficolta . ")";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- }
-
- $pos = 0;
- foreach ($json_data_body->ingredienti as $arr) {
- $note = "";
- if ($arr->note != "") {
- $note = str_replace("'", "''", htmlentities($arr->note));
- }
-
- $query = "insert into ingredienti(ID_TIPO_INGREDIENTI, ID_RICETTE, Quantita, ID_TIPO_QUANTITA, Note, Posizione) values (" .
- $arr->ingrediente_id . "," . $retNewID . "," . ($arr->quantita == "" ? "0" : $arr->quantita) . "," . ($arr->tipo_quantita_id == "" ? "NULL" : $arr->tipo_quantita_id) . ",'" . $note . "'," . $pos . ")";
-
- $mysqlconnetion->insertRecord($query);
- $pos = $pos + 1;
- }
-
- $retValue["message"] = "Ricetta inserita con successo";
- } catch (Exception $e) {
- $retValue["message"] = $e->getMessage();
- }
-
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retValue);
-});
-
-$app->get('/photos/', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
-
- $query = "SELECT * from ( SELECT id as ricetta_id, `titolo`,`autore`, ".
- " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id) as num_img,".
- " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id and immagini.published = 1) as num_img_pub".
- " FROM `ricette`".
- " ) as tmp".
- " WHERE tmp.num_img> 0";
-
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $retObj = $mysqlconnetion->queryToObject($query);
- $mysqlconnetion->disconnetti();
- returnJson($app, $callbackFn, $retObj);
-});
-
-$app->get('/ricetta/:itemID/photos', function ($itemID) use ($app) {
- $callbackFn = $app->request()->get('callback');
-
- $query = "SELECT id as id_photo, type_format, from_profile_id, uploaded_date, profilo.Name as profile_name, published from immagini" .
- " INNER JOIN profilo ON profilo.ProfiloID = immagini.from_profile_id".
- " WHERE id_ricette = " . $itemID;
-
- $mysqlconnetion = new MysqlClass;
- //$mysqlconnetion->connetti();
- $retObj = $mysqlconnetion->queryToObject($query);
- $mysqlconnetion->disconnetti();
- returnJson($app, $callbackFn, $retObj);
+get('/categories', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconneti on->connetti();
+ $retObj = $mysqlconnetion->queryToObject("select ID as category_id, NAME as name from categorie order by name");
+ $mysqlconnetion->disconnetti();
+
+ returnJsonWithDecode($app, $callbackFn, $retObj);
+});
+
+$app->get('/typeingredients', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $retObj = $mysqlconnetion->queryToObject("select ID as tipo_ingredienti_id, name from tipo_ingredienti order by name");
+ $mysqlconnetion->disconnetti();
+
+ returnJsonWithDecode($app, $callbackFn, $retObj);
+});
+
+$app->get('/typeqtys', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $retObj = $mysqlconnetion->queryToObject("select ID as tipo_quantita_id, name from tipo_quantita");
+ $mysqlconnetion->disconnetti();
+
+ returnJsonWithDecode($app, $callbackFn, $retObj);
+});
+
+$app->get('/ricette/:catID', function ($categoryID) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $query = "select ID as ricetta_id, titolo, autore, valutazione, difficolta from ricette where ID_CATEGORIA = " . $categoryID . " order by titolo, autore";
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->get('/ricetta/body/:itemID', function ($itemID) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ // istanza della classe
+ $mysqlconnetion = new MysqlClass;
+ $query = "SELECT id_categoria, categorie.name AS categoria_name, ricette.ID AS ricetta_id, titolo, procedimento, autore, link_fonte, link_youtube, valutazione, difficolta FROM ricette INNER JOIN categorie ON categorie.ID = ricette.id_categoria WHERE ricette.ID = " . $itemID;
+ $retObj = $mysqlconnetion->queryToObject($query);
+
+ $query2 = "select ingredienti.id_tipo_ingredienti, tipo_ingredienti.name as nome_ingrediente, quantita, ingredienti.id_tipo_quantita, tipo_quantita.Name as unita, note, posizione from ingredienti " .
+ "inner join tipo_ingredienti on ingredienti.ID_TIPO_INGREDIENTI = tipo_ingredienti.ID " .
+ "left outer join tipo_quantita on ingredienti.ID_TIPO_QUANTITA = tipo_quantita.ID " .
+ "where ingredienti.ID_RICETTE = " . $itemID . " order by ingredienti.posizione";
+
+ $retObj2 = $mysqlconnetion->queryToObject($query2);
+
+ $retObj[0]["titolo"] = html_entity_decode($retObj[0]["titolo"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
+ $retObj[0]["procedimento"] = html_entity_decode($retObj[0]["procedimento"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
+ $retObj[0]["ingredienti"] = $retObj2;
+
+ $mysqlconnetion->disconnetti();
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->post('/typeingredients', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $json_data_body = json_decode($app->request()->post('bodydata'));
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $query = "insert into tipo_ingredienti(Name) values ('" . $json_data_body->name . "')";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retNewID);
+});
+
+$app->post('/ricetta/body', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $json_data_body = json_decode($app->request()->post('bodydata'));
+ $retValue["result"] = true;
+ $retValue["message"] = "";
+ $mysqlconnetion = new MysqlClass;
+ // istanza della classe
+ try {
+ $retNewID = 0;
+ if ($json_data_body->ricettaID != "") {
+ $query = "update ricette SET ID_CATEGORIA = " . $json_data_body->categoria_id .
+ ", titolo = '" . htmlentities($json_data_body->titolo, null, "UTF-8") .
+ "', procedimento = '" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) .
+ "', autore = '" . str_replace("'", "''", $json_data_body->autore) .
+ "', link_fonte = '" . $json_data_body->linkFonte .
+ "', Link_youtube = '" . $json_data_body->linkVideo .
+ "', Difficolta = '" . $json_data_body->difficolta .
+ "' where ID = " . $json_data_body->ricettaID;
+
+ $mysqlconnetion->executeQuery($query);
+ $queryDelete = "DELETE FROM ingredienti where ID_RICETTE = " . $json_data_body->ricettaID;
+ $mysqlconnetion->executeQuery($queryDelete);
+ $retNewID = $json_data_body->ricettaID;
+ } else {
+
+ $query = "insert into ricette(ID_CATEGORIA, titolo, procedimento, autore, link_fonte, Link_youtube,Difficolta) values (" .
+ $json_data_body->categoria_id . ",'" . htmlentities($json_data_body->titolo, null, "UTF-8") . "','" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) .
+ "','" . str_replace("'", "''", $json_data_body->autore) . "','" .
+ $json_data_body->linkFonte . "','" . $json_data_body->linkVideo . "', " . $json_data_body->difficolta . ")";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ }
+
+ $pos = 0;
+ foreach ($json_data_body->ingredienti as $arr) {
+ $note = "";
+ if ($arr->note != "") {
+ $note = str_replace("'", "''", htmlentities($arr->note));
+ }
+
+ $query = "insert into ingredienti(ID_TIPO_INGREDIENTI, ID_RICETTE, Quantita, ID_TIPO_QUANTITA, Note, Posizione) values (" .
+ $arr->ingrediente_id . "," . $retNewID . "," . ($arr->quantita == "" ? "0" : $arr->quantita) . "," . ($arr->tipo_quantita_id == "" ? "NULL" : $arr->tipo_quantita_id) . ",'" . $note . "'," . $pos . ")";
+
+ $mysqlconnetion->insertRecord($query);
+ $pos = $pos + 1;
+ }
+
+ $retValue["message"] = "Ricetta inserita con successo";
+ } catch (Exception $e) {
+ $retValue["message"] = $e->getMessage();
+ }
+
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retValue);
+});
+
+$app->get('/photos/', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+
+ $query = "SELECT * from ( SELECT id as ricetta_id, `titolo`,`autore`, ".
+ " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id) as num_img,".
+ " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id and immagini.published = 1) as num_img_pub".
+ " FROM `ricette`".
+ " ) as tmp".
+ " WHERE tmp.num_img> 0";
+
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->get('/ricetta/:itemID/photos', function ($itemID) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+
+ $query = "SELECT id as id_photo, type_format, from_profile_id, uploaded_date, profilo.Name as profile_name, published from immagini" .
+ " INNER JOIN profilo ON profilo.ProfiloID = immagini.from_profile_id".
+ " WHERE id_ricette = " . $itemID;
+
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+ returnJson($app, $callbackFn, $retObj);
});
\ No newline at end of file
diff --git a/mdbTester.php b/mdbTester.php
new file mode 100644
index 0000000..229296b
--- /dev/null
+++ b/mdbTester.php
@@ -0,0 +1,53 @@
+GetLink($folder . "/" . $imageFileName);
+
+return;
+*/
+$layer = ImageWorkshop::initFromPath($tmpFileName);
+
+echo $layer->getImage()->image_type;
+return;
+
+$layer->resizeByLargestSideInPixel(640, true);
+
+$layer->save(dirname($tmpFileName), $imageFileName);
+
+$imgData = addslashes(file_get_contents($tmpFileName));
+
+$layer->resizeByLargestSideInPixel(300, true);
+
+$layer->save(dirname($tmpFileName), $thumbFileName);
+
+try {
+ $dropBoxObj->CreateFolder($folder);
+} catch (DropboxException $ex) {
+
+}
+
+$dropBoxObj->UploadFile(dirname($tmpFileName) . "/" . $imageFileName, $folder . "/" . $imageFileName);
+
+$dropBoxObj->UploadFile(dirname($tmpFileName) . "/" . $thumbFileName, $folder . "/" . $thumbFileName);
\ No newline at end of file
diff --git a/myDropBoxObj.php b/myDropBoxObj.php
new file mode 100644
index 0000000..f120e43
--- /dev/null
+++ b/myDropBoxObj.php
@@ -0,0 +1,78 @@
+dropbox = new DropboxClient(
+ array(
+ 'app_key' => "ft0zodv89xx804e",
+ 'app_secret' => "ut43sn7m9wufy3s",
+ 'app_full_access' => true
+ ), 'it');
+ $this->internalLoad();
+ }
+
+ protected function internalLoad() {
+ // first try to load existing access token
+ $access_token = $this->load_token("access");
+ if (!empty($access_token)) {
+ $this->dropbox->SetAccessToken($access_token);
+ echo "loaded access token:";
+ print_r($access_token);
+ } elseif (!empty($_GET['auth_callback'])) { // are we coming from dropbox's auth page?
+ // then load our previosly created request token
+ $request_token = $this->load_token($_GET['oauth_token']);
+ if (empty($request_token))
+ die('Request token not found!');
+ // get & store access token, the request token is not needed anymore
+ $access_token = $this->dropbox->GetAccessToken($request_token);
+ $this->store_token($access_token, "access");
+ $this->delete_token($_GET['oauth_token']);
+ }
+ // checks if access token is required
+ if (!$this->dropbox->IsAuthorized()) {
+ // redirect user to dropbox auth page
+ $return_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?auth_callback=1";
+ $auth_url = $this->dropbox->BuildAuthorizeUrl($return_url);
+ $request_token = $this->dropbox->GetRequestToken();
+ $this->store_token($request_token, $request_token['t']);
+ die("Authentication required. Click here.");
+ }
+ }
+
+ public function UploadFile($fileToUpload, $dropBoxPath) {
+ $ret = $this->dropbox->UploadFile($fileToUpload, $dropBoxPath);
+ return true;
+ }
+
+ public function GetLink($dropBoxPathFile) {
+ return $this->dropbox->GetLink($dropBoxPathFile, false, false);
+ }
+
+ public function CreateFolder($dropBoxPath) {
+ $ret = $this->dropbox->CreateFolder($dropBoxPath);
+ return true;
+ }
+
+ private function store_token($token, $name) {
+ if (!file_put_contents("tokens/$name.token", serialize($token)))
+ die('
Could not store token! Make sure that the directory `tokens` exists and is writable!');
+ }
+
+ private function load_token($name) {
+ if (!file_exists("tokens/$name.token"))
+ return null;
+ return @unserialize(@file_get_contents("tokens/$name.token"));
+ }
+
+ private function delete_token($name) {
+ @unlink("tokens/$name.token");
+ }
+
+}
diff --git a/nbproject/private/config.properties b/nbproject/private/config.properties
new file mode 100644
index 0000000..e69de29
diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties
new file mode 100644
index 0000000..49ccdf8
--- /dev/null
+++ b/nbproject/private/private.properties
@@ -0,0 +1,9 @@
+browser.id=Chrome.INTEGRATED
+copy.src.files=false
+copy.src.on.open=false
+copy.src.target=
+hostname=localhost
+port=8888
+router=mdbTester.php
+run.as=INTERNAL
+url=http://localhost:8888/
diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml
new file mode 100644
index 0000000..284eeec
--- /dev/null
+++ b/nbproject/private/private.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/nbproject/project.properties b/nbproject/project.properties
new file mode 100644
index 0000000..6df3cbb
--- /dev/null
+++ b/nbproject/project.properties
@@ -0,0 +1,7 @@
+include.path=${php.global.include.path}
+php.version=PHP_53
+source.encoding=UTF-8
+src.dir=.
+tags.asp=false
+tags.short=false
+web.root=.
diff --git a/nbproject/project.xml b/nbproject/project.xml
new file mode 100644
index 0000000..aa3be94
--- /dev/null
+++ b/nbproject/project.xml
@@ -0,0 +1,9 @@
+
+
+ org.netbeans.modules.php.project
+
+
+ Service
+
+
+
diff --git a/profile.php b/profile.php
index 43b2044..b9e3369 100644
--- a/profile.php
+++ b/profile.php
@@ -1,111 +1,111 @@
-get('/profile/:keyStore/ricetta/:ricetta_id', function ($keyStore, $ricetta_id) use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- $query = "select COUNT(*) as Exist FROM blocco_note where ricetta_id = " . $ricetta_id . " AND ProfiloID = '" . $keyStore . "'";
- $retObj = $mysqlconnetion->queryToObject($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retObj[0]["Exist"]);
-});
-
-$app->post('/profile/ricetta', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $json_data_body = json_decode($app->request()->post('dataPair'));
- $mysqlconnetion = new MysqlClass;
- $query = "insert into blocco_note(ricetta_id, ProfiloID) values (" . $json_data_body->ricetta_id . ", '" . $json_data_body->keyStore . "')";
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retNewID);
-});
-
-$app->delete('/profile/:keyStore/ricetta/:ricetta_id', function ($keyStore, $ricetta_id) use ($app) {
- //$callbackFn = $app->request()->params('callback');
- $mysqlconnetion = new MysqlClass;
- $query = "delete from blocco_note where ricetta_id = " . $ricetta_id . " AND ProfiloID = '" . $keyStore . "'";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- echo $retNewID;
- //returnJson($app, $callbackFn, $retNewID);
-});
-
-$app->delete('/profile/:keyStore/ricette', function ($keyStore) use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- $query = "delete from blocco_note where ProfiloID = '" . $keyStore . "'";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retNewID);
-});
-
-$app->get('/profile/:keyStore/ricette', function ($keyStore) use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- $query = "select ID as ricetta_id, titolo, autore, valutazione, difficolta from ricette" .
- " INNER JOIN blocco_note ON blocco_note.ricetta_id = ricette.id" .
- " where blocco_note.ProfiloID = '" . $keyStore . "' order by titolo, autore";
- $retObj = $mysqlconnetion->queryToObject($query);
- $mysqlconnetion->disconnetti();
-
- foreach ($retObj as $ele) {
- $ele["titolo"] = html_entity_decode($ele["titolo"]);
- }
-
- returnJson($app, $callbackFn, $retObj);
-});
-
-$app->get('/profile/:keyStore', function ($keyStore) use ($app) {
- $callbackFn = $app->request()->get('callback');
- $mysqlconnetion = new MysqlClass;
- $query = "select ProfiloID, TipoAccesso, RisultatiRicerca, TemaUI, Name, Gender, 0 as NumRicette from profilo" .
- " where ProfiloID = '" . $keyStore . "'";
- $retObj = $mysqlconnetion->queryToObject($query);
-
- $query = "update profilo set PenultimoAccesso = UltimoAccesso, UltimoAccesso = NOW() where ProfiloID = '" . $keyStore . "'";
- $mysqlconnetion->insertRecord($query);
-
- $query = "SELECT COUNT( * ) as NumRicette FROM ricette WHERE data_creazione > ( SELECT PenultimoAccesso FROM profilo " .
- "WHERE `ProfiloID` = '" . $keyStore . "' )";
-
- $retObj2 = $mysqlconnetion->queryToObject($query);
-
- $retObj[0]["NumRicette"] = $retObj2[0]["NumRicette"];
-
- returnJson($app, $callbackFn, $retObj);
-});
-
-$app->post('/profile', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $json_data_body = json_decode($app->request()->post('dataPair'));
- $mysqlconnetion = new MysqlClass;
- $query = "insert into profilo(ProfiloID, Name, Email, Gender, TipoAccesso, RisultatiRicerca, TemaUI, UltimoAccesso, CreatoIl) values ('" . $json_data_body->keyStore . "', '" . $json_data_body->name . "', '" . $json_data_body->email . "', '" . $json_data_body->gender . "', '" . $json_data_body->type . "', '" . $json_data_body->risRic . "', '" . $json_data_body->tema . "', NOW(), NOW())";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retNewID);
-});
-
-$app->put('/profile', function () use ($app) {
- $callbackFn = $app->request()->get('callback');
- $json_data_body = json_decode($app->request()->post('dataPair'));
- $mysqlconnetion = new MysqlClass;
- $query = "update profilo set RisultatiRicerca = '" . $json_data_body->risRic . "', TemaUI = '" . $json_data_body->tema . "' where ProfiloID = '" . $json_data_body->keyStore . "'";
-
- $retNewID = $mysqlconnetion->insertRecord($query);
- $mysqlconnetion->disconnetti();
-
- returnJson($app, $callbackFn, $retNewID);
-});
-
-?>
+get('/profile/:keyStore/ricetta/:ricetta_id', function ($keyStore, $ricetta_id) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ $query = "select COUNT(*) as Exist FROM blocco_note where ricetta_id = " . $ricetta_id . " AND ProfiloID = '" . $keyStore . "'";
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retObj[0]["Exist"]);
+});
+
+$app->post('/profile/ricetta', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $json_data_body = json_decode($app->request()->post('dataPair'));
+ $mysqlconnetion = new MysqlClass;
+ $query = "insert into blocco_note(ricetta_id, ProfiloID) values (" . $json_data_body->ricetta_id . ", '" . $json_data_body->keyStore . "')";
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retNewID);
+});
+
+$app->delete('/profile/:keyStore/ricetta/:ricetta_id', function ($keyStore, $ricetta_id) use ($app) {
+ //$callbackFn = $app->request()->params('callback');
+ $mysqlconnetion = new MysqlClass;
+ $query = "delete from blocco_note where ricetta_id = " . $ricetta_id . " AND ProfiloID = '" . $keyStore . "'";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ echo $retNewID;
+ //returnJson($app, $callbackFn, $retNewID);
+});
+
+$app->delete('/profile/:keyStore/ricette', function ($keyStore) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ $query = "delete from blocco_note where ProfiloID = '" . $keyStore . "'";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retNewID);
+});
+
+$app->get('/profile/:keyStore/ricette', function ($keyStore) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ $query = "select ID as ricetta_id, titolo, autore, valutazione, difficolta from ricette" .
+ " INNER JOIN blocco_note ON blocco_note.ricetta_id = ricette.id" .
+ " where blocco_note.ProfiloID = '" . $keyStore . "' order by titolo, autore";
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+
+ foreach ($retObj as $ele) {
+ $ele["titolo"] = html_entity_decode($ele["titolo"]);
+ }
+
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->get('/profile/:keyStore', function ($keyStore) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ $query = "select ProfiloID, TipoAccesso, RisultatiRicerca, TemaUI, Name, Gender, 0 as NumRicette from profilo" .
+ " where ProfiloID = '" . $keyStore . "'";
+ $retObj = $mysqlconnetion->queryToObject($query);
+
+ $query = "update profilo set PenultimoAccesso = UltimoAccesso, UltimoAccesso = NOW() where ProfiloID = '" . $keyStore . "'";
+ $mysqlconnetion->insertRecord($query);
+
+ $query = "SELECT COUNT( * ) as NumRicette FROM ricette WHERE data_creazione > ( SELECT PenultimoAccesso FROM profilo " .
+ "WHERE `ProfiloID` = '" . $keyStore . "' )";
+
+ $retObj2 = $mysqlconnetion->queryToObject($query);
+
+ $retObj[0]["NumRicette"] = $retObj2[0]["NumRicette"];
+
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->post('/profile', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $json_data_body = json_decode($app->request()->post('dataPair'));
+ $mysqlconnetion = new MysqlClass;
+ $query = "insert into profilo(ProfiloID, Name, Email, Gender, TipoAccesso, RisultatiRicerca, TemaUI, UltimoAccesso, CreatoIl) values ('" . $json_data_body->keyStore . "', '" . $json_data_body->name . "', '" . $json_data_body->email . "', '" . $json_data_body->gender . "', '" . $json_data_body->type . "', '" . $json_data_body->risRic . "', '" . $json_data_body->tema . "', NOW(), NOW())";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retNewID);
+});
+
+$app->put('/profile', function () use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $json_data_body = json_decode($app->request()->post('dataPair'));
+ $mysqlconnetion = new MysqlClass;
+ $query = "update profilo set RisultatiRicerca = '" . $json_data_body->risRic . "', TemaUI = '" . $json_data_body->tema . "' where ProfiloID = '" . $json_data_body->keyStore . "'";
+
+ $retNewID = $mysqlconnetion->insertRecord($query);
+ $mysqlconnetion->disconnetti();
+
+ returnJson($app, $callbackFn, $retNewID);
+});
+
+?>
diff --git a/ricette.php b/ricette.php
index cd3d887..3d84941 100644
--- a/ricette.php
+++ b/ricette.php
@@ -33,7 +33,7 @@ $app->get('/typeqtys', function () use ($app) {
returnJson($app, $callbackFn, $retObj);
});
-$app->get('/categoryitems/:catID', function ($categoryID) use ($app) {
+$app->get('/ricette/:catID', function ($categoryID) use ($app) {
$callbackFn = $app->request()->get('callback');
$mysqlconnetion = new MysqlClass;
//$mysqlconnetion->connetti();
@@ -48,7 +48,7 @@ $app->get('/categoryitems/:catID', function ($categoryID) use ($app) {
returnJson($app, $callbackFn, $retObj);
});
-$app->get('/categoryitems/:categoryID/mostvote(/:numItems)', function ($categoryID, $numItems = 10) use ($app) {
+$app->get('/ricette/:categoryID/mostvote(/:numItems)', function ($categoryID, $numItems = 10) use ($app) {
$callbackFn = $app->request()->get('callback');
$mysqlconnetion = new MysqlClass;
//$mysqlconnetion->connetti();
@@ -63,7 +63,7 @@ $app->get('/categoryitems/:categoryID/mostvote(/:numItems)', function ($category
returnJson($app, $callbackFn, $retObj);
});
-$app->get('/categoryitems/:categoryID/lastinserted(/:numItems)', function ($categoryID, $numItems = 10) use ($app) {
+$app->get('/ricette/:categoryID/lastinserted(/:numItems)', function ($categoryID, $numItems = 10) use ($app) {
$callbackFn = $app->request()->get('callback');
$mysqlconnetion = new MysqlClass;
//$mysqlconnetion->connetti();
@@ -78,7 +78,23 @@ $app->get('/categoryitems/:categoryID/lastinserted(/:numItems)', function ($cate
returnJson($app, $callbackFn, $retObj);
});
-$app->get('/categoryitems/search/:numItems(/:categoryId(/:difficolta(/:titolo)))',
+$app->get('/ricette/lastinserted/:profileID', function ($profileID) use ($app) {
+ $callbackFn = $app->request()->get('callback');
+ $mysqlconnetion = new MysqlClass;
+ //$mysqlconnetion->connetti();
+ $query = "select ID as ricetta_id, titolo, autore FROM ricette WHERE data_creazione > ( SELECT PenultimoAccesso FROM profilo " .
+ "WHERE `ProfiloID` = '" . $profileID . "' ) order by Data_creazione desc, titolo, autore";
+ $retObj = $mysqlconnetion->queryToObject($query);
+ $mysqlconnetion->disconnetti();
+
+ foreach ($retObj as $ele) {
+ $ele["titolo"] = html_entity_decode($ele["titolo"], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
+ }
+
+ returnJson($app, $callbackFn, $retObj);
+});
+
+$app->get('/ricette/search/:numItems(/:categoryId(/:difficolta(/:titolo)))',
function ($numItems = 10, $categoryId = 0, $difficolta = 0, $titolo = "") use ($app) {
$callbackFn = $app->request()->get('callback');
//$filterItem = json_decode($app->request()->post('post'));
diff --git a/serviceapp.php b/serviceapp.php
index 0857f4b..648eabb 100644
--- a/serviceapp.php
+++ b/serviceapp.php
@@ -1,16 +1,16 @@
-group('/api', function () use ($app) {
- include "./ricette.php";
- include "./profile.php";
- include "./image.php";
-});
-
-$app->group('/backend', function () use ($app) {
- include "./management.php";
-});
-//include "./image.php";
-
+group('/api', function () use ($app, $dirRicetteDropBox) {
+ include "./ricette.php";
+ include "./profile.php";
+ include "./image.php";
+});
+
+$app->group('/backend', function () use ($app) {
+ include "./management.php";
+});
+//include "./image.php";
+
$app->run();
\ No newline at end of file
diff --git a/utility.php b/utility.php
index 76caec5..dc959cd 100644
--- a/utility.php
+++ b/utility.php
@@ -1,78 +1,87 @@
-= '300000') {
- return false;
- }
-
- /* step through inArray */
- foreach ($inArray as $key => $val) {
- if (is_array($val)) {
- /* recurse on array elements */
- $newArray[$key] = utf8json($val);
- } else {
- /* encode string values */
- $newArray[$key] = utf8_encode($val);
- }
- }
- /* return utf8 encoded array */
- return $newArray;
- }
- /* return utf8 encoded array */
- return $inArray;
-}
-
-function returnJsonWithDecode($app, $callbackFn, $retObj) {
- if ($callbackFn) {
- $app->contentType('application/javascript; Charset=UTF-8');
- echo $callbackFn . "(" . html_entity_decode(json_encode(utf8json($retObj)), ENT_COMPAT | ENT_HTML401, 'ISO-8859-1') . ")";
- } else {
- $app->contentType('application/x-json; Charset=UTF-8');
- echo html_entity_decode(json_encode(utf8json($retObj)), ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
- }
-}
-
-function returnJson($app, $callbackFn, $retObj) {
- if ($callbackFn) {
- $app->contentType('application/javascript; Charset=UTF-8');
- echo $callbackFn . "(" . (json_encode(utf8json($retObj))) . ")";
- } else {
- $app->contentType('application/x-json; Charset=UTF-8');
- echo (json_encode(utf8json($retObj)));
- }
-}
-
-function makeThumbnail($im) {
- $final_width_of_image = 300;
- $ox = imagesx($im);
- $oy = imagesy($im);
-
- $nx = $final_width_of_image;
- $ny = floor($oy * ($final_width_of_image / $ox));
-
- $nm = imagecreatetruecolor($nx, $ny);
-
- imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
-
- return $nm;
-}
-
-function getContentFromResources($res) {
- ob_start(); //Start output buffer.
- imagejpeg($res); //This will normally output the image, but because of ob_start(), it won't.
- $contents = ob_get_contents(); //Instead, output above is saved to $contents
- ob_end_clean(); //End the output buffer.
-
- return $contents;
-}
-
-?>
+= '300000') {
+ return false;
+ }
+
+ /* step through inArray */
+ foreach ($inArray as $key => $val) {
+ if (is_array($val)) {
+ /* recurse on array elements */
+ $newArray[$key] = utf8json($val);
+ } else {
+ /* encode string values */
+ $newArray[$key] = utf8_encode($val);
+ }
+ }
+ /* return utf8 encoded array */
+ return $newArray;
+ }
+ /* return utf8 encoded array */
+ return $inArray;
+}
+
+function returnJsonWithDecode($app, $callbackFn, $retObj) {
+ if ($callbackFn) {
+ $app->contentType('application/javascript; Charset=UTF-8');
+ echo $callbackFn . "(" . html_entity_decode(json_encode(utf8json($retObj)), ENT_COMPAT | ENT_HTML401, 'ISO-8859-1') . ")";
+ } else {
+ $app->contentType('application/x-json; Charset=UTF-8');
+ echo html_entity_decode(json_encode(utf8json($retObj)), ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
+ }
+}
+
+function returnJson($app, $callbackFn, $retObj) {
+ if ($callbackFn) {
+ $app->contentType('application/javascript; Charset=UTF-8');
+ echo $callbackFn . "(" . (json_encode(utf8json($retObj))) . ")";
+ } else {
+ $app->contentType('application/x-json; Charset=UTF-8');
+ echo (json_encode(utf8json($retObj)));
+ }
+}
+
+function makeThumbnail($im) {
+ $final_width_of_image = 300;
+ $ox = imagesx($im);
+ $oy = imagesy($im);
+
+ $nx = $final_width_of_image;
+ $ny = floor($oy * ($final_width_of_image / $ox));
+
+ $nm = imagecreatetruecolor($nx, $ny);
+
+ imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
+
+ return $nm;
+}
+
+function getContentFromResources($res) {
+ ob_start(); //Start output buffer.
+ imagejpeg($res); //This will normally output the image, but because of ob_start(), it won't.
+ $contents = ob_get_contents(); //Instead, output above is saved to $contents
+ ob_end_clean(); //End the output buffer.
+
+ return $contents;
+}
+
+?>