1

I am getting the following error when I run my script php cannot be displayed because it contains errors

The script, takes a file, resizes it outputs it to screen and a file.

url_example: localhost/testimage.php?img=img_592_121_200.jpg

I have read most posts and tried proposed solutions. all Failed

The file will not output to screen/browser but it can gets save to a file.

Here is the code, I stripped all the white space off and empty lines. and hard-coded the file name inside the script

**Am starting to suspect it is my setup, but am not sure to even to how/where to start looking

Edit : simplified to go to output to screen only

testimage.php

<?php
//session_start();
//ob_start();
ini_set("display_errors", "1");
error_reporting(E_ALL); 
Header('Content-type:image/jpeg');
//$cacheFile="mypicXXX.jpg";
//$cache_dir="cache/1/592/";
$img_dir="webspace/1/592/";
$img="img_592_121.jpg";
$id="$img_dir$img";
$src=imagecreatefromjpeg($id);
$new_width=200;
$new_height=77;
$width=600;
$height=1001;
$w_src=600;
$h_src=1001;
$img=imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($img,$src,0,0,0,0,$new_width,$new_height,$w_src,$h_src);
imagejpeg($img,NULL,45);
//$cacheFile=$cache_dir.$cacheFile;
//imagejpeg($img,$cacheFile,45); 
imagedestroy($src);
imagedestroy($img);
//ob_end_flush();
?>
rudy
  • 311
  • 3
  • 14
  • So, what exactly did you try? The first suggestion should have been to remove the `header()` line and look at the error messages that probably are shown. – Fabian Schmengler Feb 17 '13 at 09:53
  • Do you close the output buffer started by `ob_start()`? Another case is that error message seems strange. Try to add `error_reporting( E_ALL ); ini_set( 'display_errors', 1 )` on top of your page – kjetilh Feb 17 '13 at 09:59
  • I tried removing the header line, The screen just displays gibberish. I tried removing ob_start, Tried Error_reporting is not generating/displaying any errors i did read that is does not like '' in imagejpeg($img,'',45); so i set it to imagejpeg($img,NULL,45);, did not help – rudy Feb 17 '13 at 10:14
  • Something else I should mention, is that this script used to work before. I just noticed today it was not working. About a month ago I upgraded to PHP Version 5.4.7 , Apache 2.4, GD Version bundled (2.0.34 compatible) this is running on my localhost, home computer windowsXP – rudy Feb 17 '13 at 10:41

2 Answers2

1

Fixed. The fix (for me at least)was to save the file as "UTF-8 - NO NOM". Before I had it as "UTF-8"

This whole file format gets me lost, I need to read up on it one of these days

Thanks for all the help!!!

rudy
  • 311
  • 3
  • 14
  • Ah, that explains things. :) I Also reckon you mean UTF-8 NO BOM (byte order mark). Since its a character that appears at the top of the file you should always save your files in UTF-8 without BOM when you use header() in that file or that file is included by another php file. Some more info here: https://bugs.php.net/bug.php?id=22108 – am_ Feb 18 '13 at 07:32
0

Have you checked that no output is sent before the header? A couple of things you can try:

  1. Move your header() function to the top.
  2. Remove the caching (ob_start) and see if you can make it work without that first.
  3. As you use ob_start, I guess you want to also use ob_end_flush(); at the end?
  4. Try to use NULL as second param to imagejpeg() to do a RAW output instead of writing to a file. (could be related to write permissions)

UPDATE Are you sure you checked that the file your pointing to exists/or that the path is valid? this works on my localhost, running default xamp installation (PHP3.5.1)

ini_set("display_errors", "1");
error_reporting(E_ALL);
Header('Content-type:image/jpeg');
//$cacheFile="mypicXXX.jpg";
//$cache_dir="cache/1/592/";
$img_dir="webspace/1/592/";
$img="test.jpg";
$src=imagecreatefromjpeg($img);
$new_width=200;
$new_height=77;
$width=600;
$height=1001;
$w_src=600;
$h_src=1001;
$img=imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($img,$src,0,0,0,0,$new_width,$new_height,$w_src,$h_src);
imagejpeg($img,NULL,45);
//$cacheFile=$cache_dir.$cacheFile;
//imagejpeg($img,$cacheFile,45);
imagedestroy($src);
imagedestroy($img);
am_
  • 2,378
  • 1
  • 21
  • 28
  • Moved header to top, removed ob_start. and no, nothing is being sent before header. Still not working – rudy Feb 17 '13 at 10:12
  • Well then It sounds like PHP can't find your file $cacheFile="mypic.jpg"; make sure you use the correct path. I also updated my answer with another thing to check for. – am_ Feb 17 '13 at 10:22
  • it is writing to the cache file ok, and placing it in my directory, i just can not get it to output to screen – rudy Feb 17 '13 at 10:25
  • I have changed/tried NULL as the second paramater, still failing – rudy Feb 17 '13 at 10:29
  • I just tested this on my localhost and it worked fine. However I didn't use any path to my image - just the image filename, and placed it in the same folder as the php script. Updated my answer above with some code. – am_ Feb 17 '13 at 18:08
  • I am now thinking it is my setup. I just installed/updated to the latest of everything (Xampp v3.1.0.3.1.0) PHP Version 5.4.7 , Apache 2.4, GD Version bundled (2.0.34 compatible) I dowloaded some simple GD examples and they are giving me the same Error. In all the Image is ok to generate to a file but won't generate to screen – rudy Feb 17 '13 at 20:08