Imagick使用教程
php Imagick 使用教程
ImagickHelper.php:
<?php namespace App\Lib; class ImagickHelper { /** * 创建一张透明图片 * @param [int] $width [要创建的图片宽度] * @param [int] $height [要创建的图片高度] * @return [image] [透明图片] */ public static function createTransparentImage($width, $height, $format = 'png') { $image = new \Imagick(); $image->newImage($width, $height,"none"); $image->setImageFormat($format); return $image; } /** * 创建一个带背景的图片 * @param [int] $width [图片宽度] * @param [int] $height [图片高度] * @param [object] $template [模板对象] * @return [imagick] [图像资源] */ public static function createBackgroundImage($width, $height, $bg_img_path, $format = 'png') { $imagick = new \Imagick($bg_img_path); $imagick->adaptiveResizeImage($width,$height);//使用数据相关三角测量自适应调整图像大小 $imagick->setImageFormat($format); return $imagick; } /** * 创建一个或一组ico图片 * @param [string] $src_img [原图片路径] * @param string $filename [保存的路径包括文件名称,默认当前路径,名称favicon.ico] * @param boolean $adjoin [保存类型,true保存一个,false保存一组] * @return [type] [description] */ public static function createIcoImage($src_img, $filename = "favicon.ico", $adjoin = true) { $src_img = new \Imagick($src_img); $icon = new \Imagick(); $geo = $src_img->getImageGeometry(); $size_w = $geo['width']; $size_h = $geo['height']; $icon->setFormat("ico"); if (128/$size_w*$size_h>128) { $src_img->scaleImage(128,0); } else { $src_img->scaleImage(0,128); } $src_img->cropImage(128, 128, 0, 0); $clone = clone $src_img; $clone->scaleImage(16,0); $icon->addImage($clone); $clone = clone $src_img; $clone->scaleImage(32,0); $icon->addImage($clone); $clone = clone $src_img; $clone->scaleImage(64,0); $icon->addImage($clone); $clone = clone $src_img; $clone->scaleImage(128,0); $icon->addImage($clone); if($icon->writeImages($filename, $adjoin)){ $src_img->destroy(); $icon->destroy(); $clone->destroy(); return true; }else{ $src_img->destroy(); $icon->destroy(); $clone->destroy(); return true; } } /** * 截取图片的一块区域 * @param [string] $image_path [图片的路径] * @param [int] $start_x [裁剪区域左上角的 X 轴坐标(以原图的左上角为原点)] * @param [int] $start_y [裁剪区域左上角的 X 轴坐标(以原图的左上角为原点)] * @param [int] $width [截图的宽度] * @param [int] $height [截图的高度] * @param [string] $format [保存格式] * @return [imagick] [图片资源] */ public static function cropImage($image_path, $start_x, $start_y, $width, $height, $format = 'png') { $image_path = self::url_to_realpath($image_path); $imagick = new \Imagick(realpath($image_path)); $imagick->cropImage($width, $height, $start_x, $start_y); $imagick->setImageFormat($format); return $imagick; } /** * 网址转化成绝对地址 * @param [string] $url [网址] * @return [string] [转换后的绝对地址] */ public static function url_to_realpath($url) { if(preg_match('/^(http:\/\/)|^(https:\/\/)/i', $url)){ $url = parse_url($url)['path']; $url = ltrim($url, '/'); $url = realpath($url); } return $url; } /** * 缩放一张图片 * @param [string] $image_path [图片的路径] * @param [int] $width [缩放后的宽度] * @param [int] $height [缩放后的高度] * @param [boolean] $opinion [是否锁定纵横比] * @return [imagick] [图片资源] */ public static function ThumbnailImage($image_path, $width, $height, $opinion = false) { $image_path = self::url_to_realpath($image_path); $image = new \Imagick($image_path); $image->ThumbnailImage($width, $height, $opinion); return $image; } /** * 根据布局数据和缩放比例返回一个布局的缩略图 * * @author linyan * @param [array] $layout [布局的数组数据] * @param [resource] $imagick [透明的图像资源] * @param [float] $scaling [缩放比例] * @return [resource] [缩略图] */ private static function drawRegions($layout, $imagick, $scaling) { $width = $imagick->getImageWidth(); $height = $imagick->getImageHeight(); $scale_width = ceil($width * $scaling); //向上舍入接近整数 $scale_height = ceil($height * $scaling); foreach ($layout as $key => $region) { //判断是否有背景颜色 if (array_has($region, 'backgroundColor')) { //为图像分配颜色 $fillColor = new \ImagickPixel($region['backgroundColor']); } else { //为图像分配颜色 $fillColor = new \ImagickPixel("rgb(155, 255, 255)"); } //画一矩形并填充 $draw = new \ImagickDraw(); $draw->setFillColor($fillColor);//填充颜色 $draw->rectangle($region['left'], $region['top'], $region['left'] + $region['width'], $region['top'] + $region['height']); $imagick->setImageFormat("png"); $imagick->drawImage($draw); } $imagick->cropThumbnailImage($scale_width, $scale_height); //缩放后,向区域中增加图标显示 foreach ($layout as $key => $region) { $icon_img_path = sprintf("images/icon/%s.png", $region['type']); if (file_exists($icon_img_path)) { $imagick_img = new \Imagick($icon_img_path); $icon_width = $imagick_img->getImageWidth(); $icon_height = $imagick_img->getImageHeight(); $scale_top = $region['top'] * $scaling + $region['height'] * $scaling / 2 - $icon_height / 2; $scale_left = $region['left'] * $scaling + $region['width'] * $scaling / 2 - $icon_width / 2; $imagick->compositeImage($imagick_img, $imagick_img->getImageCompose(), $scale_left, $scale_top); } } return $imagick; } /** * 在图片上加文字 * @param [imagick] $image [图片资源] * @param float $x [x坐标] * @param float $y [y坐标] * @param string $text [文字内容] * @return [imagick] [图片资源] */ public static function annotation($image, float $x , float $y , string $text ) { $draw = new \ImagickDraw(); $fillColor = new \ImagickPixel('#fff'); // $strokeColor = new \ImagickPixel('#000'); // $draw->setFont("fonts/msyh.ttf"); $draw->setStrokeWidth(12); $draw->setFontSize(36); // $draw->setStrokeColor($strokeColor);//设置用于描绘对象轮廓的颜色 $draw->setFillColor($fillColor);//设置用于绘制填充对象的填充颜色 $draw->setStrokeAntialias(true);//控制描边轮廓是否反锯齿 $draw->setTextAntialias(true);//控制文本是否反锯齿 @$draw->setFillAlpha(0.39);//设置不透明度 $draw->annotation($x,$y,$text); $image->drawImage($draw); return $image; } }