Skip to main content

Editor

Struct Editor 

Source
pub struct Editor;
Expand description

图像编辑器 — 对齐 PHP Grafika\Gd\Editor

PHP Grafika 把所有操作方法集中在 Editor 类,Image 只持有状态。 Rust 端保持同样的架构分离。

使用方式(对齐 PHP $editor = Grafika::createEditor(['Gd'])):

use sz_rust_core::upload::image::{Editor, Image, Color};
let mut editor = Editor::new();
let mut img = Image::open("test.png")?;
editor.resize_exact(&mut img, 100, 100);
editor.save(&img, "out.png", None, None, false, 0o755)?;

Implementations§

Source§

impl Editor

Source

pub fn new() -> Editor

创建编辑器实例 — 对齐 Grafika::createEditor(['Gd'])

Source

pub fn open<P>(&self, path: P) -> Result<Image, ImageError>
where P: AsRef<Path>,

打开文件 — 对齐 Editor::open(&$image, $imageFile)

PHP 语义:$image 按引用传递,函数内部赋值为新 Image 对象。 Rust 语义:返回新 Image,调用方自行赋值。

Source

pub fn resize_exact(&self, image: &mut Image, new_width: u32, new_height: u32)

强制尺寸缩放 — 对齐 Editor::resizeExact(&$image, $newWidth, $newHeight)

PHP 行为:忽略宽高比,强制缩放到目标尺寸(对应 GD imagecopyresampled)。 Rust 端用 image::imageops::resize + FilterType::Lanczos3(高质量重采样)。

Source

pub fn resize_fit(&self, image: &mut Image, new_width: u32, new_height: u32)

等比缩放适配 — 对齐 Editor::resizeFit(&$image, $newWidth, $newHeight)

PHP 行为:等比缩放,使图像完全包含在目标框内(不裁剪)。

Source

pub fn resize_fill(&self, image: &mut Image, new_width: u32, new_height: u32)

填充+裁剪 — 对齐 Editor::resizeFill(&$image, $newWidth, $newHeight)

PHP 行为:等比缩放使图像完全覆盖目标框,超出部分居中裁剪。

Source

pub fn resize_exact_width(&self, image: &mut Image, new_width: u32)

等比按宽 — 对齐 Editor::resizeExactWidth(&$image, $newWidth)

Source

pub fn resize_exact_height(&self, image: &mut Image, new_height: u32)

等比按高 — 对齐 Editor::resizeExactHeight(&$image, $newHeight)

Source

pub fn crop( &self, image: &mut Image, crop_width: u32, crop_height: u32, position: Position, offset_x: i32, offset_y: i32, ) -> Result<(), ImageError>

裁剪 — 对齐 Editor::crop(&$image, $cropWidth, $cropHeight, $position, $offsetX, $offsetY)

PHP 行为:按 $position 计算裁剪起点,加 $offsetX/$offsetY 偏移。

Source

pub fn blend( &self, image1: &mut Image, image2: &Image, blend_type: BlendType, opacity: f32, position: Position, offset_x: i32, offset_y: i32, ) -> Result<(), ImageError>

图层合成 — 对齐 Editor::blend(&$image1, $image2, $type, $opacity, $position, $offsetX, $offsetY)

PHP 行为:

  1. $position + $offsetX/$offsetY 计算叠加位置
  2. 创建新画布(image1 大小)
  3. 复制 image1 到新画布
  4. $type(normal/multiply/overlay/screen)混合 image2 到新画布
  5. 销毁原 image1 GD 资源,替换为新画布

Rust 端实现 normal 模式(项目业务只用 normal),其他模式预留接口。

Source

pub fn text( &self, image: &mut Image, text: &str, size: u32, x: i32, y: i32, color: Color, font_path: Option<&Path>, ) -> Result<(), ImageError>

文本绘制 — 对齐 Editor::text(&$image, $text, $size, $x, $y, $color, $font, $angle)

PHP 行为(GD imagettftext):

  • $x, $y 是文本基线起点
  • Grafika 内部 $y += $size(GD 的 y 是基线,绘制时 y 要加 size 才是顶部)
  • $angle 是角度(0=水平)
  • $font 是 TTF 文件路径

Rust 端使用 imageproc::drawing::draw_text_mut

  • y 是顶部(不是基线)
  • 所以 Rust y = PHP y - size(反向偏移)
  • angle 暂不支持(imageproc 0.25 文本绘制不支持旋转)
Source

pub fn rotate(&self, image: &mut Image, angle: f32) -> Result<(), ImageError>

旋转 — 对齐 Editor::rotate(&$image, $angle, $color)

PHP 行为:用 imagerotate 旋转,$color 是旋转后空白区域填充色。 Rust 端用 image::imageops::rotate90/180/270 处理 90/180/270 度, 任意角度暂不支持(imageproc 0.25 不直接支持)。

Source

pub fn flip(&self, image: &mut Image, mode: FlipMode)

翻转 — 对齐 Editor::flip(&$image, $mode)

PHP 行为:$mode 是 ‘h’(水平翻转)或 ‘v’(垂直翻转)。

Source

pub fn fill(&self, image: &mut Image, color: Color)

填充 — 对齐 Editor::fill(&$image, $color, $x, $y)

PHP 行为:从 ($x, $y) 开始用 $color 填充连通区域(imagefill)。 Rust 端简化为填充整个图像(业务侧不使用此方法)。

Source

pub fn save( &self, image: &Image, file: &Path, image_type: Option<ImageType>, quality: Option<u8>, _interlace: bool, permission: u32, ) -> Result<(), ImageError>

保存 — 对齐 Editor::save($image, $file, $type, $quality, $interlace, $permission)

PHP 行为:

  • $type=null 时按文件扩展名猜,扩展名也未知时用原图类型
  • 自动 mkdir($targetDir, $permission, true)
  • GIF:动画 → GifHelper::encode;非动画 → imagegif
  • PNG:imagepng(无 quality)
  • JPEG:quality=null → 75;imageinterlace 控制渐进式;imagejpeg

Rust 端用 image::save 简化处理,JPEG quality 通过 image::codecs::jpeg::JpegEncoder 设置。

Trait Implementations§

Source§

impl Default for Editor

Source§

fn default() -> Editor

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more