我有一个表格来上传文件:
<form method="POST" action="upload-image.php">
<input type="file" name="file"><br>
<input type="submit" value="Submit">
</form>
还有一个 PHP 代码来调整上传文件(图像)的大小,将其转换为 JPG,并在浏览器中显示。
<?php
function image_to_jpg($src_file){
$file = $_FILES["file"]["name"];
$ext = end(explode(".",$file));
list($width_orig,$height_orig) = getimagesize($src_file);
$width = 128;
$height = 128;
ini_set("memory_limit","32M");
if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($file);
}
if($ext == "gif"){
$image = imagecreatefromgif($file);
}
if($ext == "png"){
$image = imagecreatefrompng($file);
}
$new_image = imagecreatetruecolor($width,$height);
imagecopyresampled($new_image,$image,0,0,0,0,$width,$height,$width_orig,height_orig);
$dest_file = "ok.jpg";
header("Content-Type: image/jpeg");
imagejpeg($new_image,$dest_file);
}
$src_file = $_FILES["file"]["tmp_name"];
image_to_jpg($src_file);
?>
它不工作。它显示一个空白图像。我的错误是什么?
注意:改编自帖子的代码被接受为答案: