【问题标题】:How can I resize an image from URL using PHP?如何使用 PHP 从 URL 调整图像大小?
【发布时间】:2015-06-13 13:45:07
【问题描述】:

我从 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);

我该怎么做?谢谢。

【问题讨论】:

标签: php


【解决方案1】:

这里是基于imagecopyresampled(GD库)http://php.net/manual/en/function.imagecopyresampled.php的解决方案

$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);
}

【讨论】:

  • 不幸的是,图像似乎已调整大小但黑色
  • 绝对不是,vitr.biz/1/1.php,你有正确安装 GD 库吗?你能发布你的完整代码吗?
  • 现在正在工作我将 new_url 更改为名称。谢谢。
  • 变量名得到了真正的url,new_url是文件在目录中的名字
猜你喜欢
  • 2010-12-29
  • 2021-07-20
  • 2012-02-28
  • 1970-01-01
  • 2015-09-12
  • 2013-04-22
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多