如何使用PHP从URL调整图像大小?

如何使用PHP从URL调整图像大小?,php,Php,我正在接收来自URL的图像,我想将这些图像以三种不同的大小保存到一个新目录中。我已经在这里得到了URL,现在我只需要一种方法来调整每个图像的大小,具有特定的高度和宽度 我不想调整上传图像的大小,只想调整来自特定URL的图像 我的代码: $content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg'); $name = "http://www.joomlaworks.ne

我正在接收来自URL的图像,我想将这些图像以三种不同的大小保存到一个新目录中。我已经在这里得到了URL,现在我只需要一种方法来调整每个图像的大小,具有特定的高度和宽度

我不想调整上传图像的大小,只想调整来自特定URL的图像

我的代码:

$content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
$name = "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg";
$parts = explode('.', $name);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . $parts[count($parts) - 1];
file_put_contents(DIRECTORY.'/' . $new_url , $content);

我该怎么做?谢谢。

这是基于
imagecopyresampled
(GD库)的解决方案


ImageMagick是一个处理图像的强大工具:图像的可能副本不幸的是,图像的可能副本已调整大小,但绝对不是,您是否正确安装了GD库?你能发布你的完整代码吗?现在开始工作了,我把新的url改成了name。谢谢。变量名得到了真实的url,新的url是文件在目录中的名称
$content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
$name = "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg";
$parts = explode('.', $name);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . $parts[count($parts) - 1];
file_put_contents(DIRECTORY.'/' . $new_url , $content);

resizeImage($new_url, DIRECTORY.'/1_' . $new_url, 100, 100);
resizeImage($new_url, DIRECTORY.'/2_' . $new_url, 200, 200);
resizeImage($new_url, DIRECTORY.'/3_' . $new_url, 300, 300);

function resizeImage($source, $dest, $new_width, $new_height)
{
    list($width, $height) = getimagesize($source);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($source);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, $dest, 100);
}