dtbl1231 2014-09-29 14:59
浏览 49
已采纳

使用PHP中的readfile()在下载和演示之前调整图像大小

I'd like to have an image resized on the server side using PHP before download and before it is presented to the user. This cannot be done during upload since the images are constantly changing and being uploaded using FTP. I am using the following code to present the image

header('Content-Type: image/jpeg');
readfile($img . $filename . "." . $ext);

Is it possible for this to be done through PHP as I'd like to reduce the download size of the image; ideally without writing to disk (since the file is constantly accessed by users).

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • doushe8577 2014-09-29 15:16
    关注

    If you have GD library installed , you can do what you need without writing it to disk.

    <?php
    $filename = 'images/picture.jpg';
    //the resize will be a percent of the original size
    $percent = 0.5; // 50% 
    
    // Content type
    header('Content-Type: image/jpeg');
    
    // Get new sizes
    list($width, $height) = getimagesize($filename);
    $newwidth = $width * $percent;
    $newheight = $height * $percent;
    
    // Load
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    
    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    
    imagejpeg($thumb); // this will output image data
    // if you need much lower size of image try experimenting with quality param
    // imagejpeg($thumb,$saveToFile=null, $quality=70);
    imagedestroy($thumb); //free some memory
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等