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 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)