Couldn’t find anything on the webs so here is the issue: I have a cropper tool and I want to show the cropped image on this page. But because my functions.php
has a function that uses a header method, I had to use ob_start
in my file. That causes the problem that my image is not shown (it’s a question mark right now, not the right image).
Code:
<?php
ob_start();
require_once("includes/session.php");
require_once("includes/connection.php");
require("includes/constants.php");
require_once("includes/functions.php");
confirm_logged_in();
require_once("includes/header.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targ_w = $_POST['w'];
$targ_h = $_POST['h'];
$jpeg_quality = 90;
$src = $_POST['image'];
$ext = end(explode(".", $_POST['image']));
switch($ext) {
case 'jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor($targ_w, $targ_h);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r,null, $jpeg_quality);
$output = ob_get_contents();
break;
case 'png';
$img_r = imagecreatefrompng($src);
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/png');
imagepng($dst_r, null, 8);
$output = ob_get_contents();
break;
}
}
echo $output;
ob_end_clean();
?>
ob_start
starts output buffering.ob_end_clean
cleans the buffer and stops output buffering without sending anything to the client, so you basically discard any output.I think you meant to use
ob_end_flush
instead ofob_end_clean
, which sends the output buffer to the client instead of just ending buffering.Since you used
ob_get_contents
to put the output in a variable, you could opt to echo that variable after callingob_end_clean
, but that will make your script just larger, less clear and more memory-consuming, since you then have the contents of the entire image in the output buffer and in the$output
variable. So I think usingob_end_flush
really is the better option.