0

I have run into a problem using php in Zend Framweork to dynamically scale images for return as mime-type image/jpeg.

The problem manifests itself as firefox reporting 'cannot display image because it contains errors'. This is the same problem reported in: return dynamic image zf2

To replicate the problem, I removed any file IO and copied code from a similar stack overflow example verbatim (in a zend FW action controller):

$resp = $this->getRespose();
$myImage = imagecreate(200,200);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
ob_start();
imagejpeg($myImage);
$img_string = ob_get_contents();
$scaledSize = ob_get_length();
ob_end_clean();
imagedestroy($myImage);

$resp->setContent($img_string);
$resp->getHeaders()->addHeaders(array(
       'Content-Type' => $mediaObj->getMimeType(),
          'Content-Transfer-Encoding' => 'binary'));                
$resp->setStatusCode(Response::STATUS_CODE_200);
return $resp;

When I use wget to capture the jpeg response, I notice a '0A' as the first byte of the output rather than the field separator 'FF'. There is no such '0A' in the data captured in the buffer, nor in the response's content member. Attempting to open the wget output with GIMP fails, unless I remove the 0A. I am guessing that Zend FW is using the line-feed as a field separator for the response fields vs. the content, but I'm not sure if that is the problem, or if it is, how to fix it.

My response fields look OK:

HTTP request sent, awaiting response... 
HTTP/1.1 200 OK
Date: Sat, 02 Jun 2018 23:30:09 GMT
Server: Apache/2.4.7 (Ubuntu) OpenSSL/1.0.1f
Set-Cookie: PHPSESSID=nsgk1o5au7ls4p5g6mr9kegoeg; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Transfer-Encoding: binary
Content-Length: 1887
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: image/jpeg

Here is the dump of the first few bytes of the wget with the jpeg stream that fails:

00000000 0a ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 |.......JFIF.....|

00000010 60 00 60 00 00 ff fe 00 3e 43 52 45 41 54 4f 52 |......>CREATOR|

Any idea where the '0A' is coming from? I am running zend framework 2.5.1, PHP 7.2.2

ebcken
  • 11
  • 2
  • This is usually caused by whitespace at the start or end of PHP files you're including. – Tim Fountain Jun 04 '18 at 18:37
  • Thank you - but there is no view script being rendered - everything else is vanilla zend framework code. The response object is just being pulled from the action controller, headers and content set, and the response returned without a view. I've tried tracing through the framework's event handling, but I'm not sure of where to look for the code that actually outputs the response object. Could you suggest where, in the framework - is the code that actually echoes the response content to the browser? – ebcken Jun 05 '18 at 01:16
  • I didn't say anything about a view script. It's still most likely to be white space in one of the PHP files included before the response is sent to the browser. Unfortunately this can be a little tricky to track down. – Tim Fountain Jun 05 '18 at 09:03

1 Answers1

0

Thank you Tim Fountain.

I did finally find the offending file buried in some doctrine entities I had created. Sure enough, a stray "?>" with an empty line.

Much appreciated

ebcken
  • 11
  • 2