【问题标题】:How to add an image from url to temp?如何将图像从 url 添加到 temp?
【发布时间】:2012-11-20 02:36:29
【问题描述】:

我正在使用来自 http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ 的 SimpleImage.php 类来调整、压缩和保存图像。

通常将其与输入中的图像一起使用:

$tmp_dir = $_FILES['file']['tmp_name'];
$file_name = 'something.jpg'; 

include('SimpleImage.php');
$image = new SimpleImage();
$image->load($tmp_dir);

$image->resizeToWidth($width);
$image->save('imgd/l'.$file_name);

但我如何只处理图片网址?(来自另一个网站)

$img = file_get_contents($url);

上面的$img 变量保留了实际的图像。 那么如何将其保存到 temp 以使用它呢? 如果这是正确的方法。

如果有可能不必更改 SimpleImage.php 类。

【问题讨论】:

    标签: php


    【解决方案1】:

    tempnam()

    创建具有唯一文件名的文件,访问权限设置为 0600,在指定目录下。如果目录不存在, tempnam() 可能会在系统的临时目录中生成一个文件,并且 返回那个名字。

    <?php 
    $tmpfname = tempnam("/tmp", "UL_IMAGE");
    $img = file_get_contents($url);
    file_put_contents($tmpfname, $img);
    
    include('SimpleImage.php');
    $image = new SimpleImage();
    $image->load($tmpfname);
    
    $image->resizeToWidth($width);
    $image->save('imgd/l'.$file_name);
    ?>
    

    【讨论】:

    • 谢谢,现在我发现它也适用于 url,我有另一个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多