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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable