目錄
PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF,imagick水印
您可能感兴趣的文章:
首頁 php教程 php手册 PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF,imagick水印

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

Jun 13, 2016 am 08:45 AM
gif imagick phpcms 水印 縮圖 裁切

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

给骨头系统开发的图像库的 imagick 部分 ,支持 gif , 完美支持裁切、生成缩略图、添加水印 。

支持按方位生成缩略图像, 如:

// 把左上角优先
$image->resize_to(100, 100, 'north_west');
// 右边优先
$image->resize_to(100, 100, 'east');
...
登入後複製

更多参数看源代码

原图

效果图:

调用方式:

include 'imagick.class.php'; 
$image = new lib_image_imagick(); 
$image->open('a.gif'); 
$image->resize_to(100, 100, 'scale_fill'); 
$image->add_text('1024i.com', 10, 20); 
$image->add_watermark('1024i.gif', 10, 50); 
$image->save_to('x.gif'); 
登入後複製

imagick.class.php

<&#63;php 
/* 
@版本日期: 版本日期: 2012年1月18日 
@著作权所有: 1024 intelligence ( http://www.1024i.com ) 
获得使用本类库的许可, 您必须保留著作权声明信息. 
报告漏洞,意见或建议, 请联系 Lou Barnes(iua1024@gmail.com) 
*/ 

class lib_image_imagick 
{ 
private $image = null; 
private $type = null; 
// 构造函数 
public function __construct(){} 

// 析构函数 
public function __destruct() 
{ 
if($this->image!==null) $this->image->destroy(); 
} 
// 载入图像 
public function open($path) 
{ 
$this->image = new Imagick( $path ); 
if($this->image) 
{ 
$this->type = strtolower($this->image->getImageFormat()); 
} 
return $this->image; 
} 

public function crop($x=0, $y=0, $width=null, $height=null) 
{ 
if($width==null) $width = $this->image->getImageWidth()-$x; 
if($height==null) $height = $this->image->getImageHeight()-$y; 
if($width<=0 || $height<=0) return; 
if($this->type=='gif') 
{ 
$image = $this->image; 
$canvas = new Imagick(); 
$images = $image->coalesceImages(); 
foreach($images as $frame){ 
$img = new Imagick(); 
$img->readImageBlob($frame); 
$img->cropImage($width, $height, $x, $y); 
$canvas->addImage( $img ); 
$canvas->setImageDelay( $img->getImageDelay() ); 
$canvas->setImagePage($width, $height, 0, 0); 
} 
$image->destroy(); 
$this->image = $canvas; 
} 
else 
{ 
$this->image->cropImage($width, $height, $x, $y); 
} 
} 
/* 
* 更改图像大小 
$fit: 适应大小方式 
'force': 把图片强制变形成 $width X $height 大小 
'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height 
'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明)) 
其它: 智能模能 缩放图像并载取图像的中间部分 $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(255,255,255,0) ) 
{ 
switch($fit) 
{ 
case 'force': 
if($this->type=='gif') 
{ 
$image = $this->image; 
$canvas = new Imagick(); 
$images = $image->coalesceImages(); 
foreach($images as $frame){ 
$img = new Imagick(); 
$img->readImageBlob($frame); 
$img->thumbnailImage( $width, $height, false ); 
$canvas->addImage( $img ); 
$canvas->setImageDelay( $img->getImageDelay() ); 
} 
$image->destroy(); 
$this->image = $canvas; 
} 
else 
{ 
$this->image->thumbnailImage( $width, $height, false ); 
} 
break; 
case 'scale': 
if($this->type=='gif') 
{ 
$image = $this->image; 
$images = $image->coalesceImages(); 
$canvas = new Imagick(); 
foreach($images as $frame){ 
$img = new Imagick(); 
$img->readImageBlob($frame); 
$img->thumbnailImage( $width, $height, true ); 
$canvas->addImage( $img ); 
$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( ($width-$dst_width)/2 ); 
} 
$image = $this->image; 
$canvas = new Imagick(); 
$color = 'rgba('.$fill_color[0].','.$fill_color[1].','.$fill_color[2].','.$fill_color[3].')'; 
if($this->type=='gif') 
{ 
$images = $image->coalesceImages(); 
foreach($images as $frame) 
{ 
$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($draw); 
$canvas->addImage( $img ); 
$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($draw); 
$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($fit) 
{ 
case 'north_west': 
$crop_x = 0; 
$crop_y = 0; 
break; 
case 'north': 
$crop_x = intval( ($src_width-$crop_w)/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( ($src_width-$crop_w)/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( ($src_width-$crop_w)/2 ); 
$crop_y = $src_height-$crop_h; 
break; 
case 'south_east': 
$crop_x = $src_width-$crop_w; 
$crop_y = $src_height-$crop_h; 
break; 
default: 
$crop_x = intval( ($src_width-$crop_w)/2 ); 
$crop_y = intval( ($src_height-$crop_h)/2 ); 
} 
$image = $this->image; 
$canvas = new Imagick(); 
if($this->type=='gif') 
{ 
$images = $image->coalesceImages(); 
foreach($images as $frame){ 
$img = new Imagick(); 
$img->readImageBlob($frame); 
$img->cropImage($crop_w, $crop_h, $crop_x, $crop_y); 
$img->thumbnailImage( $width, $height, true ); 
$canvas->addImage( $img ); 
$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( $image ); 
$canvas->setImagePage($width, $height, 0, 0); 
} 
$image->destroy(); 
$this->image = $canvas; 
} 
} 


// 添加水印图片 
public function add_watermark($path, $x = 0, $y = 0) 
{ 
$watermark = new Imagick($path); 
$draw = new ImagickDraw(); 
$draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark); 
if($this->type=='gif') 
{ 
$image = $this->image; 
$canvas = new Imagick(); 
$images = $image->coalesceImages(); 
foreach($image as $frame) 
{ 
$img = new Imagick(); 
$img->readImageBlob($frame); 
$img->drawImage($draw); 
$canvas->addImage( $img ); 
$canvas->setImageDelay( $img->getImageDelay() ); 
} 
$image->destroy(); 
$this->image = $canvas; 
} 
else 
{ 
$this->image->drawImage($draw); 
} 
} 

// 添加水印文字 
public function add_text($text, $x = 0 , $y = 0, $angle=0, $style=array()) 
{ 
$draw = new ImagickDraw(); 
if(isset($style['font'])) $draw->setFont($style['font']); 
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($this->type=='gif') 
{ 
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( $path ) 
{ 
if($this->type=='gif') 
{ 
$this->image->writeImages($path, true); 
} 
else 
{ 
$this->image->writeImage($path); 
} 
} 
// 输出图像 
public function output($header = true) 
{ 
if($header) 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( $type='png' ) 
{ 
$this->type = $type; 
$this->image->setImageFormat( $type ); 
} 
// 获取源图像类型 
public function get_type() 
{ 
return $this->type; 
} 

// 当前对象是否为图片 
public function is_image() 
{ 
if( $this->image ) 
return true; 
else 
return false; 
} 

public function thumbnail($width = 100, $height = 100, $fit = true){ $this->image->thumbnailImage( $width, $height, $fit );} // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片 
/* 
添加一个边框 
$width: 左右边框宽度 
$height: 上下边框宽度 
$color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'... 
*/ 
public function border($width, $height, $color='rgb(220, 220, 220)') 
{ 
$color=new ImagickPixel(); 
$color->setColor($color); 
$this->image->borderImage($color, $width, $height); 
} 
public function blur($radius, $sigma){$this->image->blurImage($radius, $sigma);} // 模糊 
public function gaussian_blur($radius, $sigma){$this->image->gaussianBlurImage($radius, $sigma);} // 高斯模糊 
public function motion_blur($radius, $sigma, $angle){$this->image->motionBlurImage($radius, $sigma, $angle);} // 运动模糊 
public function radial_blur($radius){$this->image->radialBlurImage($radius);} // 径向模糊 
public function add_noise($type=null){$this->image->addNoiseImage($type==null&#63;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($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描 
public function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油画效果 
public function flop(){$this->image->flopImage();} // 水平翻转 
public function flip(){$this->image->flipImage();} // 垂直翻转 
}
登入後複製

您可能感兴趣的文章:

  • PHP使用imagick读取PDF生成png缩略图的两种方法
  • php使用imagick模块实现图片缩放、裁剪、压缩示例
  • PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
  • PHP中使用Imagick读取pdf并生成png缩略图实例
  • PHP中使用imagick生成PSD文件缩略图教程
  • PHP利用imagick生成组合缩略图
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Win11怎麼停用工作列縮圖預覽? 關閉滑鼠移動工作列圖示顯示縮圖技巧 Win11怎麼停用工作列縮圖預覽? 關閉滑鼠移動工作列圖示顯示縮圖技巧 Feb 29, 2024 pm 03:20 PM

本文將介紹如何在Win11系統中關閉滑鼠移動工作列圖示時顯示的縮圖功能。這項功能在預設情況下是開啟的,當使用者將滑鼠指標懸停在工作列上的應用程式圖示上時,會顯示該應用程式的目前視窗縮圖。然而,有些用戶可能覺得這個功能不太實用或會幹擾到他們的使用體驗,因此想要關閉它。工作列縮圖可能很有趣,但它們也可能分散注意力或煩人。考慮到您將滑鼠懸停在該區域的頻率,您可能無意中關閉了重要視窗幾次。另一個缺點是它使用更多的系統資源,因此,如果您一直在尋找一種提高資源效率的方法,我們將向您展示如何停用它。不過

我該如何裁剪HTML中的IFrame? 我該如何裁剪HTML中的IFrame? Aug 29, 2023 pm 04:33 PM

內嵌框架在HTML中稱為iframe。標籤指定內容中的一個矩形區域,瀏覽器可以在其中顯示帶有捲軸和邊框的不同文件。若要在目前HTML文檔中嵌入另一個文檔,請使用內嵌框架。可以使用HTMLiframe名稱屬性指定元素的參考。在JavaScript中,對元素的參考也是使用name屬性進行的。 iframe本質上用於在目前顯示的網頁中顯示網頁。包含iframe的文件的URL使用「src」屬性指定。語法以下是HTML的語法<iframesrc="URL"title="d

掃描全能王去除水印怎麼去除 掃描全能王去除水印怎麼去除 Mar 05, 2024 pm 05:34 PM

去除浮水印是軟體掃描全能王中的一個好用的工具,有些用戶還不太清楚掃描全能王去除水印怎麼去除,可以在保存界面點擊編輯PDF中的去除水印即可閉,接下來就是小編為用戶帶來的去除浮水印方法的介紹,有興趣的用戶快來一起看看吧!掃描全能王使用教學掃描全能王去除浮水印怎麼去除答:可以在儲存介面點選編輯PDF中的移除浮水印詳情介紹:1、進入軟體,點選【相機】圖示。 2、拍攝掃描需要去浮水印的文件。 3.點選【→】進入下一步。 4.完成編輯後,點選【✓】。 5.點選其中的【編輯PDF】。 6.選擇下方的【去除浮水印】即可。

美圖秀秀怎麼加浮水印呢?分享美圖秀加入浮水印方法! 美圖秀秀怎麼加浮水印呢?分享美圖秀加入浮水印方法! Mar 16, 2024 pm 09:55 PM

想知道美圖秀怎麼加浮水印嗎?美圖秀秀是一款非常好用的修圖p圖軟體,提供摳圖放在另一個圖、改圖片大小kb、去浮水印、換證件照底色、全螢幕加時間日期地點水印等功能,可以幫助使用者快速的完成圖片的製作。有用戶製作了自己的圖片不想被別人盜圖想要鋪滿自己的浮水印,但不知道如何操作?小編現在為大家分享美圖秀加入浮水印方法!喜歡就快來下載吧!一、美圖秀怎麼加浮水印?分享美圖秀加入浮水印方法! 1.開啟在本站下載的美圖秀2023版。美圖秀2023版類別:拍攝美化下載美圖秀2023版是一款功能豐富的圖片美化與編輯軟

Wink如何去影片浮水印 Wink如何去影片浮水印 Feb 23, 2024 pm 07:22 PM

Wink如何去視訊浮水印?winkAPP中是有去除掉視訊浮水印的工具,但是多數的小伙伴不知道wink中如何去除掉視頻中的水印,接下來就是小編為玩家帶來的Wink視頻去水印方法圖文教程,有興趣的用戶快來一起看看吧! Wink如何去視訊浮水印1、先開啟winkAPP,在首頁面專區中選擇【去浮水印】功能;2、然後在相簿中選擇你需要去除水印的影片;3、接著選擇影片之後,剪輯影片之後點擊右上角【√】;4、最後點選如下圖的【一鍵去印】之後點選【處理】即可。

小米14Ultra如何去除浮水印? 小米14Ultra如何去除浮水印? Feb 28, 2024 pm 02:28 PM

小米14Ultra是一款性能配置非常不錯的全新機型,這款手機還採用了與眾不同的外觀設計,顏值更高辨識度也很不錯,很多入手的消費者們想要了解一下小米14Ultra怎麼去除浮水印? ,下面本站小編就來為大家介紹吧!小米14Ultra怎麼去除浮水印? 1.開啟相機應用程式:找到並開啟小米14預先安裝的相機應用程式。 2.進入設定選單:點選畫面右下角或左下角(取決於您使用哪個版本)顯示其他選項按鈕。然後,在彈出式選單中選擇“設定”。 3.關閉浮水印選項:在設定選單中,您將看到各種選項和參數。向下滑動直到找到“浮水印”選項

剪映怎麼去水印 怎麼去除剪映水印 剪映怎麼去水印 怎麼去除剪映水印 Feb 22, 2024 pm 05:16 PM

開啟剪映並選擇模板,編輯好影片以後點選匯出,點選無浮水印並分享。教學適用型號:iPhone13系統:iOS15.3.1版本:剪映6.8.0解析1開啟剪映並選擇剪輯範本。 2點選開啟模板後,點選右下角的剪同款選項。 3從相簿中選擇照片填充片段並點擊下一步。 4如何點選頁面右上角的匯出選項。 5最後點選無浮水印並分享就可以了。補充:剪映是什麼軟體1剪映是影片編輯軟體,擁有全面的剪輯功能,可以變速、有濾鏡和美顏效果,還有豐富的曲庫資源。自2021年2月起,剪映支援在手機行動端,Pad端,Mac電

小米14怎麼設定拍照浮水印? 小米14怎麼設定拍照浮水印? Mar 18, 2024 am 11:00 AM

為了讓拍攝出的照片更具個性和獨特性,小米14提供了拍照水印設定。透過設定拍照浮水印,使用者可以在拍攝的照片上添加圖案、文字和標誌,使得每一張照片都能更好地記錄下珍貴的時刻和回憶。接下來,我們將介紹如何在小米14中設定拍照浮水印,讓您的照片更加個人化和生動。小米14怎麼設定拍照浮水印? 1.首先點選“相機”。 2、然後點選「設定」。 3.接著找到浮水印,隨後就可以開始拍攝了。

See all articles