如何在Joomla中调整图片大小?

4
我尝试使用这个:

$image = new JImage();
$image->loadFile($item->logo);
$image->resize('208', '125');

$properties = JImage::getImageFileProperties($item->logo);

echo $image->toFile(JPATH_CACHE . DS . $item->logo, $properties->type);

但不起作用 = \ 有什么想法吗?
1个回答

13

试试这个:

// Set the path to the file
$file = '/Absolute/Path/To/File';

// Instantiate our JImage object
$image = new JImage($file);

// Get the file's properties
$properties = JImage::getImageFileProperties($file);

// Declare the size of our new image
$width = 100;
$height = 100;

// Resize the file as a new object
$resizedImage = $image->resize($width, $height, true);

// Determine the MIME of the original file to get the proper type for output
$mime = $properties->mime;

if ($mime == 'image/jpeg')
{
    $type = IMAGETYPE_JPEG;
}
elseif ($mime == 'image/png')
{
    $type = IMAGETYPE_PNG;
}
elseif ($mime == 'image/gif')
{
    $type = IMAGETYPE_GIF;
}

// Store the resized image to a new file
$resizedImage->toFile('/Absolute/Path/To/New/File', $type);

getImageFileProperties()是一个静态方法,因此调用应该是$properties = JImage::getImageFileProperties($file)。否则,感谢您的出色回答! - Joseph Leedy
@Michael - 非常感谢您的回答,我会编辑它以消除一些错误并添加一些额外的内容。 - bumerang
@Michael 这救了我的一天,但我建议使用 createThumbs 而不是 resize(如果你像我一样需要精确尺寸的图像)。 - Andrei Konstantinov
第4行有一个小错误: new JImage($originalFile); 应该是:JImage($file);这只是挑剔,但我建议使用变量来表示宽度和高度,你的示例中的字符串最初让我感到困惑。 例如:声明:$width=100;$height=100; 然后使用 $image->resize($width,$height, true); - TomDK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接