백엔드 개발 PHP 튜토리얼 PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.

PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.

Jul 29, 2016 am 09:02 AM
gt height image this width

본 시스템용으로 개발된 이미지 라이브러리의 imagick 부분은 gif를 지원하며 자르기, 썸네일 생성 및 워터마크 추가를 완벽하게 지원합니다.

은 다음과 같은 방향별 썸네일 이미지 생성을 지원합니다.

// 把左上角优先
$image->resize_to(100, 100, 'north_west');
// 右边优先
$image->resize_to(100, 100, 'east');
...
로그인 후 복사

더 많은 매개변수를 보려면 소스 코드를 참조하세요.

원본 사진

PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.

렌더링:

PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.PHP는 Imagick을 사용하여 썸네일 자르기/생성/워터마크를 추가하여 GIF를 자동으로 감지하고 처리합니다.

호출 방법:

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?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();} // 垂直翻转 
}
로그인 후 복사

위 내용은 콘텐츠를 포함하여 GIF를 자동으로 감지하고 처리하기 위해 PHP에서 Imagick을 사용하여 썸네일을 생성하고 워터마크를 추가하는 방법을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? 화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? Dec 29, 2023 pm 02:27 PM

많은 사용자들이 스마트 시계를 선택할 때 Huawei 브랜드를 선택하게 됩니다. 그 중 Huawei GT3pro와 GT4가 가장 인기 있는 선택입니다. 두 제품의 차이점을 궁금해하는 사용자가 많습니다. Huawei GT3pro와 GT4의 차이점은 무엇입니까? 1. 외관 GT4: 46mm와 41mm, 재질은 유리 거울 + 스테인레스 스틸 본체 + 고해상도 섬유 후면 쉘입니다. GT3pro: 46.6mm 및 42.9mm, 재질은 사파이어 유리 + 티타늄 본체/세라믹 본체 + 세라믹 백 쉘입니다. 2. 건강한 GT4: 최신 Huawei Truseen5.5+ 알고리즘을 사용하면 결과가 더 정확해집니다. GT3pro: ECG 심전도, 혈관 및 안전성 추가

수정: Windows 11에서 캡처 도구가 작동하지 않음 수정: Windows 11에서 캡처 도구가 작동하지 않음 Aug 24, 2023 am 09:48 AM

Windows 11에서 캡처 도구가 작동하지 않는 이유 문제의 근본 원인을 이해하면 올바른 솔루션을 찾는 데 도움이 될 수 있습니다. 캡처 도구가 제대로 작동하지 않는 주요 이유는 다음과 같습니다. 초점 도우미가 켜져 있습니다. 이렇게 하면 캡처 도구가 열리지 않습니다. 손상된 응용 프로그램: 캡처 도구가 실행 시 충돌하는 경우 응용 프로그램이 손상되었을 수 있습니다. 오래된 그래픽 드라이버: 호환되지 않는 드라이버가 캡처 도구를 방해할 수 있습니다. 다른 응용 프로그램의 간섭: 실행 중인 다른 응용 프로그램이 캡처 도구와 충돌할 수 있습니다. 인증서가 만료되었습니다. 업그레이드 프로세스 중 오류로 인해 이 문제가 발생할 수 있습니다. 이 문제는 대부분의 사용자에게 적합하며 특별한 기술 지식이 필요하지 않습니다. 1. Windows 및 Microsoft Store 앱 업데이트

Bing Image Creator를 무료로 사용하는 방법 Bing Image Creator를 무료로 사용하는 방법 Feb 27, 2024 am 11:04 AM

