如何通过URL调整图片大小并使图像更小

6

我有一个使用 Virtuemart 模块的 Joomla 网站。

我的图片和缩略图托管在另一个域名上。

Virtuemart 给了我两个选项:

1 - 浏览图片,会自动创建缩略图

2 - 输入外部托管图片的 URL,此选项没有调整大小的选项

我克服这个问题的方法(当然是在帮助下)是在 img 标签中设置 height="" 属性。唯一的问题是,当加载大图像,例如 500 x 700 px 时,所谓的缩略图需要时间来加载,这是合理的。

有人能给我提供选项,如何“调整大小”从 URL 获得图像,以使缩略图加载时间更短?

个人认为可以通过代码、CSS 或其他方式减少从 URL 调整大小的图像质量?

我知道这与解决代码的问题关系不大,但我知道你们比我知道更多选项。

1个回答

13

你可以利用PHP的imagecopyresampled函数。

示例程序(来源:php.net)

<?php
// The file
$filename = 'http://valplibrary.files.wordpress.com/2009/01/5b585d_merry-christmas-blue-style.jpg';
$percent = 0.5; // percentage of resize

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

当然可以,我已经在帖子中添加了一个示例来实现这个。 - codaddict
@ bzabhi,您能否帮我看一下我的下一个问题?它涉及到您给我的代码。http://stackoverflow.com/questions/1961031/resample-script-how-to-make-it-work-in-a-excisting-website-with-different-img-ur - Chris

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