php – 如何用想象库填充缩略图
发布时间:2020-12-13 21:49:15 所属栏目:PHP教程 来源:网络整理
导读:我正在使用imag库为图像创建缩略图.就这么简单. $size = new ImagineImageBox(240,180);$imagine-open($source_path)-thumbnail($size,'inset')-save($target_path); 该库提供两种模式:插入和出站.在插入模式下,图像调整大小但不填充缩略图大小.所以我需
我正在使用imag库为图像创建缩略图.就这么简单.
$size = new ImagineImageBox(240,180); $imagine->open($source_path)->thumbnail($size,'inset')->save($target_path); 该库提供两种模式:插入和出站.在插入模式下,图像调整大小但不填充缩略图大小.所以我需要填充它来填充目标大小.有没有一种简单的方法来使用库函数? 解决方法
如果您不想“缩放”缩略图以适合,则必须裁剪图像.对于裁剪,您必须找到确切的起点,并且需要一点点努力.
编写自定义方法来查找精确的裁剪点,调整大小并返回新图像是个好主意. Imagine是一个非常好的库,它提供了我们需要的所有方法. 要遵循的步骤: >使用getSize()获取原始图像的尺寸 >如果是横向,请使用目标框的宽度查找目标宽度 >使用THUMBNAIL_OUTBOUND调整图像大小并创建“小大缩略图”. 伪代码: function resizeToFit( $targetWidth,$targetHeight,$sourceFilename ) { // Box is Imagine Box instance // Point is Imagine Point instance $target = new Box($targetWidth,$targetHeight ); $originalImage = imagine->open( $sourceFilename ); $orgSize = $originalImage->getSize(); if ($orgSize->width > $orgSize->height) { // Landscaped.. We need to crop image by horizontally $w = $orgSize->width * ( $target->height / $orgSize->height ); $h = $target->height; $cropBy = new Point( ( max ( $w - $target->width,0 ) ) / 2,0); } else { // Portrait.. $w = $target->width; // Use target box's width and crop vertically $h = $orgSize->height * ( $target->width / $orgSize->width ); $cropBy = new Point( 0,( max( $h - $target->height,0 ) ) / 2); } $tempBox = Box($w,$h); $img = $originalImage->thumbnail($tempBox,ImageInterface::THUMBNAIL_OUTBOUND); // Here is the magic.. return $img->crop($cropBy,$target); // Return "ready to save" final image instance } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |