php_imagick完成图片剪切、旋转、锐化、减色或充实特效的方法_php技巧_脚本之家

复制代码 代码如下:public function size {
$this->w = $width; $this->h = $height; return $this; }

1.创建一个缩略图并显示出来复制代码
代码如下:thumbnailImage; echo $image;
?>2.创建一个目录下的缩略图,并保存复制代码 代码如下:thumbnailImage; }
$images->writeImages(); ?>3.缩略GIF动画图片复制代码 代码如下:thumbnailImage; /* Set the
virtual canvas to correct size */ $frame->setImagePage; }/* Notice
writeImages instead of writeImage */
$im->writeImages(“example_small.gif”, true);
?>现在我们进入正题吧,

PHP 使用Imagick模块 缩放,裁剪,压缩图片 包括gif图片

注意提交的URL” method=”post”>

复制代码 代码如下: public function size {
$this->w = $width; $this->h = $height; }

extension=php_imagick.dll ,重启web server

保存时压缩处理

中间可以自己定义图片的二维码生成器

复制代码
代码如下://按这个类的编写方式。。使用方法应该是这样

windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll

  • $crop_w;$crop_y = $src_height – $crop_h;break;default :$crop_x =
    intval ( / 2 );$crop_y = intval ( ($src_height – $crop_h) / 2
    );}$image = $this->image;$canvas = new Imagick ();if {$images =
    $image->coalesceImages ();foreach {$img = new Imagick
    ();$img->readImageBlob ;$img->cropImage ( $crop_w, $crop_h,
    $crop_x, $crop_y );$img->thumbnailImage ( $width, $height, true
    );$canvas->addImage ;$canvas->setImageDelay (
    $img->getImageDelay ;$canvas->setImagePage ( $width, $height, 0, 0
    );}} else {$image->cropImage ( $crop_w, $crop_h, $crop_x, $crop_y
    );$image->thumbnailImage ( $width, $height, true
    );$canvas->addImage ;$canvas->setImagePage ( $width, $height, 0, 0
    );}$image->destroy ();$this->image = $canvas;}}//
    添加水印图片public function add_watermark($path, $x = 0, $y = 0)
    {$watermark = new Imagick ;$draw = new ImagickDraw
    ();$draw->composite ( $watermark->getImageCompose (), $x, $y,
    $watermark->getImageWidth (), $watermark->getimageheight ;if
    {$image = $this->image;$canvas = new Imagick ();$images =
    $image->coalesceImages ();foreach {$img = new Imagick
    ();$img->readImageBlob ;$img->drawImage ;$canvas->addImage
    ;$canvas->setImageDelay ( $img->getImageDelay ;}$image->destroy
    ();$this->image = $canvas;} else {$this->image->drawImage ;}}//
    添加水印文字public function add_text($text, $x = 0, $y = 0, $angle = 0,
    $style = array {$draw = new ImagickDraw ();if (isset $draw->setFont
    ;if (isset ( $style [‘font_size’] ))$draw->setFontSize ( $style
    [‘font_size’] );if (isset ( $style [‘fill_color’]
    ))$draw->setFillColor ( $style [‘fill_color’] );if (isset ( $style
    [‘under_color’] ))$draw->setTextUnderColor ( $style
    [‘under_color’] );if {foreach ( $this->image as $frame )
    {$frame->annotateImage ( $draw, $x, $y, $angle, $text );}} else
    {$this->image->annotateImage ( $draw, $x, $y, $angle, $text );}}//
    保存到指定路径public function save_to
    {//压缩图片质量$this->image->setImageFormat;$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);$a
    = $this->image->getImageCompressionQuality {$a =
    60;}$this->image->setImageCompressionQuality;$this->image->stripImage();if
    {$this->image->writeImages ;} else {$this->image->writeImage
    ;}}// 输出图像public function output {if header ( ‘Content-type: ‘ .
    $this->type );echo $this->image->getImagesBlob ();}public
    function get_width() {$size = $this->image->getImagePage
    ();return $size [‘width’];}public function get_height() {$size =
    $this->image->getImagePage ();return $size [‘height’];}//
    设置图像类型, 默认与源类型一致public function set_type {$this->type
    = $type;$this->image->setImageFormat ;}// 获取源图像类型public
    function get_type() {return $this->type;}public function
    get_file_size{return 0;//$this->image->getImageLength();
    getImageLength not find}else{return 0;}}public function
    get_file_type{return
    $this->image->getimagemimetype();}else{return 0;}}public function
    get_sha1{return sha1($this->image->__tostring;}else{return
    ”;}}// 当前对象是否为图片public function is_imagereturn
    true;elsereturn false;}/* * 添加一个边框 $width: 左右边框宽度 $height:
    上下边框宽度 $color: 颜色: RGB 颜色 ‘rgb’ 或 16进制颜色 ‘#FF0000’
    或颜色单词 ‘white’/’red’… */public function border($width, $height,
    $color = ‘rgb {$color = new ImagickPixel ();$color->setColor
    ;$this->image->borderImage ( $color, $width, $height );}public
    function blur {$this->image->blurImage ;} // 模糊public function
    gaussian_blur {$this->image->gaussianBlurImage ;} //
    高斯模糊public function motion_blur($radius, $sigma, $angle)
    {$this->image->motionBlurImage ( $radius, $sigma, $angle );} //
    运动模糊public function radial_blur
    {$this->image->radialBlurImage ;} // 径向模糊public function
    add_noise {$this->image->addNoiseImage ( $type == null ?
    imagick::NOISE_IMPULSE : $type );} // 添加噪点public function
    level($black_point, $gamma, $white_point)
    {$this->image->levelImage ( $black_point, $gamma, $white_point
    );} // 调整色阶public function modulate($brightness, $saturation, $hue)
    {$this->image->modulateImage ( $brightness, $saturation, $hue );}
    // 调整亮度、饱和度、色调public function charcoal
    {$this->image->charcoalImage ;} // 素描public function oil_paint
    {$this->image->oilPaintImage ;} // 油画效果public function flop()
    {$this->image->flopImage ();} // 水平翻转public function flip()
    {$this->image->flipImage ();} // 垂直翻转}

中间可以自己定义图片的二维码生成器

二维码要生的内容:

希望能添加自己的图片地址:

$simg = new simg(); //一开始是空的echo $simg->width; //肯定是
0$simg->load; //以一张图为基准echo $simg->width;
//没改过,是原图大小

一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。

复制代码 代码如下:// 保存到指定路径public
function save_to
{//压缩图片质量$this->image->setImageFormat;$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);$a
= $this->image->getImageCompressionQuality {$a =
60;}$this->image->setImageCompressionQuality;$this->image->stripImage();if
{$this->image->writeImages ;} else {$this->image->writeImage
;}}

1,首先你必须生成二维码具体代码如下: 复制代码 代码如下: class QRCode{ public $w;
public $h; public $s; function __construct{ $this->w = $w1;
$this->h = $h1; $this->s = $s1; $this->outimgase(); } function
qrcode(){ $post_data = array(); $post_data[‘cht’] = ‘qr’;
$post_data[‘chs’] = $this->w.”x”.$this->h; $post_data[‘chl’]
= $this->s; $post_data[‘choe’] = “UTF-8”; $url =
“”; $data_Array = array();
foreach($post_data as $key => $value) { $data_Array[] =
$key.’=’.$value; } $data = implode; $ch = curl_init(); curl_setopt;
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,
$url); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch,
CURLOPT_RETURNTRANSFER, 1); $result = curl_exec; return $result; }
function outimgase(){ echo $this->qrcode(); } }
header(“Content-type:image/png”); $t = new QRCode;
2,然后通过一个php文件将二维码和你的目的图片画在一起代码如下: 复制代码
代码如下: $surl = $_POST[“url”]; function GrabImage { if:return
false;endif; if { $ext=strrchr; if($ext!=”.gif” && $ext!=”.jpg”):return
false;endif; $filename=date.$ext; } ob_start; $img = ob_get_contents;
$size = strlen; $fp2=@fopen; fwrite; fclose; return $filename; } $source
= GrabImage(“”); $water
=GrabImage; function getImageInfo{ $imageInfo = getimagesize; if {
$imageType = strtolower(substr(image_type_to_extension; $imageSize =
filesize; $info = array( “width” => $imageInfo[0], “height” =>
$imageInfo[1], “type” => $imageType, “size” => $imageSize,
“mime” => $imageInfo[‘mime’] ); return $info; } else { return
false; } } function thumb($image, $thumbname, $type=”, $maxWidth=200,
$maxHeight=50, $interlace=true) { // 获取原图信息 $info = getImageInfo;
if { $srcWidth = $info[‘width’]; $srcHeight = $info[‘height’]; $type
= empty ? $info[‘type’] : $type; $type = strtolower; $interlace =
$interlace ? 1 : 0; unset; $scale = min($maxWidth / $srcWidth,
$maxHeight / $srcHeight); // 计算缩放比例 if { // 超过原图大小不再缩略
$width = $srcWidth; $height = $srcHeight; } else { // 缩略图尺寸 $width
= ; $height = ; } // 载入原图 $createFun = ‘ImageCreateFrom’ . ($type ==
‘jpg’ ? ‘jpeg’ : $type); $srcImg = $createFun; //创建缩略图 if ($type !=
‘gif’ && function_exists(‘imagecreatetruecolor’)) $thumbImg =
imagecreatetruecolor; else $thumbImg = imagecreate; // 复制图片 if
(function_exists) imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0,
$width, $height, $srcWidth, $srcHeight); else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,
$srcWidth, $srcHeight); if (‘gif’ == $type || ‘png’ == $type) {
//imagealphablending;//取消默认的混色模式
//imagesavealpha;//设定保存完整的 alpha 通道信息 $background_color =
imagecolorallocate; // 指派一个绿色 imagecolortransparent($thumbImg,
$background_color); // 设置为透明色,若注释掉该行则输出绿色的图 } //
对jpeg图形设置隔行扫描 if (‘jpg’ == $type || ‘jpeg’ == $type)
imageinterlace($thumbImg, $interlace); // 生成图片 $imageFun = ‘image’ .
($type == ‘jpg’ ? ‘jpeg’ : $type); $imageFun($thumbImg, $thumbname);
imagedestroy; imagedestroy; return $thumbname; } return false; }
function water($source, $thumb, $savename=””, $alpha=100){
//检查文件是否存在 if || !file_exists return false; //图片信息 $sInfo =
getImageInfo; $water =
thumb($thumb,”wy.jpg”,”jpg”,$sInfo[“width”]/4,$sInfo[“height”]/4);
$wInfo = getImageInfo; //如果图片小于水印图片,不生成图片 if
($sInfo[“width”] < $wInfo[“width”] || $sInfo[‘height’] <
$wInfo[‘height’]) return false; //建立图像 $sCreateFun =
“imagecreatefrom” . $sInfo[‘type’]; $sImage = $sCreateFun; $wCreateFun
= “imagecreatefrom” . $wInfo[‘type’]; $wImage = $wCreateFun;
//设定图像的混色模式 imagealphablending; //图像位置,默认为右下角右对齐
// $posY = $sInfo[“height”] – $wInfo[“height”]; // $posX =
$sInfo[“width”] – $wInfo[“width”]; $posY = ($sInfo[“height”] –
$wInfo[“height”])/2; $posX = ($sInfo[“width”] –
$wInfo[“width”])/2; //生成混合图像 imagecopymerge($sImage, $wImage,
$posX, $posY, 0, 0, $wInfo[‘width’], $wInfo[‘height’], $alpha);
//输出图像 $ImageFun = ‘Image’ . $sInfo[‘type’];
//如果没有给出保存文件名,默认为原图像名 if { $savename = $source;
@unlink; } //保存图像 $ImageFun; imagedestroy; } water;
在上面的代码中用3个函数 GrabImage()函数是将生成二维码的文件转化成图片
接下来的函数就是处理图片的缩放 将目的图片添加到二位上。
3,在来一个入口文件index.html 代码如下: 复制代码 代码如下:

