doter1995 2013-03-28 07:31
浏览 36
已采纳

使用php下载图像,同时调整大小

I want to download this image: http://imgs.xkcd.com/clickdrag/1n2w.png

But the image is too large for me so i want to resize it to lets say 100 times smaller than it is now. Also i want the image to have its original name (in this case 1n2w.png).

For downloading i was thinking of using somet
$content = file_get_contents('http://imgs.xkcd.com/clickdrag/1n2w.png');
file_put_contents('/images', $content);

But it didnt work. Maybe i need to use curl for this? As for the resizing part i dont know what to use, so if possible i would like too see some suggestions on this.

  • 写回答

1条回答 默认 最新

  • dousi1961 2013-03-28 07:36
    关注

    For image re-sizing you can use it.

        $percent = 0.5;
    
        $filename = '/home/Pictures/downloaded_file.jpg';
    
        header('Content-type: image/jpeg');
    
        list($width, $height) = getimagesize($filename);
    
        $new_width = $width * $percent;
    
        $new_height = $height * $percent;
    
        $image_p = imagecreatetruecolor($new_width, $new_height);
    
        $image = imagecreatefromjpeg($filename)
        ;
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
        imagejpeg($image_p, null, 100);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启