이 기사에서는 무료 BingImageCreator를 사용하여 고품질 출력을 얻는 7가지 방법을 소개합니다. BingImageCreator(현재 Microsoft Designer용 ImageCreator로 알려짐)는 훌륭한 온라인 인공 지능 아트 생성기 중 하나입니다. 사용자 프롬프트를 기반으로 매우 사실적인 시각 효과를 생성합니다. 프롬프트가 더 구체적이고 명확하며 창의적일수록 결과는 더 좋아질 것입니다. BingImageCreator는 고품질 이미지 생성에 있어 상당한 진전을 이루었습니다. 이제 Dall-E3 트레이닝 모드를 사용하여 더 높은 수준의 디테일과 현실감을 보여줍니다. 그러나 일관되게 HD 결과를 생성하는 능력은 빠른 속도를 포함한 여러 요인에 따라 달라집니다.

Xiaomi 휴대폰에서 이미지를 삭제하는 방법 Xiaomi 휴대폰에서 이미지를 삭제하는 방법 Mar 02, 2024 pm 05:34 PM

Xiaomi 휴대폰에서 이미지를 삭제하는 방법 Xiaomi 휴대폰에서 이미지를 삭제할 수 있지만 대부분의 사용자는 이미지 삭제 방법을 모릅니다. 다음은 편집자가 가져온 Xiaomi 휴대폰에서 이미지 삭제 방법에 대한 튜토리얼입니다. 와서 우리와 함께 보자! Xiaomi 휴대폰에서 이미지를 삭제하는 방법 1. 먼저 Xiaomi 휴대폰에서 [앨범] 기능을 엽니다. 2. 그런 다음 불필요한 사진을 확인하고 오른쪽 하단에 있는 [삭제] 버튼을 클릭합니다. 상단의 특수 영역에 들어가려면 [휴지통]을 선택합니다. 4. 그런 다음 아래 그림과 같이 [휴지통 비우기]를 직접 클릭합니다. 5. 마지막으로 [영구 삭제]를 직접 클릭하여 완료합니다.

html의 너비는 무엇을 의미합니까? html의 너비는 무엇을 의미합니까? Jun 03, 2021 pm 02:15 PM

HTML5에서 너비는 너비를 의미합니다. 너비 속성은 콘텐츠 영역 외부에 내부 여백, 테두리 및 외부 여백을 추가할 수 있습니다. 요소.

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 Jul 29, 2023 am 08:22 AM

1부: 초기 문제 해결 단계 Apple 시스템 상태 확인: 복잡한 솔루션을 살펴보기 전에 기본 사항부터 시작해 보겠습니다. 문제는 귀하의 기기에 있는 것이 아닐 수도 있습니다. Apple 서버가 다운되었을 수도 있습니다. Apple의 시스템 상태 페이지를 방문하여 AppStore가 제대로 작동하는지 확인하세요. 문제가 있는 경우 Apple이 문제를 해결하기를 기다리는 것뿐입니다. 인터넷 연결 확인: "AppStore에 연결할 수 없음" 문제는 때때로 연결 불량으로 인해 발생할 수 있으므로 인터넷 연결이 안정적인지 확인하십시오. Wi-Fi와 모바일 데이터 간을 전환하거나 네트워크 설정을 재설정해 보세요(일반 > 재설정 > 네트워크 설정 재설정 > 설정). iOS 버전을 업데이트하세요.

Imagemagic 설치 Centos 및 이미지 설치 튜토리얼 Imagemagic 설치 Centos 및 이미지 설치 튜토리얼 Feb 12, 2024 pm 05:27 PM

LINUX는 유연성과 사용자 정의 가능성으로 인해 많은 개발자와 시스템 관리자가 가장 먼저 선택하는 운영 체제입니다. LINUX 시스템에서 이미지 처리는 매우 중요한 작업이며 Imagemagick과 Image는 매우 인기 있는 이미지 처리 도구입니다. 이 기사에서는 Centos 시스템에 Imagemagick 및 Image를 설치하는 방법을 소개하고 자세한 설치 튜토리얼을 제공합니다. Imagemagic 설치 Centos 튜토리얼 Imagemagick은 명령줄에서 다양한 이미지 작업을 수행할 수 있는 강력한 이미지 처리 도구 세트입니다. 다음은 Centos 시스템에 Imagemagick을 설치하는 단계입니다.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles