parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

imageMagick.p

Janek 16.04.2010 12:38 / 16.04.2010 13:53

###########################################################################
# $Id: ImageMagick.p,v 1.2 2007/05/14 10:45:36 misha Exp $
# Author: Michael V. Petrushin
# Implementation for working with images using ImageMagic: http://www.imagemagick.org/
#	Janek 2010/04/16 10:37:00:
# 	Some fixes + new actions implementation (text + watermark)
###########################################################################


@CLASS
ImageMagick

@USE
Img.p
pf/types/pfString.p

@BASE
Img



###########################################################################
@auto[]
$sDefaultPath[]
$sDefaultScript[convert]
#end @auto[]



###########################################################################
@create[hParam]
^BASE:create[$hParam]
#end @create[]



###########################################################################
@_parseInfo[fInfo][sDummy;tSize]
$result[
	$.hRaw[^hash::create[]]
]
#^throw[;$fInfo.status=$fInfo.text]
^if(!$fInfo.status && def $fInfo.text){
	$sDummy[^fInfo.text.match[^^\s*(\S.+\S):\s+(.+)\s*^$][gm]{
		^result.hRaw.add[$.[$match.1][$match.2]]
		^switch[$match.1]{
			^case[Format]{
				^result.add[$.sFormat[^match.2.match[^^(\S+)\s.*][]{^match.1.lower[]}]]
			}
			^case[Geometry]{
				$tSize[^match.2.split[x][lh]]
				^result.add[
					$.iWidth($tSize.0)
					$.iHeight(^pfString:left[$tSize.1;+])
				]
			}
			^case[Compression;Orientation;Quality]{^result.add[$.[s$match.1][$match.2]]}
			^case[Colormap]{^result.add[$.iColors($match.2)]}
		}
	}]
}
#end @_parseInfo[]



###########################################################################
@_exec[sAction;hParam][position2gravity;correctPath]
^if($hParam.sFormat ne "gif"){
	^hParam.delete[iColors]
}
^if($sAction eq "watermark"){
	^if(!def $hParam.sPosition){
		^throw[;For now - only implemented positioning by ^$.sPosition]
	}
	$position2gravity[^table::create{from	to}]
	^position2gravity.append{top-left	NorthWest}
	^position2gravity.append{top-center	North}
	^position2gravity.append{top-right	NorthEast}
	^position2gravity.append{center-left	West}
	^position2gravity.append{center	Center}
	^position2gravity.append{center-right	East}
	^position2gravity.append{bottom-left	SouthWest}
	^position2gravity.append{bottom-center	South}
	^position2gravity.append{bottom-right	SouthEast}

	^if(!def $hParam.sPosition){
		$hParam.sPosition[center]
	}{
		$hParam.sPosition[^hParam.sPosition.lower[]]
		$hParam.sPosition[^hParam.sPosition.replace[$position2gravity]]
	}
	
	^if($hParam.iAlpha == 100){
		^hParam.delete[iAlpha]
	}
}
^if((def $hParam.sOutput) && !-d ^file:dirname[$hParam.sOutput]){
	^hParam.sOutput.save[^file:dirname[^hParam.sOutput.match[^^$env:DOCUMENT_ROOT][g][]]/null]
}


$result[^switch[$sAction]{
	^case[info]{^file::exec[${sScriptPath}/identify;;-verbose;$hParam.sInput]}
	^case[convert]{^file::exec[$sScript;;$hParam.sInput;^if($hParam.sFormat eq "jpeg"){-flatten};^if($hParam.bRemoveMeta){-strip}{-quiet};-format;$hParam.sFormat;-quality;$hParam.iQuality;^if($hParam.iColors){-colors}{-quiet};^if($hParam.iColors){$hParam.iColors}{-quiet};$hParam.sOutput]}
	^case[rotate]{^file::exec[$sScript;;$hParam.sInput;-background;rgb($hParam.iR,$hParam.iG,$hParam.iB);^if($hParam.sFormat eq "jpeg"){-flatten};-rotate;$hParam.iAngle;^if($hParam.bRemoveMeta){-strip}{-quiet};-quality;$hParam.iQuality;^if($hParam.iColors){-colors}{-quiet};^if($hParam.iColors){$hParam.iColors}{-quiet};$hParam.sOutput]}
	^case[crop]{^file::exec[$sScript;;$hParam.sInput;^if($hParam.sFormat eq "jpeg"){-flatten};^if($hParam.iColors){-colors}{-quiet};^if($hParam.iColors){$hParam.iColors}{-quiet};-quality;$hParam.iQuality;-crop;${hParam.iCropWidth}x${hParam.iCropHeight}+${hParam.iX}+${hParam.iY};^if($hParam.bRemoveMeta){-strip}{-quiet};-format;$hParam.sFormat;$hParam.sOutput]}
	^case[resize]{^file::exec[$sScript;;$hParam.sInput;^if($hParam.sFormat eq "jpeg"){-flatten};-filter;$hParam.sRType;-quality;$hParam.iQuality;^if($hParam.iColors){-colors}{-quiet};^if($hParam.iColors){$hParam.iColors}{-quiet};-resize;${hParam.sWidth}x${hParam.sHeight}^if(!$hParam.bKeepRatio){!}^switch[$hParam.sFlag]{^case[incr]{<}^case[decr]{>}};^if($hParam.bRemoveMeta){-strip}{-quiet};-format;$hParam.sFormat;$hParam.sOutput]}
	^case[watermark]{^file::exec[${sScriptPath}/composite;;-gravity;$hParam.sPosition;^if(def $hParam.iAlpha){-dissolve}{-quiet};^if(def $hParam.iAlpha){$hParam.iAlpha}{-quiet};^if($hParam.bRemoveMeta){-strip}{-quiet};-format;$hParam.sFormat;-quality;$hParam.iQuality;$hParam.sWMFile;$hParam.sInput;$hParam.sOutput]}
	^case[text]{^file::exec[$sScript;;-background;$hParam.background;-fill;$hParam.color;-font;$hParam.font;-pointsize;${hParam.size};label:$hParam.text;$hParam.sOutput]}]
	^case[DEFAULT]{^throw[$sClassName;;Action '$sAction' not implemented yet]}
}]


^if(-f "^file:dirname[$hParam.sOutput]/null" && def $hParam.sOutput){
	^file:delete[^file:dirname[^hParam.sOutput.match[^^$env:DOCUMENT_ROOT][g][]]/null]
}

^if($sAction ne "info" && $hParam.sInput ne $hParam.sOutput){
	^if(!(-f ^hParam.sOutput.match[^^$env:DOCUMENT_ROOT][g][])){
		^throw[;$result.stderr $result.text status: $result.status]
	}
}

#end @_exec[]


@text[sFileDest;sFormat;hParams][fResult]
^if(-f $sFileDest){^file:delete[$sFileDest]}

$hParams[^self._prepareParams[$hParams]]
^if(!def $sFileDest){^throw[$sClassName;text;Destination file MUST be specified]}

^if(!def $hParams.text){^throw[$sClassName;text;Text MUST be specified]}

$hParams.text[^hParams.text.match[((\\n){1,})][g]{\\n}]
$hParams.text[^hParams.text.match[((\n){1,})][g]{\n}]
$hParams.text[^hParams.text.match[(["]{1,})][g]{}]

^if(!def $hParams.background){$hParams.background[transparent]}
^if(!def $hParams.color){$hParams.color['#000']}
^if(!def $hParams.size){$hParams.size(50)}
^if(!def $hParams.font){$hParams.font[Helvetica]}

$hParams.sOutput[$sDocRoot/$sFileDest]
$hParams.sFormat[^self._getDestFormat[$sAction;$sFileSrc;$sFileDest;$sFormat]]

$fResult[^self._exec[text;$hParams]]
$result($fResult.status)

@_getResampleType[sAction;sType]
$result[^switch[$sType]{
	^case[lz;lanczos;]{lanczos}
	^case[g;gaussian]{gaussian}
	^case[m;mitchell]{mitchell}
#	ImageMagic specific
	^case[DEFAULT]{^throw[$sClassName;$sAction;Unknown resample type '$sType']}
}]
#end @_getResampleType[]



###########################################################################
@_getResizeType[sAction;sResizeType]
$result[^switch[$sResizeType]{
	^case[incr;decr]{$sResizeType}
	^case[DEFAULT]{^throw[$sClassName;$sAction;Unknown resize type '$sResizeType']}
}]
#end @_getResizeType[]