你会推荐什么样的PHP库用于生成缩略图? 我需要它与GD(不仅是Imagick)一起工作,能够在仅给出一个维度时拉伸图像或保持宽高比,并且 - 最重要的是 - 能够裁剪缩放图像(生成缩略图时,我需要所有的他们是,我们说128x128,所以我想图书馆在这种情况下裁剪图像) 你的建议是什么?thumbnail generation/image resize
2
A
回答
2
我编码我自己的GD wrapper为我的框架,phunction。一些例子:
$input = '/path/to/source/image.jpg';
$output = '/path/to/destination/image.jpg';
/*
this will crop the biggest possible square (since 128/128 = 1)
from the center of the image and resize the width to 500px
while keeping the height porportional (to maintian aspect ratio)
*/
ph()->Disk->Image($input, '128/128', '500*0', null, $output);
/*
same as the above, but aspect ratio is not respected
since both width and height are specified
*/
ph()->Disk->Image($input, '128/128', '500*1000', null, $output);
/*
no cropping, just porpotional resize to the width
*/
ph()->Disk->Image($input, null, '500*0', null, $output);
有喜欢Asido和WideImage其他的好办法,但这个对我的作品,而且因为它是一个简单的方法只有一个依赖,你可以很容易地使用它独立:
function Image($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
{
if (isset($input, $output) === true)
{
if (is_string($input) === true)
{
$input = @ImageCreateFromString(@file_get_contents($input));
}
if (is_resource($input) === true)
{
$size = array(ImageSX($input), ImageSY($input));
$crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
$scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));
if (count($crop) == 2)
{
$crop = array($size[0]/$size[1], $crop[0]/$crop[1]);
if ($crop[0] > $crop[1])
{
$size[0] = round($size[1] * $crop[1]);
}
else if ($crop[0] < $crop[1])
{
$size[1] = round($size[0]/$crop[1]);
}
$crop = array(ImageSX($input) - $size[0], ImageSY($input) - $size[1]);
}
else
{
$crop = array(0, 0);
}
if (count($scale) >= 1)
{
if (empty($scale[0]) === true)
{
$scale[0] = round($scale[1] * $size[0]/$size[1]);
}
else if (empty($scale[1]) === true)
{
$scale[1] = round($scale[0] * $size[1]/$size[0]);
}
}
else
{
$scale = array($size[0], $size[1]);
}
$image = ImageCreateTrueColor($scale[0], $scale[1]);
if (is_resource($image) === true)
{
ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
ImageSaveAlpha($image, true);
ImageAlphaBlending($image, true);
if (ImageCopyResampled($image, $input, 0, 0, round($crop[0]/2), round($crop[1]/2), $scale[0], $scale[1], $size[0], $size[1]) === true)
{
$result = false;
if ((empty($sharp) !== true) && (is_array($matrix = array_fill(0, 9, -1)) === true))
{
array_splice($matrix, 4, 1, (is_int($sharp) === true) ? $sharp : 16);
if (function_exists('ImageConvolution') === true)
{
ImageConvolution($image, array_chunk($matrix, 3), array_sum($matrix), 0);
}
}
if ((isset($merge) === true) && (is_resource($merge = @ImageCreateFromString(@file_get_contents($merge))) === true))
{
ImageCopy($image, $merge, round(0.95 * $scale[0] - ImageSX($merge)), round(0.95 * $scale[1] - ImageSY($merge)), 0, 0, ImageSX($merge), ImageSY($merge));
}
foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
{
if (preg_match('~' . $key . '$~i', $output) > 0)
{
$type = str_replace('?', '', $key);
$output = preg_replace('~^[.]?' . $key . '$~i', '', $output);
if (empty($output) === true)
{
header('Content-Type: image/' . $type);
}
$result = call_user_func_array('Image' . $type, array($image, $output, $value));
}
}
return $result;
}
}
}
}
else if (count($result = @GetImageSize($input)) >= 2)
{
return array_map('intval', array_slice($result, 0, 2));
}
return false;
}
也支持水印和转换/显示(而不是保存)图像。
-3
Jus在提问之前使用搜索。关于keeping aspect ratio和about cropping。
+0
我不是在寻找一段代码,全功能和测试库。可能通过'司机'支持轻松切换GD和Imagick。 – migajek 2011-05-29 12:24:34
3
你可以试试这个:https://gist.github.com/342704/
用法示例:
include("classes/Resize.php");
$resizer = new Resize('images/cars/large/input.jpg');
$resizer->resizeImage(150, 100, 0);
$resizer->saveImage('images/cars/large/output.jpg', 100);
相关问题
- 1. ImageMagick转换中的sample/resample/scale/resize/adaptive-resize/thumbnail运算符有什么区别?
- 2. Resize-reduce/thumbnail在ImageMagick上传期间的图像
- 3. Plupload Thumbnail
- 4. $(window).resize()is slowggish
- 5. ABContact setImage,thumbnail bug?
- 6. center bootstrap img-thumbnail
- 7. Sorl-thumbnail bad url's
- 8. Fancybox Thumbnail Opacity
- 9. Getting vine thumbnail
- 10. Java Thumbnail View
- 11. Thumbnail not moving on hover
- 12. django:sorl-thumbnail的问题
- 13. 而不是.resize?
- 14. Galleria resize stop load()
- 15. PHP Image Resize(gallery)
- 16. android resize animation
- 17. android resize image
- 18. jtextarea resize java swing
- 19. Resize Jquery Datepicker
- 20. jquery resize()api confusion
- 21. Resize Sprite Flex/ActionScript
- 22. javascript resize background image
- 23. cakephp resize component/helper
- 24. $(window).resize():之前
- 25. Iframe Auto-Resize
- 26. resize sdwebImage swift
- 27. Resize WinForms UserControl
- 28. resize css sprite
- 29. jQuery Resize插件
- 30. Resize ctypes array
等十道题完全相同的副本 – dynamic 2011-05-27 14:57:34