/** * 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); } }
if ($layerWidth > $mainLayerWidth) { $layerWidth = $col_x * $watermarkWidth; } } if ($offsetTop < 0) { $maxWM_y = 2 * intval($mainLayerHeight / ($watermarkHeight - $wmMarginBottom)); $unseenWM_y = intval(abs($offsetTop) / $watermarkHeight); $offsetTop = $offsetTop + $unseenWM_y * $watermarkHeight; $col_y = ceil((abs($coooffsetToprdy) + $mainLayerHeight) / $watermarkHeight); $layerHeight = $watermarkHeight * ($maxWM_y - $unseenWM_y); if ($layerHeight > $mainLayerHeight) { $layerHeight = $col_y * $watermarkHeight; } } $layer = ImageWorkshop::initVirginLayer($mainLayerWidth, $mainLayerHeight); $row = ImageWorkshop::initVirginLayer($mainLayerWidth, $watermarkHeight); $tile_x = 0; $tile_y = 0; $x = 0; $y = 0; while ($x++ < $col_x) { $row->addLayer(1, $watermark, $tile_x, 0, "LT"); $row->mergeAll(); $tile_x += $watermarkWidth; } while ($y++ < $col_y) { $layer->addLayer(1, $row, 0, $tile_y, "LT"); $layer->mergeAll(); $tile_y += $watermarkHeight; } $watermark = clone $layer;
/** * Test initVirginLayer */ public function testInitVirginLayer() { $layer = ImageWorkshop::initVirginLayer(189, 242, 'ff0000'); $this->assertTrue(is_object($layer) === true, 'Expect $layer to be an object'); $this->assertTrue(get_class($layer) === 'PHPImageWorkshop\\Core\\ImageWorkshopLayer', 'Expect $layer to be an ImageWorkshopLayer object'); }
/** * Initialize a layer * * @param integer $method */ protected function initializeLayer($method = 1) { $layer = ImageWorkshop::initVirginLayer(100, 75); switch ($method) { case 1: break; case 2: // Add 4 sublayers in $layer stack $layer->addLayer(1, $layer); $layer->addLayer(2, $layer); $layer->addLayer(3, $layer); $layer->addLayer(4, $layer); break; case 3: // Add 5 sublayers in $layer stack $layer->addLayer(1, $layer); $layer->addLayer(2, $layer); $layer->addLayer(3, $layer); $layer->addLayer(4, $layer); $layer->addLayer(5, $layer); break; } return $layer; }
/** * Print the image as an ImageWorkshop object * * @return ImageWorkshop $bgLayer */ public function paintImage() { // Image Settings $width = 700; $height = 700; $bgColor = 'FFC4C6'; $fontDirectory = public_path('assets/fonts/'); // Border Settings $border = 9; $borderColor = 'FFFFFF'; // Recipient Settings $toColor = 'f51e6b'; $toFontSize = 25; $toGreeting = 'Dear'; // Sender Settings $fromColor = 'f51e6b'; $fromFontSize = 16; $fromMessage = 'your secret crush'; // Message Settings $message = $this->parseMessage($this->message); $msgColor = 'FFFFFF'; $msgFontSize = 26; // Initialise Layers $bgLayer = ImageWorkshop::initVirginLayer($width, $height, $bgColor); // Footer $footerLayer = ImageWorkshop::initVirginLayer($bgLayer->getWidth(), $bgLayer->getHeight()); $footerBird = ImageWorkshop::initFromPath(public_path('assets/img/shareable/footer-bird.png')); $footerFrom = ImageWorkshop::initTextLayer(strtoupper($fromMessage), $fontDirectory . 'Proxima_Nova/PROXIMANOVA-SEMIBOLD.OTF', $fromFontSize, $fromColor, 0); // Border $borderLayer = ImageWorkshop::initVirginLayer($bgLayer->getWidth(), $bgLayer->getHeight()); $hBorder = ImageWorkshop::initVirginLayer($width, $border, $borderColor); $vBorder = ImageWorkshop::initVirginLayer($border, $height, $borderColor); // Recipient $toLayer = ImageWorkshop::initVirginLayer($bgLayer->getWidth(), $bgLayer->getHeight()); $toText = ImageWorkshop::initTextLayer(strtoupper($toGreeting . ' ' . $this->recipient), $fontDirectory . 'Proxima_Nova/PROXIMANOVA-REGULAR.OTF', $toFontSize, $toColor, 0, $borderColor); $toBackground = ImageWorkshop::initVirginlayer($toText->getWidth() + $toFontSize * 2, $toFontSize * 2, $borderColor); $toLeftBorder = ImageWorkshop::initFromPath(public_path('assets/img/shareable/to-bookend-left.png')); $toRightBorder = ImageWorkshop::initFromPath(public_path('assets/img/shareable/to-bookend-right.png')); // Message $msgLayer = ImageWorkshop::initVirginLayer($bgLayer->getWidth(), $bgLayer->getHeight()); $msgText = ImageWorkshop::initTextLayer(strtoupper($message), $fontDirectory . 'Proxima_Nova/PROXIMANOVA-SEMIBOLD.OTF', $msgFontSize, $msgColor, 0); // Add Layers // Recipient $toLayer->addLayer(1, $toBackground, 0, 100 - $toFontSize / 2, 'MT'); $toLayer->addLayer(2, $toText, 0, 100, 'MT'); $toLayer->addLayer(3, $toLeftBorder, 0 - ($toBackground->getWidth() / 2 + $toLeftBorder->getWidth() / 2), 100 - $toFontSize / 2, 'MT'); $toLayer->addLayer(3, $toRightBorder, 0 - 1 + ($toBackground->getWidth() / 2 + $toLeftBorder->getWidth() / 2), 100 - $toFontSize / 2, 'MT'); // Add Message $msgLayer->addLayer(1, $msgText, 0, -50 + $msgText->getHeight() / (substr_count($message, "\n") + 1), 'MM'); // Add Borders $borderLayer->addLayer(1, $hBorder, 0, 0); $borderLayer->addLayer(1, $hBorder, 0, 0, 'LB'); $borderLayer->addLayer(1, $vBorder, 0, 0); $borderLayer->addLayer(1, $vBorder, 0, 0, 'RT'); // Add Footer $footerLayer->addLayer(1, $footerBird, 0, 0, 'MB'); $footerLayer->addLayer(1, $footerFrom, $fromFontSize * 5.5, $fromFontSize * 5, 'RM'); // Create Image $bgLayer->addLayer(1, $footerLayer); $bgLayer->addLayer(2, $toLayer); $bgLayer->addLayer(2, $msgLayer); $bgLayer->addLayer(3, $borderLayer); // Resize $bgLayer->resizeInPixel(440, null, true); return $bgLayer; }
/** * 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 float $width * @param float $height * @param float $positionX * @param float $positionY * @param string $position */ public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') { 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)) { $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); } }
/** * Resize an image * * Image properties parameters: * - size : Square size (set to 0 if not square) * - width : Width (if not square) * - height : Height (if not square) * - max_size : Resize to fit square at maximum * - max_width : Resize to fit non square at maximum * - max_height : Resize to fit non square at maximum * - crop : Crop image * - crop_position : Crop image position (L = left, T = top, M = middle, B = bottom, R = right) * - quality : Output image quality (from 0 to 100) * - enlarge : Enlarge image when source is smaller than output. Fill with bg_color when false * - trim_bg : Remove the background color when not enlarging * - keep_proportions : Keep source image proportions (and fill with blank if needed) * - bg_color : Background color when image does not fill expected output size * * @param string $sourcePath Image path to process * @param string $destPathWithFormat Folder to output processed image * @param array $dim Image properties */ public static function resize($sourcePath, $destPathWithFormat, $dim) { $fixOrientation = isset($dim['fix_orientation']) ? $dim['fix_orientation'] : false; $layer = ImageWorkshop::initFromPath($sourcePath, $fixOrientation); $initSize = array('width' => $layer->getWidth(), 'height' => $layer->getHeight(), 'wh_ratio' => $layer->getWidth() / $layer->getHeight()); if ($dim['size'] > 0) { $dim['width'] = $dim['size']; $dim['height'] = $dim['size']; } if ($dim['max_size'] > 0) { $dim['max_width'] = $dim['max_size']; $dim['max_height'] = $dim['max_size']; } if ($dim['width'] > 0 || $dim['height'] > 0) { if ($dim['crop']) { if ($dim['keep_proportions']) { if ($layer->getWidth() / $dim['width'] > $layer->getHeight() / $dim['height']) { $layer->resizeInPixel(null, $dim['height'], true, 0, 0, 'MM'); } else { $layer->resizeInPixel($dim['width'], null, true, 0, 0, 'MM'); } $layer->cropInPixel($dim['width'], $dim['height'], 0, 0, $dim['crop_position']); } else { $layer->resizeInPixel($dim['width'], $dim['height']); $layer->cropInPixel($dim['width'], $dim['height'], 0, 0, $dim['crop_position']); } } if (!$dim['enlarge'] && $dim['trim_bg']) { if ($layer->getWidth() > $dim['width'] && $dim['width'] > 0) { $layer->resizeInPixel($dim['width'], null, true, 0, 0, 'MM'); } if ($layer->getHeight() > $dim['height'] && $dim['height'] > 0) { $layer->resizeInPixel(null, $dim['height'], true, 0, 0, 'MM'); } } else { if (!$dim['enlarge'] && $layer->getWidth() <= $dim['width'] && $layer->getHeight() <= $dim['height']) { $boxLayer = ImageWorkshop::initVirginLayer($dim['width'], $dim['height'], $dim['bg_color']); $boxLayer->addLayer(1, $layer, 0, 0, 'MM'); $layer = $boxLayer; } else { if ($dim['trim_bg']) { $hratio = $dim['height'] / $layer->getHeight(); $wratio = $dim['width'] / $layer->getWidth(); $w = $wratio < $hratio ? $dim['width'] : $dim['height'] * $initSize['wh_ratio']; $h = $hratio < $wratio ? $dim['height'] : $dim['width'] / $initSize['wh_ratio']; $layer->resizeInPixel($w, $h, $dim['keep_proportions'], 0, 0, 'MM'); } else { $dim['width'] = $dim['width'] > 0 ? $dim['width'] : null; $dim['height'] = $dim['height'] > 0 ? $dim['height'] : null; $layer->resizeInPixel($dim['width'], $dim['height'], $dim['keep_proportions'], 0, 0, 'MM'); } } } //Add color background if (!$dim['trim_bg'] && ($layer->getWidth() <= $dim['width'] && $layer->getHeight() <= $dim['height'])) { $colorLayer = ImageWorkshop::initVirginLayer($dim['width'], $dim['height'], $dim['bg_color']); $colorLayer->addLayer(1, $layer, 0, 0, 'MM'); $layer = $colorLayer; } } if ($dim['max_width'] > 0 || $dim['max_height'] > 0) { if ($layer->getWidth() / $dim['max_width'] > $layer->getHeight() / $dim['max_height']) { $layer->resizeInPixel(min($dim['max_width'], $layer->getWidth()), null, true, 0, 0, 'MM'); } else { $layer->resizeInPixel(null, min($dim['max_height'], $layer->getHeight()), true, 0, 0, 'MM'); } } $layer->save(substr($destPathWithFormat, 0, strrpos($destPathWithFormat, '/')), substr($destPathWithFormat, strrpos($destPathWithFormat, '/') + 1), true, null, $dim['quality']); $layer = null; }