复制代码 代码如下:1 $simg = new simg;2
echo $simg->width;3 echo $simg->height;4 echo $simg->type;5
//这个对象,应该可以这样操作。

2、下载php_imagick.dll

缩放 裁剪

那怎么开始设计一个合格的类呢,一开始就写class{}的都错了,正确的是什么都不写,而是假设这个类已经存在,这个对象已经存在,各种属性方法都已经有了,在这个完全的假设下想象下这个对象应该怎么用,例如我们制作一个缩略图的类,我们希望封装成一个类,方便下次使用,我们首先需要明确对象是什么它会做什么,要制作缩略图本质操作是缩小图片并输出,这里被操作的是图片,那么对象就是图片,由于网站上的图片不是唯一的我们得告诉这是那张图片,这就可以假设下这个类已经存在,一开始就得声明是那张图片,例如$simg
= new simg;那么,一张图片应该有哪些属性?
在制作缩略图的时候,我们最关心的应该是
宽,高,类型,而且这三项对一张图片而言是肯定的,这意味着这个对象一定有这些属性,$simg->width,$simg->height,$simg->type,并且这些属性一开始就可以读取的到的,

在php.ini中添加

针对gif图片的处理方法

之后再根据对每个方法每个属性的要求,填上里面的代码,一开始就必须知道文件的高度,宽度,由于php处理不同类型的图片使用的函数不同,我们在这里不得不知道文件类型是多少。以决定用哪个函数
设计类的时候,才是思考“怎么做”的时候,要在实例化之后,马上知道宽高。一定是在构造函数里完成的,只有构造函数会在类实例化的时候执行,这里我们可以使用
getimagesize 函数,取得文件的宽度,高度,类型
,宽度和高度,我们可以在这里,赋值给属性。这样一来,实例化图片,就得到属性的问题就解决了,那改变大小的过程呢?

