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

使用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.

我想在下载之前和之前使用PHP在服务器端调整图像大小 用户。 这在上传期间无法完成,因为图像不断变化并使用FTP上传。 我使用以下代码来显示图像 p>

 标头('Content-Type:image / jpeg'); 
readfile($ img。$ filename。“。”。  $ ext); 
  code>  pre> 
 
 

这是否可以通过PHP完成,因为我想减少图像的下载大小; 理想情况下无需写入磁盘(因为用户经常访问该文件)。 p>

感谢您的帮助。 p> div>

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