Welcome

首页 / 网页编程 / PHP / 利用php_imagick实现复古效果的方法

介绍
php_imagick是一个可以供PHP调用ImageMagick功能的PHP扩展,使用这个扩展可以使PHP具备和ImageMagick相同的功能。
ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式 的图片文件,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。

php_imagick程序示例
1.创建一个缩略图并显示出来
<?phpheader("Content-type: image/jpeg");$image = new Imagick("image.jpg");// If 0 is provided as a width or height parameter,// aspect ratio is maintained$image->thumbnailImage(100, 0);echo $image;?>
2.创建一个目录下的缩略图,并保存
<?php$images = new Imagick(glob("images/*.JPG"));foreach($images as $image) {// Providing 0 forces thumbnailImage to maintain aspect ratio$image->thumbnailImage(1024,0);}$images->writeImages();?>
3.缩略GIF动画图片
<?php/* Create a new imagick object and read in GIF */$im = new Imagick("example.gif");/* Resize all frames */foreach ($im as $frame) {/* 50x50 frames */$frame->thumbnailImage(50, 50);/* Set the virtual canvas to correct size */$frame->setImagePage(50, 50, 0, 0);}/* Notice writeImages instead of writeImage */$im->writeImages("example_small.gif", true);?>
利用php_imagick实现复古效果的方法
先来看下效果图


复古效果展示
要实现以上效果,我们先用Photoshop用以下步骤实现。
打开原图

新建图层,使用颜色#C0FFFF填充后,不透明度设为44%,图层混合模式为柔光

新建图层,使用颜色#000699填充后,不透明设置为48%,图层混合模式为排除

合并图层

用PHP代码,也就只需要按照以上步骤实现即可,代码如下:

//打开图片$im = new Imagick("./hebe.jpg");//新建图层,使用颜色`#C0FFFF`填充后,不透明度设为`44%`$layer = new Imagick();$layer->newImage($im->getImageWidth(), $im->getImageHeight(), "#C0FFFF");$layer->setImageOpacity (0.44);//叠加到原图上,图层混合模式为`柔光`$im->compositeImage($layer, Imagick::COMPOSITE_SOFTLIGHT, 0, 0);//新建图层,使用颜色`#000699`填充后,不透明设置为`48%`$layer = new Imagick();$layer->newImage($im->getImageWidth(), $im->getImageHeight(), "#000699");$layer->setImageOpacity (0.48);//叠加到原图上,图层混合模式为`排除` $im->compositeImage($layer, Imagick::COMPOSITE_EXCLUSION, 0, 0);//完成!$im->writeImage("./vintage.jpg");
总结
以上就是利用php_imagick实现复古效果的方法,文章通过示例代码介绍的还是很详细的,感兴趣的朋友们自己敲敲代码更能方便理解,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。