dongxiaoxing3058 2015-06-13 13:45
浏览 100
已采纳

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

I'm receiving Images from URLs and I would like to save these images into a new directory, in three different sizes. I'm already getting the URLs here, now I just need a way to resize each image, with a specific height and width.

I dont want to resize uploaded images, only Images from a specific URL.

My code:

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

How can I do that? Thanks.

  • 写回答

1条回答 默认 最新

  • doudi7570 2015-06-13 14:31
    关注

    here is the solution based on imagecopyresampled (GD library) 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题