3.安装imagick扩展01.wget
xzvf
imagick-3.1.0RC2.tgz03.cd
imagick-3.1.0RC204.phpize05../configure06.make07.make install

复制代码 代码如下:/** * 图片裁剪 *
裁剪规则: * 1. 高度为空或为零 按宽度缩放 高度自适应 * 2.
宽度为空或为零 按高度缩放 宽度自适应 * 3. 宽度,高度到不为空或为零
按宽高比例等比例缩放裁剪 默认从头部居中裁剪 * @param number $width *
@param number $height */public function resize{if($width==0 &&
$height==0){return;}$color = ”;// ‘rgba’;$size =
$this->image->getImagePage ();//原始宽高$src_width = $size
[‘width’];$src_height = $size [‘height’];//按宽度缩放
高度自适应if($width!=0 && $height==0){if{$height =
intval($width*$src_height/$src_width);if
{$this->_resizeGif;}else{$this->image->thumbnailImage (
$width, $height, true );}}return;}//按高度缩放 宽度自适应if($width==0 &&
$height!=0){if{$width = intval($src_width*$height/$src_height);if
{$this->_resizeGif;}else{$this->image->thumbnailImage (
$width, $height, true );}}return;}//缩放的后的尺寸$crop_w =
$width;$crop_h = $height;//缩放后裁剪的位置$crop_x = 0;$crop_y =
0;if(($src_width/$src_height) < {//宽高比例小于目标宽高比例
宽度等比例放大 按目标高度从头部截取$crop_h =
intval($src_height*$width/$src_width);//从顶部裁剪 不用计算
$crop_y}else{//宽高比例大于目标宽高比例 高度等比例放大
按目标宽度居中裁剪$crop_w =
intval($src_width*$height/$src_height);$crop_x = intval;}if
{$this->_resizeGif($crop_w, $crop_h, true, $width,
$height,$crop_x, $crop_y);} else {$this->image->thumbnailImage (
$crop_w, $crop_h, true );$this->image->cropImage($width,
$height,$crop_x, $crop_y);}}

