作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用想象库为图像创建缩略图。就这么简单。
$size = new \Imagine\Image\Box(240, 180);
$imagine->open($source_path)->thumbnail($size, 'inset')->save($target_path);
图书馆提供两种模式:插入和出站。在插入模式下,图像会缩小,但不会填满缩略图大小。所以我需要填充它来填充目标大小。有没有使用库函数的简单方法来做到这一点?
最佳答案
如果您不想“缩放”缩略图以适应,则必须裁剪图像。对于裁剪,您必须找到确切的起点,这需要一点努力。
编写一个自定义方法来找到准确的裁剪点、调整大小并返回新图像是个好主意。 Imagine 是一个非常好的库,它提供了我们需要的所有方法。
要遵循的步骤:
伪代码:
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
}
关于php - 如何用想象库填充缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18844252/
我是一名优秀的程序员,十分优秀!