我们将上传大小超过3 MB的图像,超过2,500px。我有我的代码用于上传多个图像,现在希望将它们调整大小或缩小到大约500px宽的Web查看大小。我希望它保持良好的质量,但不需要很长时间下载。我将进行预览,当你点击它时,在灯箱中打开一个更大的图像,这将是500px或任何我可以逃脱的东西,仍然可以快速下载。
如果有人可以帮我修改我的代码以按比例调整为500px宽,我将不胜感激。
$valid_formats = array("jpg", "png", "gif", "bmp");
$max_file_size = 100 * 1024 * 1024; //100 mb
$path = "PW-Files/$listingid/images/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
$ext = end(explode('.', $name));
$newname = rand(10,10000) . rand(10,1000) . "." . "$ext";
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$newname)) {
$count++; // Number of successfully uploaded files
}
}
}
}
}
答案 0 :(得分:3)
要调整图像大小,可以使用WideImage(用于图像处理的php库)。
它提供了一种简单的方法来调整图像大小,如:
$image = WideImage::load( 'yourImage.jpg' );
$resizedImage = $image->resize( $width, $height );
您也可以在调整大小后保存文件。
$resizedImage->saveToFile( 'filename.jpg' );
有关详情,请参阅documentation。