Vẽ mã thông báo vào php

Imagick giúp thao tác hình ảnh trong PHP cực kỳ dễ dàng thông qua giao diện OO. Dưới đây là một ví dụ nhanh về cách tạo hình thu nhỏ

Nội dung chính Hiển thị

  • Làm cách nào để thêm tưởng tượng vào PHP?
  • Phần mở rộng tưởng tượng PHP là gì?
  • Làm cách nào để biết PHP tưởng tượng đã được cài đặt chưa?
  • Làm cách nào để cài đặt thư viện tưởng tượng?

Ví dụ #1 Tạo hình thu nhỏ trong Imagick

header

('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(1000);

echo

$image;?>

Sử dụng SPL và các tính năng OO khác được hỗ trợ trong Imagick, có thể đơn giản thay đổi kích thước tất cả các tệp trong một thư mục (hữu ích cho việc thay đổi kích thước hàng loạt hình ảnh máy ảnh kỹ thuật số lớn để có thể xem được trên web). Ở đây chúng tôi sử dụng thay đổi kích thước, vì chúng tôi có thể muốn giữ lại một số siêu dữ liệu

Ví dụ #2 Tạo hình thu nhỏ của tất cả các tệp JPG trong một thư mục

$images

= new Imagick(glob('images/*.JPG'));

foreach(

$images as $image) {// Providing 0 forces thumbnailImage to maintain aspect ratio
    
$image->thumbnailImage(1024,0);

}

$images->writeImages();?>

Đây là một ví dụ về tạo phản chiếu của một hình ảnh. Sự phản chiếu được tạo ra bằng cách lật hình ảnh và phủ một dải màu lên trên nó. Sau đó, cả hình ảnh gốc và hình ảnh phản chiếu được phủ lên trên canvas

Ví dụ #3 Tạo phản chiếu của hình ảnh

/* Read the image */
$im = new Imagick("test.png");/* Thumbnail the image */
$im->thumbnailImage(200null);/* Create a border for the image */
$im->borderImage(new ImagickPixel("white"), 55);/* Clone the image and flip it */
$reflection $im->clone();
$reflection->flipImage();/* Create gradient. It will be overlayed on the reflection */
$gradient = new Imagick();/* Gradient needs to be large enough for the image and the borders */
$gradient->newPseudoImage($reflection->getImageWidth() + 10$reflection->getImageHeight() + 10"gradient:transparent-black");/* Composite the gradient on the reflection */
$reflection->compositeImage($gradientimagick::COMPOSITE_OVER00);/* Add some opacity. Requires ImageMagick 6.2.9 or later */
$reflection->setImageOpacity0.3 );/* Create an empty canvas */
$canvas = new Imagick();/* Canvas needs to be large enough to hold the both images */
$width $im->getImageWidth() + 40;
$height = ($im->getImageHeight() * 2) + 30;
$canvas->newImage($width$height, new ImagickPixel("black"));
$canvas->setImageFormat("png");/* Composite the original image and the reflection on the canvas */
$canvas->compositeImage($imimagick::COMPOSITE_OVER2010);
$canvas->compositeImage($reflectionimagick::COMPOSITE_OVER20$im->getImageHeight() + 10);/* Output the image*/
header("Content-Type: image/png");
echo 
$canvas;
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như

Ví dụ này minh họa cách sử dụng các mẫu tô trong khi vẽ

Ví dụ #4 Tô văn bản với gradient

/* Create a new imagick object */
$im = new Imagick();/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(5050"gradient:red-black");/* Create imagickdraw object */
$draw = new ImagickDraw();/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient'005050);/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER005050$im);/* Close the pattern */
$draw->popPattern();/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');/* Set font size to 52 */
$draw->setFontSize(52);/* Annotate some text */
$draw->annotation(2050"Hello World!");/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(35070"white");/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);/* 1px black border around the image */
$canvas->borderImage('black'11);/* Set the format to PNG */
$canvas->setImageFormat('png');/* Output the image */
header("Content-Type: image/png");
echo 
$canvas;
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như

Làm việc với ảnh GIF động

Ví dụ #5 Đọc trong ảnh GIF và thay đổi kích thước tất cả các khung

________số 8

Làm việc với phông chữ nguyên thủy và tùy chỉnh hình elip

Ví dụ #6 Tạo logo PHP

/* Set width and height in proportion of genuine PHP logo */
$width 400;
$height 210;/* Create an Imagick object with transparent canvas */
$img = new Imagick();
$img->newImage($width$height, new ImagickPixel('transparent'));/* New ImagickDraw instance for ellipse draw */
$draw = new ImagickDraw();
/* Set purple fill color for ellipse */
$draw->setFillColor('#777bb4');
/* Set ellipse dimensions */
$draw->ellipse($width 2$height 2$width 2$height 20360);
/* Draw ellipse onto the canvas */
$img->drawImage($draw);/* Reset fill color from purple to black for text (note: we are reusing ImagickDraw object) */
$draw->setFillColor('black');
/* Set stroke border to white color */
$draw->setStrokeColor('white');
/* Set stroke border thickness */
$draw->setStrokeWidth(2);
/* Set font kerning (negative value means that letters are closer to each other) */
$draw->setTextKerning(-8);
/* Set font and font size used in PHP logo */
$draw->setFont('Handel Gothic.ttf');
$draw->setFontSize(150);
/* Center text horizontally and vertically */
$draw->setGravity(Imagick::GRAVITY_CENTER);/* Add center "php" with Y offset of -10 to canvas (inside ellipse) */
$img->annotateImage($draw0, -100'php');
$img->setImageFormat('png');/* Set appropriate header for PNG and output the image */
header('Content-Type: image/png');
echo 
$img;
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như

vokseli ¶

8 năm trước

header0

header1

$images

header3

inoshadi tại gmail dot com ¶

8 năm trước

header4

header5

header6

header7

Làm cách nào để thêm tưởng tượng vào PHP?

Điều hướng đến Trang chủ - Phần mềm - Trình cài đặt mô-đun, sau đó nhấp vào nút Quản lý bên cạnh PHP Pecl. Trong màn hình tiếp theo, chọn phiên bản PHP được yêu cầu, sau đó nhấp vào Áp dụng. Bây giờ bạn có thể nhập “imagick” vào trường Install a PHP Pecl và nhấp vào nút Install Now

Phần mở rộng tưởng tượng PHP là gì?

Imagick là tiện ích mở rộng PHP để tạo và sửa đổi hình ảnh bằng thư viện ImageMagick . Ngoài ra còn có một phiên bản Imagick dành cho HHVM. Mặc dù hai tiện ích mở rộng hầu như tương thích trong API của chúng và cả hai đều gọi thư viện ImageMagick, hai tiện ích mở rộng là các cơ sở mã hoàn toàn riêng biệt.

Làm cách nào để biết PHP tưởng tượng đã được cài đặt chưa?

Để kiểm tra xem ImageMagick đã được cài đặt trên hệ thống dựa trên Unix hay chưa, hãy thử cách sau. .

Mở một cửa sổ terminal - console

Thực hiện lệnh sau. chuyển đổi -version

Nếu phiên bản ImageMagick và các thông tin khác được hiển thị, nghĩa là bạn đã cài đặt ImageMagick và bạn có thể bỏ qua phần tiếp theo