phpixie / image
PHPixie 图像处理库
3.3
2018-02-15 23:57 UTC
Requires (Dev)
- phpixie/test: ~3.0
README
PHPixie Image 库
PHP 中存在多个图像处理库,但遗憾的是它们的 API 并非总是直观。PHPixie Image 提供了一个通用的简单接口,用于处理 GD、Imagick 和 Gmagick,使得开发变得简单,并允许轻松地在它们之间切换。
初始化
将 "phpixie/image":"~3.0"
添加到您的 composer.json 中,并运行 composer update
。
$image = new \PHPixie\Image(); //You can also specify which driver to use ('gd', 'imagick' or 'gmagick') //With 'gd' being the default one $image = new \PHPixie\Image('gmagick');
- GD – 最常用的 PHP 图像库。对于大多数情况来说足够好,但速度较慢,并且可能不会提供相同级别的图像质量。
- Imagick – ImageMagick 库的 PHP 封装。提供良好的性能和完美的质量,但需要 PHP 扩展。
- Gmagick – PHP GraphicsMagick 绑定。比 Imagick 快,但最近 API 的更改有时会在处理文本时产生不可预测的结果。
PHPixie 框架用户已经内置了 Image 组件,可以通过以下方式访问:
$image = $builder->components()->image();
默认驱动可以在配置文件中指定
// /assets/config/image.php return array( 'defaultDriver' => 'imagick' );
创建和读取图像
现在您已经初始化了库,您可以创建一个新的图像或从磁盘读取一个图像
// Create a new 100x200 white image $img = $image->create(100, 200); // Create a new 100x200 image filled with red color at 0.5 opacity $img = $image->create(100, 200, 0xff0000, 0.5); // Read a file from disk $img = $image->read('/some/file.png'); // Load a file from file contents $data = file_get_contents('/some/file.png'); $img = $image->load($data);
上面的示例都将使用您在初始化库时指定的默认驱动程序。如果您同时使用多个后端,您可以指定每个图像使用的驱动程序
$driver = $image->driver('imagick'); $img = $driver->create(100, 200, 0xff0000, 0.5); $img = $driver->read('/some/file.png'); $img = $driver->load($data); //Or using an optional parameter $img = $image->create(100, 200, 0xff0000, 0.5, 'imagick'); $img = $image->read('/some/file.png', 'imagick'); $img = $image->load($data, 'imagick');
我们将在下一部分查看图像处理,但首先让我们看看如何将图像写回磁盘或将它们渲染到变量中。
// Save to file // By default Image will guess the image format // from the file name $img->save('pixie.png'); // You can always specify the format and quality manually though $img->save('pixie.jpg', 'jpg', 90); // This will render the image data into a variable, // useful for sending directly to the browser $data = $img->render('png'); // This method also supports quality specification $data = $img->render('jpg', 90);
图像处理
调整大小和裁剪
// Resize it to 400px width, aspect ratio is maintained $img->resize(400); // Resize to 200px in height $img->resize(null, 200); // Resize to fit in a 200x100 box // A 300x300 image would become 100x100 // it's as if you specify the maximum size $img->resize(200, 100); // Resize to "fill" a 200x100 box // A 300x300 image would become 200x200 // it's as if you specify the minumum size $img->resize(200, 100, false); //Scale image using a ratio //This would make it twice as big $img->scale(2); //Crop image to 100x150 with 10 horizontal offset //and 15 vertical $img->crop(100, 100, 10, 15);
如果您希望用户头像具有固定大小,您必须将它们调整到尽可能接近所需的大小,然后进行裁剪,如下所示:
//Let's assume $img is 300x200 //and we want to make 100x100 avatars. //Note how you can chain the methods together $img->resize(100, 100, false) //becomes 150x100 ->crop(100, 100) ->save('avatar.png'); //We even have a predefined fill() function for this =) $img->fill(100, 100)->save('avatar.png'); //that's it
旋转和翻转
//Rotate the image 45 degrees counter clockwise //filling the background with semitransparent white $img->rotate(45, 0xffffff, 0.5); $img->flip(true); //flip horizontally $img->flip(false, true); //flip vertically $img->flip(true, true); //flip bloth
叠加图像 叠加对于水印图像或创建一些花哨的头像非常有用。您可以通过链式调用 overlay() 来叠加任意数量的图像
$meadow = $image->read('meadow.png'); $fairy = $image->read('fairy.png'); $flower = $image->read('flower.png'); //Put fairy at coordinates 40, 50 $meadow->overlay($fairy, 40, 50) ->overlay($flower, 100, 200) ->save('meadow2.png');
请注意,叠加图像不会自动扩展现有的图像,这意味着如果您在 100x100 的图像上叠加一个 500x300 的图像,您将得到一个 100x100 的结果,其中裁剪掉了访问区域。您可以通过创建一个画布层来解决这个问题。
$large = $image->read('large.png');// 500x300 $small = $image->read('small.png');// 100x100 //Make transparent canvas the size of large image $canvas = $image->create($large->width(), $large->height(); $canvas->overlay($small) ->overlay($large) ->save('merged.png');
绘制文本
绘制文本是图像处理中最令人沮丧的事情之一,尤其是在需要跨多行包装文本时。在我们看示例之前,让我们看看字体度量。
当我们指定文本坐标时,我们将指定 基线 的坐标,因此文本将稍微高一些。
//Make white background $img = $this->pixie->create(500, 500, 0xffffff, 1); //Write "tinkerbell" using font.ttf font and font size 30 //Put it in coordinates 50, 60 (baseline coordinates) //And make it half transparent red color $img->text("Tinkerbell", 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5); //Wrap text so that its 200 pixel wide $text = "Trixie is a nice little fairy that like spicking flowers"; $img->text($text, 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, 200); //Increase Line spacing by 50% $img->text($text, 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, 200, 1.5); //Write text under a 45 degree counter clockwise angle: $img->text("Tinkerbell", 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, null, 1, 45);
就是这样。您应该能够处理几乎任何图像。