/** * Process image types here and returns filename to write * * @param array $aFile uploaded file information * @param string $folder the file path * @param string $aName the file name * @param array $imageInfo image information array: width, height, resize_mode * * @return bool|string * @throws ImageWorkshopException * @throws Exception */ public function processImage($aFile, $folder, $aName, $imageInfo) { $ext = self::_getImageExt($aFile['type']); if (empty($ext)) { $this->setMessage(iaLanguage::getf('file_type_error', array('extension' => implode(', ', array_unique(self::$_typesMap))))); return false; } try { $path = IA_UPLOADS . $folder; $image = ImageWorkshop::initFromPath($aFile['tmp_name']); // save source image $image->save($path, self::SOURCE_PREFIX . $aName . $ext); // process thumbnails for files uploaded in CKEditor and other tools if (empty($imageInfo)) { // apply watermark $image = self::_applyWaterMark($image); $image->save($path, self::_createFilename($aName, $ext)); return true; } // check this is an animated GIF if (self::ALLOW_ANIMATED_GIFS && 'image/gif' == $aFile['type']) { require_once IA_INCLUDES . 'phpimageworkshop' . IA_DS . 'Core' . IA_DS . 'GifFrameExtractor.php'; $gifPath = $aFile['tmp_name']; if (GifFrameExtractor\GifFrameExtractor::isAnimatedGif($gifPath)) { // Extractions of the GIF frames and their durations $gfe = new GifFrameExtractor\GifFrameExtractor(); $frames = $gfe->extract($gifPath); // For each frame, we add a watermark and we resize it $retouchedFrames = array(); $thumbFrames = array(); foreach ($frames as $frame) { $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']); $thumbLayer = ImageWorkshop::initFromResourceVar($frame['image']); $frameLayer->resizeInPixel($imageInfo['image_width'], $imageInfo['image_height'], true); $frameLayer = self::_applyWaterMark($frameLayer); $retouchedFrames[] = $frameLayer->getResult(); $thumbLayer->resizeInPixel($imageInfo['thumb_width'], $imageInfo['thumb_height'], true); $thumbFrames[] = $thumbLayer->getResult(); } // Then we re-generate the GIF require_once IA_INCLUDES . 'phpimageworkshop' . IA_DS . 'Core' . IA_DS . 'GifCreator.php'; $gc = new GifCreator\GifCreator(); $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0); file_put_contents($path . self::_createFilename($aName, $ext), $gc->getGif()); $thumbCreator = new GifCreator\GifCreator(); $thumbCreator->create($thumbFrames, $gfe->getFrameDurations(), 0); file_put_contents($path . self::_createFilename($aName, $ext, true), $thumbCreator->getGif()); return self::_createFilename($folder . $aName, $ext, true); } } // save full image $largestSide = $imageInfo['image_width'] > $imageInfo['image_height'] ? $imageInfo['image_width'] : $imageInfo['image_height']; if ($largestSide) { $image->resizeByLargestSideInPixel($largestSide, true); } $image = self::_applyWaterMark($image); $image->save($path, self::_createFilename($aName, $ext)); // generate thumbnail $thumbWidth = $imageInfo['thumb_width'] ? $imageInfo['thumb_width'] : $this->iaCore->get('thumb_w'); $thumbHeight = $imageInfo['thumb_height'] ? $imageInfo['thumb_height'] : $this->iaCore->get('thumb_h'); if ($thumbWidth || $thumbHeight) { $thumb = ImageWorkshop::initFromPath($aFile['tmp_name']); switch ($imageInfo['resize_mode']) { case self::FIT: $thumb->resizeInPixel($thumbWidth, $thumbHeight, true, 0, 0, 'MM'); break; case self::CROP: $largestSide = $thumbWidth > $thumbHeight ? $thumbWidth : $thumbHeight; $thumb->cropMaximumInPixel(0, 0, 'MM'); $thumb->resizeInPixel($largestSide, $largestSide); $thumb->cropInPixel($thumbWidth, $thumbHeight, 0, 0, 'MM'); } $thumb->save($path, self::_createFilename($aName, $ext, true)); } } catch (Exception $e) { $this->iaView->setMessages(iaLanguage::get('invalid_image_file')); return false; } return self::_createFilename($folder . $aName, $ext, true); }
/** * 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 int $layerId * * @return bool */ 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; }
public function automatic_reduction($file, $image_url) { $filetype = $this->getFileType($file); if ((!empty($this->maximum_picture_size['width']) || !empty($this->maximum_picture_size['height'])) && ($this->width > $this->maximum_picture_size['width'] || $this->height > $this->maximum_picture_size['height'])) { if ($this->width > $this->height) { $maximum_picture_size_width = empty($this->maximum_picture_size['width']) ? $this->width * $this->maximum_picture_size['height'] / $this->height : $this->maximum_picture_size['width']; $new_width = intval($maximum_picture_size_width); $new_height = intval($this->height * $maximum_picture_size_width / $this->width); } else { $maximum_picture_size_height = empty($this->maximum_picture_size['height']) ? $this->height * $this->maximum_picture_size['width'] / $this->width : $this->maximum_picture_size['height']; $new_width = intval($this->width * $maximum_picture_size_height / $this->height); $new_height = intval($maximum_picture_size_height); } $layer = ImageWorkshop::initFromString($file); if ($filetype == 'gif' && GifFrameExtractor\GifFrameExtractor::isAnimatedGif($this->temp)) { $gfe = new GifFrameExtractor\GifFrameExtractor(); $frames = $gfe->extract($this->temp, true); $retouchedFrames = array(); foreach ($frames as $frame) { $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']); $frameLayer->resizeInPixel($new_width, $new_height); $retouchedFrames[] = $frameLayer->getResult(); } $gc = new GifCreator\GifCreator(); $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0); $file = $gc->getGif(); } else { $layer->resizeInPixel($new_width, $new_height, true); ob_start(); switch ($filetype) { case 'jpg': case 'jpeg': ImageJpeg($layer->getResult(), null, 75); break; case 'png': imagealphablending($layer->getResult(), false); imagesavealpha($layer->getResult(), true); ImagePng($layer->getResult()); break; case 'gif': ImageGif($layer->getResult(), null, 75); break; } $file = ob_get_contents(); ob_end_clean(); } imagedestroy($layer->getResult()); $this->width = $new_width; $this->height = $new_height; } return $file; }
/** * Test initFromResourceVar */ public function testInitFromResourceVar() { $layer = ImageWorkshop::initFromResourceVar(imageCreateFromJPEG(__DIR__ . static::IMAGE_SAMPLE_PATH)); $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'); }
$deltaY = round($deltaY * $basicImageLayerHeight / IMG_CONTAINER_HEIGHT); $deltaX = round($deltaX * $basicImageLayerWidth / (IMG_CONTAINER_HEIGHT * $relIndex)); } } } //generate pattern matrix $wmarkCountX = round($basicImageLayerWidth * 2 / $waterMarkLayerWidth); $wmarkCountY = round($basicImageLayerHeight * 2 / $waterMarkLayerHeight); $wmarkPosX = $waterMarkLayerXpos; $wmarkPosY = $waterMarkLayerYpos; for ($i = 0; $i < $wmarkCountY; $i++) { if ($wmarkPosY < $basicImageLayerHeight && $wmarkPosY > -$waterMarkLayerHeight) { for ($j = 0; $j < $wmarkCountX; $j++) { if ($wmarkPosX < $basicImageLayerWidth && $wmarkPosX > -$waterMarkLayerWidth) { $basicImageLayer->addLayer(1, $waterMarkLayer, $wmarkPosX, $wmarkPosY, "LT"); $basicImageLayer = ImageWorkshop::initFromResourceVar($basicImageLayer->getResult()); } $wmarkPosX += $deltaX + $waterMarkLayerWidth; } } $wmarkPosY += $deltaY + $waterMarkLayerHeight; $wmarkPosX = $waterMarkLayerXpos; } } $image = $basicImageLayer->getResult(); $tempName = generateRandomName('.jpg'); imagejpeg($image, './php/temp/' . $tempName, 95); response(200, $tempName); exit; } catch (Exception $e) { response(400, $lang["perr_expt"] . $e->getMessage());