imgrooot Posted March 29, 2017 Share Posted March 29, 2017 The image uploads fine but I get this weird gibberish code after I submit. It goes away after i refresh the page. JFIF>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality C $.' ",#(7),01444'9=82<.342C 2!!22222222222222222222222222222222222222222222222222" }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz?((((((((((((((((((((((((((((((((((((((((((((((((((((( Here's my code. if(isset($_POST['submit'])) { $errors = array(); $db->beginTransaction(); if(isset($_FILES['image'])) { if(!empty($_FILES['image']['name'])) { $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $type = $_FILES['image']['type']; $size = $_FILES['image']['size']; $ext = pathinfo($name, PATHINFO_EXTENSION); $size2 = getimagesize($temp); $width = $size2[0]; $height = $size2[1]; $upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 )); // Restrictions for uploading $maxwidth = 1500; $maxheight = 1500; $allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif'); // Recognizing the extension switch($type){ // Image/Jpeg case 'image/jpeg': $ext= '.jpeg'; break; // Image/Jpg case 'image/jpg': $ext= '.jpg'; break; // Image/png case 'image/png': $ext= '.png'; break; // Image/gif case 'image/gif': $ext= '.gif'; break; } // check if extension is allowed. if(in_array($type, $allowed)) { // Checking if the resolution is FULLHD or under this resolution. if($width <= $maxwidth && $height <= $maxheight) { if ($size <= 5242880) { $admin_dir = "images/"; if(!is_dir($admin_dir)){ mkdir($admin_dir, 0775, true); } // upload variables $path = $admin_dir . $upload . $ext; // Resizing according to extension. switch ($type) { // Image/Jpeg case 'image/jpeg'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagejpeg($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/Jpg case 'image/jpg'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagejpeg($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/png case 'image/png'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagepng($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/gif case 'image/gif'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagegif($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; } $insert_date = date('Y-m-d H:i:s'); $insert = $db->prepare("INSERT INTO images(path, date_added) VALUES(:path, :date_added)"); $insert->bindParam(':path', $path); $insert->bindParam(':date_added', $insert_date); $result_insert = $insert->execute(); if($result_insert == false) { $errors[] = 'Profile photo could not be uploaded.'; } if(empty($errors)) { $db->commit(); move_uploaded_file($temp, $path); $success = 'Profile photo has been updated.'; } else { $db->rollBack(); } } else { $errors[] = 'The image size is bigger than 5mb.'; } } else { $errors[] = 'The image resolution exceeds the limit of 1500px by 1500px.'; } } else { $errors[] = 'You have uploaded a forbidden extension.'; } } else { $errors[] = 'An image is required.'; } } } ?> <form id="upload-form" action="" method="post" enctype="multipart/form-data"> <label>175px by 175px</label> <input type="file" name="image" /> <input type="submit" id="upload-submit" value="Upload Image" name="submit"> </form> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 30, 2017 Share Posted March 30, 2017 Well, you're serving the response as standard text/html, but for some strange reason, you've then decided to create an empty image and output it with the imagejpeg() function. That's how an image interpreted as HTML looks like. A lot of the code is plain weird. For example, why on earth to you use a transaction for a single query and roll it back when there's an error? Just wait until you've finished all error checking and then insert the data. Quote Link to comment Share on other sites More sharing options...
Solution imgrooot Posted March 30, 2017 Author Solution Share Posted March 30, 2017 Well, you're serving the response as standard text/html, but for some strange reason, you've then decided to create an empty image and output it with the imagejpeg() function. That's how an image interpreted as HTML looks like. A lot of the code is plain weird. For example, why on earth to you use a transaction for a single query and roll it back when there's an error? Just wait until you've finished all error checking and then insert the data. it's an old image upload code that I copied from a tutorial and then mixed it with my own. The original code actually created 2 images. One original and the other a thumb image. So I removed that thumb code but it looks like there might be some left and that might be causing it. Yep so this piece of code was causing it. I removed it in all four cases and no more gibbersh code. It's all good now. $thumb = imagecreatetruecolor($width, $height); imagegif($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); In terms of why I am using transaction for a single query, i didn't realize it till now. Some of the other pages have multiple queries that require the transaction so naturally I copied the code. Thanks for pointing that out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.