PHP imagejpeg forcingly open file after save

50 Views Asked by At

i'm trying to make simple code for uploading image > resize it > save in directory > and show in browser after resize. Problem is that file is being saved but none of my echo works and i got grey background with info that image contains errors and cannot be displayed.

When imagejpeg parameters are set to imagejpeg($imageP, null, 100) the picture shows on this grey background but still the rest of page is missing.

Resize function:

function resizeProportionallyByPx($width, $height, $image)
{
header('Content-Type: image/jpg');
list($widthOriginal, $heightOriginal) = getimagesize($image);
$ratioOriginal = $widthOriginal/$heightOriginal;

if($width/$height > $ratioOriginal){
    $width = $height * $ratioOriginal;
}else{
    $height = $width * $ratioOriginal;
}

$imageP = imagecreatetruecolor($width, $height);
$imageJ = imagecreatefromjpeg($image);

imagecopyresampled($imageP, $imageJ, 0, 0, 0, 0, $width, $height, $widthOriginal, $heightOriginal);
imagejpeg($imageP, '../imagesResized/demo.jpg', 100);
}

Code:

if (isset($_FILES['imageToUpload']) && isset($_POST['width']) && isset($_POST['height']) && is_numeric($_POST['width']) && is_numeric($_POST['height'])) {

    $uploadDir = "../images/";
    $uploadFile = $uploadDir . basename($_FILES['imageToUpload']['name']);
    $width = $_POST['width'];
    $height = $_POST['height'];

    if(move_uploaded_file($_FILES['imageToUpload']['tmp_name'], $uploadFile))
    {
        $size = formatSizeUnits($_FILES['imageToUpload']['size']);
        $image = $_FILES["imageToUpload"]["name"];
        $imageOriginalPath = "../images/" . $image;

        resizeProportionallyByPx($width, $height, $imageOriginalPath);
        $imageResizedPath = '../imagesResized/demo.jpg';

        echo "Uploaded file info:" . '<br />'
            . "File name: " . $_FILES['imageToUpload']['name'] . " is valid and was successfully uploaded.<br />"
            . " File type: " . $_FILES['imageToUpload']['type'] . '<br />'
            . " File size: " . $size . '<br />'
            . " Temp file: " . $_FILES["imageToUpload"]["tmp_name"] . '<br />'
            . "<img src=" . "$imageOriginalPath><br />"
            . "Image after resize: <br />"
            . "<img src=$imageResizedPath>" . "<br />";

    }
    else
    {
        echo "Possible file upload attack, upload has failed." . "Error: " . $_FILES['imageToUpload']['error'];
    }
}

chmod and chow are configured. Hope someone can help me out with my first question ever, cheers.

0

There are 0 best solutions below