private function saveImages($filesInfo, $dir, $thumbs) { $this->checkDir($dir); $result = array(); $i = 1; foreach ($filesInfo as $file) { foreach ($thumbs as $key => $thumb) { // http://phpimageworkshop.com/documentation.html $layer = ImageWorkshop::initFromPath($file->path); if (isset($thumb['action']) == true) { switch ($thumb['action']) { case "exact_resize": $layer->resizeInPixel($thumb['width'], $thumb['height'], true, 0, 0, 'MM'); break; case "landscape_resize": $layer->resizeInPixel($thumb['width'], null, true); break; case "portrait_resize": $layer->resizeInPixel(null, $thumb['height'], true); break; case "exact_crop": if ($thumb['width'] / $thumb['height'] < 1) { $resize = $thumb['width']; } else { $resize = $thumb['height']; } $layer->resizeByNarrowSideInPixel($resize, true); $layer->cropInPixel($thumb['width'], $thumb['height'], 0, 0, "MM"); break; default: $layer->resizeInPixel($thumb['width'], $thumb['height'], false); //exact, without props break; } } if (isset($thumb['watermark']) == true) { $watermarkLayer = ImageWorkshop::initFromPath(__DIR__ . '/../Resources/public/images/' . $thumb['watermark']); $watermarkLayer->opacity($thumb['opacity']); $layer->addLayerOnTop($watermarkLayer, $thumb['padding'], $thumb['padding'], $thumb['position']); } $this->checkDir($dir . '/' . $i); $name = $key . uniqid() . '.' . $thumb['format']; $layer->save($dir . '/' . $i, $name, false, null, $thumb['quality']); $result[] = $i . '/' . $name; } $i++; unlink($file->path); } return $result; }
/** * 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); } }