How to return a file with image content in php?
<p>So I have a php file that takes in a image file and returns a image file back. The problem is that it returns a cluster of characters when I was expecting a file. How do I fix this? Sorry I am new to Php.</p>

<p>So I set the content type in the header and use this method imagejpeg($newImage);</p>

<pre><code>&lt;?php
error_reporting(0);
$newSize = $_POST['newSize'];
if(empty($newSize)){
    header("Status: 400 Bad Request", true, 400);
    exit();
}
$extension = strtolower(strrchr($_FILES['image']['name'], '.'));
$imageInfo = getimagesize($_FILES['image']['tmp_name']);

switch($extension){
    case '.jpg':
    case '.jpeg':
        $image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
        break;
    case '.gif':
        $image = imagecreatefromgif($_FILES['image']['tmp_name']);
        break;
    case '.png':
        $image = imagecreatefrompng($_FILES['image']['tmp_name']);
        break;
    default:
        header("Status: 400 Bad Request", true, 400);
        exit('');
}

$newImage = imagecreatetruecolor($newSize, $newSize);

if(empty($newImage) || empty($image)){
    header("Status: 400 Bad Request", true, 400);
    exit();
}

imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newSize, $newSize, $imageInfo[0], $imageInfo[1]);

header('Content-Type: ' . $imageInfo['mime']);

switch($extension){
    case '.jpg':
    case '.jpeg':
        imagejpeg($newImage);
        break;
    case '.gif':
        imagegif($newImage);
        break;
    case '.png':
        imagepng($newImage);
        break;
}
?&gt;
</code></pre>
