“MagickWand”的版本间的差异
来自Blueidea
小 |
小 |
||
| (未显示2个用户的7个中间版本) | |||
| 第1行: | 第1行: | ||
| − | MagickWand 是PHP的一个扩展程序,通过它建立起与[[:Category:ImageMagick|ImageMagick]]的交互,进行图片的处理。它是默认的GD图象函数库的绝佳替代方案。从安全性和易用性来说,在PHP中使用MagickWand比使用命令行ImageMagick要安全快捷的多。另外 [ | + | == 简介 == |
| + | MagickWand 是PHP的一个扩展程序,通过它建立起与[[:Category:ImageMagick|ImageMagick]]的交互,进行图片的处理。它是默认的GD图象函数库的绝佳替代方案。从安全性和易用性来说,在PHP中使用MagickWand比使用命令行ImageMagick要安全快捷的多。另外 [http://pecl.php.net/package/imagick imagick]也可用于PHP中作为ImageMagick的替代方案。 | ||
MagickWand 有两种形式, | MagickWand 有两种形式, | ||
| − | # | + | #只是建立与ImageMagick的交互,这种情况必须先安装ImageMagick,优点是扩展程序文件小(一般几百K),可以快速升级ImageMagick版本,不过WINDOWS临时目录需要特殊权限。 |
| − | # | + | #扩展程序本身包含了ImageMagick,优点是不需要额外安装ImageMagick,临时目录不需要特殊权限,但扩展程序文件比较大(一般4M以上),。 |
MagickWand 默认并没有在PHP的安装包里,需要去PHP网站上下载,并在PHP.INI中打开此扩展。 | MagickWand 默认并没有在PHP的安装包里,需要去PHP网站上下载,并在PHP.INI中打开此扩展。 | ||
| − | + | ||
| + | == 代码示例 == | ||
| + | |||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower.jpg -quality 80% flower_quality.jpg | ||
| + | $resource = NewMagickWand(); | ||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | MagickSetFormat($resource, 'JPG'); | ||
| + | MagickSetImageCompression($resource, MW_JPEGCompression); | ||
| + | MagickSetImageCompressionQuality($resource, 80.0); | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | '''在图片上写文字:''' | ||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 Flower flower_annotate1.jpg | ||
| + | |||
| + | $resource = NewMagickWand(); | ||
| + | $dwand = NewDrawingWand(); | ||
| + | $pwand = NewPixelWand(); | ||
| + | |||
| + | PixelSetColor($pwand, "white"); | ||
| + | DrawSetFont($dwand, "/usr/share/fonts/default/TrueType/cour.ttf");//指定字体 | ||
| + | DrawSetFontSize($dwand, 20); | ||
| + | DrawSetFillColor($dwand, $pwand); | ||
| + | |||
| + | DrawSetGravity($dwand, MW_SouthGravity); | ||
| + | |||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | |||
| + | if( MagickAnnotateImage( $resource, $dwand, 0, 0, 0, "Flower" ) ){ | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | }else{ | ||
| + | echo MagickGetExceptionString($resource); | ||
| + | } | ||
| + | |||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | '''裁切:''' | ||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower.jpg -crop 128×128+50+50 flower_crop.jpg | ||
| + | $resource = NewMagickWand(); | ||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | if( MagickCropImage( $resource, 128, 128, 50, 50 ) ){ | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | }else{ | ||
| + | echo MagickGetExceptionString($resource); | ||
| + | } | ||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | '''旋转图片:''' | ||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower.jpg -rotate 45 flower_rotate45.jpg | ||
| + | $resource = NewMagickWand(); | ||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | MagickRotateImage( $resource, null, 45 ); | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | '''调整大小:''' | ||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower_original.jpg -resize 640×480 flower.jpg | ||
| + | $resource = NewMagickWand(); | ||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | MagickResizeImage( $resource, 100, 100, MW_QuadraticFilter, 1.0 ); | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | '''锐化:''' | ||
| + | <source lang="PHP"> | ||
| + | <?php | ||
| + | // convert flower.jpg -unsharp 1.5×1.0+1.5+0.02 flower_unsharp.jpg | ||
| + | $resource = NewMagickWand(); | ||
| + | MagickReadImage( $resource, 'small_flower.jpg' ); | ||
| + | MagickUnsharpMaskImage( $resource, 1.5, 1.0, 1.5, 0.02 ); | ||
| + | header( 'Content-Type: image/gif' ); | ||
| + | MagickEchoImageBlob( $resource ); | ||
| + | ?> | ||
| + | </source> | ||
| + | |||
| + | |||
| + | == 相关链接 == | ||
*[http://www.dirk.sh/dirk/magickwand/ MagickWand For PHP (Windows+Patch)] | *[http://www.dirk.sh/dirk/magickwand/ MagickWand For PHP (Windows+Patch)] | ||
*[http://www.magickwand.org/ MagickWand For PHP Manual] | *[http://www.magickwand.org/ MagickWand For PHP Manual] | ||
| − | |||
[[Category:ImageMagick]][[Category:PHP Extensions]] | [[Category:ImageMagick]][[Category:PHP Extensions]] | ||
2011-01-18T11:45:22的最后版本
简介
MagickWand 是PHP的一个扩展程序,通过它建立起与ImageMagick的交互,进行图片的处理。它是默认的GD图象函数库的绝佳替代方案。从安全性和易用性来说,在PHP中使用MagickWand比使用命令行ImageMagick要安全快捷的多。另外 imagick也可用于PHP中作为ImageMagick的替代方案。
MagickWand 有两种形式,
- 只是建立与ImageMagick的交互,这种情况必须先安装ImageMagick,优点是扩展程序文件小(一般几百K),可以快速升级ImageMagick版本,不过WINDOWS临时目录需要特殊权限。
- 扩展程序本身包含了ImageMagick,优点是不需要额外安装ImageMagick,临时目录不需要特殊权限,但扩展程序文件比较大(一般4M以上),。
MagickWand 默认并没有在PHP的安装包里,需要去PHP网站上下载,并在PHP.INI中打开此扩展。
代码示例
<?php // convert flower.jpg -quality 80% flower_quality.jpg $resource = NewMagickWand(); MagickReadImage( $resource, 'small_flower.jpg' ); MagickSetFormat($resource, 'JPG'); MagickSetImageCompression($resource, MW_JPEGCompression); MagickSetImageCompressionQuality($resource, 80.0); header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); ?>
在图片上写文字:
<?php // convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 Flower flower_annotate1.jpg $resource = NewMagickWand(); $dwand = NewDrawingWand(); $pwand = NewPixelWand(); PixelSetColor($pwand, "white"); DrawSetFont($dwand, "/usr/share/fonts/default/TrueType/cour.ttf");//指定字体 DrawSetFontSize($dwand, 20); DrawSetFillColor($dwand, $pwand); DrawSetGravity($dwand, MW_SouthGravity); MagickReadImage( $resource, 'small_flower.jpg' ); if( MagickAnnotateImage( $resource, $dwand, 0, 0, 0, "Flower" ) ){ header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); }else{ echo MagickGetExceptionString($resource); } ?>
裁切:
<?php // convert flower.jpg -crop 128×128+50+50 flower_crop.jpg $resource = NewMagickWand(); MagickReadImage( $resource, 'small_flower.jpg' ); if( MagickCropImage( $resource, 128, 128, 50, 50 ) ){ header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); }else{ echo MagickGetExceptionString($resource); } ?>
旋转图片:
<?php // convert flower.jpg -rotate 45 flower_rotate45.jpg $resource = NewMagickWand(); MagickReadImage( $resource, 'small_flower.jpg' ); MagickRotateImage( $resource, null, 45 ); header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); ?>
调整大小:
<?php // convert flower_original.jpg -resize 640×480 flower.jpg $resource = NewMagickWand(); MagickReadImage( $resource, 'small_flower.jpg' ); MagickResizeImage( $resource, 100, 100, MW_QuadraticFilter, 1.0 ); header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); ?>
锐化:
<?php // convert flower.jpg -unsharp 1.5×1.0+1.5+0.02 flower_unsharp.jpg $resource = NewMagickWand(); MagickReadImage( $resource, 'small_flower.jpg' ); MagickUnsharpMaskImage( $resource, 1.5, 1.0, 1.5, 0.02 ); header( 'Content-Type: image/gif' ); MagickEchoImageBlob( $resource ); ?>