//设置大小$simg->size;//保存到$simg->save;

裁切/生成缩略图/添加水印, 自动检测和处理 GIF

/*** 图片处理服务类* 使用php扩展服务Imagick实现* ImageMagick
官网地址 [url]http:www.imagemagick.org/script/index.php[/url] * *
@author weiguang3* @since 20140403*/class ImagickService {private
$image = null;private $type = null;// 构造函数public function
__construct() {}// 析构函数public function __destruct() {if
($this->image !== null)$this->image->destroy ();}public
function init(){}// 载入图像public function open {$this->image = new
Imagick ;if {$this->type = strtolower (
$this->image->getImageFormat ;}return $this->image;}/** *
图片裁剪 * 裁剪规则: * 1. 高度为空或为零 按宽度缩放 高度自适应 * 2.
宽度为空或为零 按高度缩放 宽度自适应 * 3. 宽度,高度到不为空或为零
按宽高比例等比例缩放裁剪 默认从头部居中裁剪 * @param number $width *
@param number $height */public function resize{if($width==0 &&
$height==0){return;}$color = ”;// ‘rgba’;$size =
$this->image->getImagePage ();//原始宽高$src_width = $size
[‘width’];$src_height = $size [‘height’];//按宽度缩放
高度自适应if($width!=0 && $height==0){if{$height =
intval($width*$src_height/$src_width);if
{$this->_resizeGif;}else{$this->image->thumbnailImage (
$width, $height, true );}}return;}//按高度缩放 宽度自适应if($width==0 &&
$height!=0){if{$width = intval($src_width*$height/$src_height);if
{$this->_resizeGif;}else{$this->image->thumbnailImage (
$width, $height, true );}}return;}//缩放的后的尺寸$crop_w =
$width;$crop_h = $height;//缩放后裁剪的位置$crop_x = 0;$crop_y =
0;if(($src_width/$src_height) < {//宽高比例小于目标宽高比例
宽度等比例放大 按目标高度从头部截取$crop_h =
intval($src_height*$width/$src_width);//从顶部裁剪 不用计算
$crop_y}else{//宽高比例大于目标宽高比例 高度等比例放大
按目标宽度居中裁剪$crop_w =
intval($src_width*$height/$src_height);$crop_x = intval;}if
{$this->_resizeGif($crop_w, $crop_h, true, $width,
$height,$crop_x, $crop_y);} else {$this->image->thumbnailImage (
$crop_w, $crop_h, true );$this->image->cropImage($width,
$height,$crop_x, $crop_y);}}/** * 处理gif图片 需要对每一帧图片处理
* @param unknown $t_w 缩放宽 * @param unknown $t_h 缩放高 * @param
string $isCrop 是否裁剪 * @param number $c_w 裁剪宽 * @param number
$c_h 裁剪高 * @param number $c_x 裁剪坐标 x * @param number $c_y
裁剪坐标 y */private function _resizeGif($t_w, $t_h, $isCrop=false,
$c_w=0, $c_h=0, $c_x=0, $c_y=0){$dest = new
Imagick();$color_transparent = new ImagickPixel;//透明色foreach{$page =
$img->getImagePage();$tmp = new
Imagick();$tmp->newImage($page[‘width’], $page[‘height’],
$color_transparent, ‘gif’);$tmp->compositeImage($img,
Imagick::COMPOSITE_OVER, $page[‘x’],
$page[‘y’]);$tmp->thumbnailImage ;if{$tmp->cropImage($c_w,
$c_h, $c_x, $c_y);}$dest->addImage;$dest->setImagePage,
$tmp->getImageHeight;$dest->setImageDelay);$dest->setImageDispose($img->getImageDispose;}$this->image->destroy
();$this->image = $dest;}/** * 更改图像大小 * $fit: 适应大小方式
* ‘force’: 把图片强制变形成 $width X $height 大小 * ‘scale’:
按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小
不完全等于 $width X $height * ‘scale_fill’: 按比例在安全框 $width X
$height 内缩放图片,安全框内没有像素的地方填充色, *
使用此参数时可设置背景填充色 $bg_color = array * 透明度 其它: 智能模能
缩放图像并载取图像的中间部分 $width X $height 像素大小 * $fit =
‘force’,’scale’,’scale_fill’ 时: 输出完整图像 * $fit = 图像方位值 时,
输出指定位置部分图像 字母与图像的对应关系如下: * north_west north
north_east * west center east * south_west south south_east
*/public function resize_to($width = 100, $height = 100, $fit =
‘center’, $fill_color = array {switch {case ‘force’ :if {$image =
$this->image;$canvas = new Imagick ();$images =
$image->coalesceImages ();foreach {$img = new Imagick
();$img->readImageBlob ;$img->thumbnailImage ( $width, $height,
false );$canvas->addImage ;$canvas->setImageDelay (
$img->getImageDelay ;}$image->destroy ();$this->image =
$canvas;} else {$this->image->thumbnailImage ( $width, $height,
false );}break;case ‘scale’ :if {$image = $this->image;$images =
$image->coalesceImages ();$canvas = new Imagick ();foreach {$img =
new Imagick ();$img->readImageBlob ;$img->thumbnailImage ( $width,
$height, true );$canvas->addImage ;$canvas->setImageDelay (
$img->getImageDelay ;}$image->destroy ();$this->image =
$canvas;} else {$this->image->thumbnailImage ( $width, $height,
true );}break;case ‘scale_fill’ :$size =
$this->image->getImagePage ();$src_width = $size
[‘width’];$src_height = $size [‘height’];$x = 0;$y = 0;$dst_width
= $width;$dst_height = $height;if ($src_width * $height >
$src_height * $width) {$dst_height = intval ( $width * $src_height
/ $src_width );$y = intval ( ($height – $dst_height) / 2 );} else
{$dst_width = intval ( $height * $src_width / $src_height );$x =
intval ( / 2 );}$image = $this->image;$canvas = new Imagick ();$color
= ‘rgba(‘ . $fill_color [0] . ‘,’ . $fill_color [1] . ‘,’ .
$fill_color [2] . ‘,’ . $fill_color [3] . ‘)’;if {$images =
$image->coalesceImages ();foreach {$frame->thumbnailImage (
$width, $height, true );$draw = new ImagickDraw ();$draw->composite (
$frame->getImageCompose (), $x, $y, $dst_width, $dst_height, $frame
);$img = new Imagick ();$img->newImage ( $width, $height, $color,
‘gif’ );$img->drawImage ;$canvas->addImage
;$canvas->setImageDelay ( $img->getImageDelay
;$canvas->setImagePage ( $width, $height, 0, 0 );}} else
{$image->thumbnailImage ( $width, $height, true );$draw = new
ImagickDraw ();$draw->composite ( $image->getImageCompose (), $x,
$y, $dst_width, $dst_height, $image );$canvas->newImage ( $width,
$height, $color, $this->get_type ;$canvas->drawImage
;$canvas->setImagePage ( $width, $height, 0, 0 );}$image->destroy
();$this->image = $canvas;break;default :$size =
$this->image->getImagePage ();$src_width = $size
[‘width’];$src_height = $size [‘height’];$crop_x = 0;$crop_y =
0;$crop_w = $src_width;$crop_h = $src_height;if ($src_width *
$height > $src_height * $width) {$crop_w = intval ( $src_height
* $width / $height );} else {$crop_h = intval ( $src_width * $height
/ $width );}switch {case ‘north_west’ :$crop_x = 0;$crop_y =
0;break;case ‘north’ :$crop_x = intval ( / 2 );$crop_y = 0;break;case
‘north_east’ :$crop_x = $src_width – $crop_w;$crop_y = 0;break;case
‘west’ :$crop_x = 0;$crop_y = intval ( ($src_height – $crop_h) / 2
);break;case ‘center’ :$crop_x = intval ( / 2 );$crop_y = intval (
($src_height – $crop_h) / 2 );break;case ‘east’ :$crop_x =
$src_width – $crop_w;$crop_y = intval ( ($src_height – $crop_h) / 2
);break;case ‘south_west’ :$crop_x = 0;$crop_y = $src_height –
$crop_h;break;case ‘south’ :$crop_x = intval ( / 2 );$crop_y =
$src_height – $crop_h;break;case ‘south_east’ :$crop_x = $src_width

发表评论

电子邮件地址不会被公开。 必填项已用*标注