I have a form to upload a file:
<form method="POST" action="upload-image.php">
<input type="file" name="file"><br>
<input type="submit" value="Submit">
</form>
And a PHP code to resize the uploaded file (image), convert it to JPG, and display it in the browser.
<?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);
?>
It is not working. It displays a blank image. What is my error?
Note: Code adapted from the post accepted as answer at:
How do I resize and convert an uploaded image to a PNG using GD