doulin4844 2012-11-19 18:29
浏览 23
已采纳

如何将图像从url添加到temp?

I am using the SimpleImage.php class from: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ , to resize, compress and save the images.

Usually using it with an image from input:

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

But how can I deal just with the image url? (from another website)

$img = file_get_contents($url);

The above $img variable keeps the actual image. So how can I save it to temp to use it? If this is the right way.

If it's possible not to have to change SimpleImage.php class.

  • 写回答

1条回答 默认 最新

  • duanbo1659 2012-11-19 18:36
    关注

    With tempnam()

    Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist, tempnam() may generate a file in the system's temporary directory, and return the name of that.

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致