1

I am debugging a PHP application that handles image resources. I would like to see the output ($dst_image as per the PHP manual's jargon), but the code is not in a place that I could simply output it to the browser. Would the best debugging procedure be to write $dst_image to a file, and to load that file in the browser? Any other ideas?

Thanks.

2
  • Why can't you output to a browser? An echo anywhere should do that. Commented Apr 23, 2012 at 15:15
  • I cannot output to a browser as other code has already output text to a browser. The image-handling bit happens in the background. If the application used output buffering I could do it, but also it cannto be refactored as such. Commented Apr 23, 2012 at 15:32

1 Answer 1

1

See Example #1 and #2, imagejpeg will output the jpeg data.

You need to do few tings:

  1. set headers for image header('Content-Type: image/jpeg');
  2. output your image data
  3. Make sure you are not outputting any data except the image

At the end you should end up with something like this:

<?php

// Get new dimensions
// Resample
// etc...

// set header so browser can render image properly
header('Content-Type: image/jpeg');

// Output
imagejpeg($image, null, xxx);
// [or] 
echo file_get_contents($pathToJpgImage);


If you find your self in a situation where current request outputs data and you cannot output image... You can inject the base64 encoded image data into the HTML by using <img src="data:image/jpeg;base64,..." />. See php documentation on base64_encode for images.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but for technical reasons (output already sent) the image cannot be simple sent to the browser unless there is a way to negate all the text previously output.
Although I doesn't address the issue that I bring up, I am accepting this as the accepted answer as it is the canonical solution for the general case. When this solution doesn't work, then one must write the image to a file, as I had suspected. Thank you Alex.
@dotancohen my apologies, it was not clear to me from your question... See 2nd part of my answer, it should do the trick.
Alex, that is terrific! It is exactly the fix that I needed but was afraid would not exist! That is a clever and creative solution, I would call it witty if one could describe code as such. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.