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
impl Editor
Sourcepub fn open<P>(&self, path: P) -> Result<Image, ImageError>
pub fn open<P>(&self, path: P) -> Result<Image, ImageError>
打开文件 — 对齐 Editor::open(&$image, $imageFile)
PHP 语义:$image 按引用传递,函数内部赋值为新 Image 对象。
Rust 语义:返回新 Image,调用方自行赋值。
Sourcepub fn resize_exact(&self, image: &mut Image, new_width: u32, new_height: u32)
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(高质量重采样)。
Sourcepub fn resize_fit(&self, image: &mut Image, new_width: u32, new_height: u32)
pub fn resize_fit(&self, image: &mut Image, new_width: u32, new_height: u32)
等比缩放适配 — 对齐 Editor::resizeFit(&$image, $newWidth, $newHeight)
PHP 行为:等比缩放,使图像完全包含在目标框内(不裁剪)。
Sourcepub fn resize_fill(&self, image: &mut Image, new_width: u32, new_height: u32)
pub fn resize_fill(&self, image: &mut Image, new_width: u32, new_height: u32)
填充+裁剪 — 对齐 Editor::resizeFill(&$image, $newWidth, $newHeight)
PHP 行为:等比缩放使图像完全覆盖目标框,超出部分居中裁剪。
Sourcepub fn resize_exact_width(&self, image: &mut Image, new_width: u32)
pub fn resize_exact_width(&self, image: &mut Image, new_width: u32)
等比按宽 — 对齐 Editor::resizeExactWidth(&$image, $newWidth)
Sourcepub fn resize_exact_height(&self, image: &mut Image, new_height: u32)
pub fn resize_exact_height(&self, image: &mut Image, new_height: u32)
等比按高 — 对齐 Editor::resizeExactHeight(&$image, $newHeight)
Sourcepub fn crop(
&self,
image: &mut Image,
crop_width: u32,
crop_height: u32,
position: Position,
offset_x: i32,
offset_y: i32,
) -> Result<(), ImageError>
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 偏移。
Sourcepub fn blend(
&self,
image1: &mut Image,
image2: &Image,
blend_type: BlendType,
opacity: f32,
position: Position,
offset_x: i32,
offset_y: i32,
) -> Result<(), ImageError>
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 行为:
- 按
$position+$offsetX/$offsetY计算叠加位置 - 创建新画布(image1 大小)
- 复制 image1 到新画布
- 按
$type(normal/multiply/overlay/screen)混合 image2 到新画布 - 销毁原 image1 GD 资源,替换为新画布
Rust 端实现 normal 模式(项目业务只用 normal),其他模式预留接口。
Sourcepub fn text(
&self,
image: &mut Image,
text: &str,
size: u32,
x: i32,
y: i32,
color: Color,
font_path: Option<&Path>,
) -> Result<(), ImageError>
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 文本绘制不支持旋转)
Sourcepub fn rotate(&self, image: &mut Image, angle: f32) -> Result<(), ImageError>
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 不直接支持)。
Sourcepub fn flip(&self, image: &mut Image, mode: FlipMode)
pub fn flip(&self, image: &mut Image, mode: FlipMode)
翻转 — 对齐 Editor::flip(&$image, $mode)
PHP 行为:$mode 是 ‘h’(水平翻转)或 ‘v’(垂直翻转)。
Sourcepub fn fill(&self, image: &mut Image, color: Color)
pub fn fill(&self, image: &mut Image, color: Color)
填充 — 对齐 Editor::fill(&$image, $color, $x, $y)
PHP 行为:从 ($x, $y) 开始用 $color 填充连通区域(imagefill)。
Rust 端简化为填充整个图像(业务侧不使用此方法)。
Sourcepub fn save(
&self,
image: &Image,
file: &Path,
image_type: Option<ImageType>,
quality: Option<u8>,
_interlace: bool,
permission: u32,
) -> Result<(), ImageError>
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§
Auto Trait Implementations§
impl Freeze for Editor
impl RefUnwindSafe for Editor
impl Send for Editor
impl Sync for Editor
impl Unpin for Editor
impl UnsafeUnpin for Editor
impl UnwindSafe for Editor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.