1

Good day guys,

I inherited some legacy code that reads a BLOB from the database, parses it and returns the jpeg stream to be displayed in the browser. The legacy code used to run on PHP 5.5, it has since been deployed on a PHP 7.1 server where I am running into the issue.

I have made sure that the gd library for php is installed and loaded in Apache, I can verify that it is enabled and working with the php info file. I have also dumped the value from the DB and made sure the blob is a valid image, which it is. For some reason or another I cannot get the function to provide the image.

Here is the code for the function after the data has been retrieved from the DB and stored in $image

$image = imagecreatefromstring($image);

$originalWidth = imagesx($image);
$originalHeight = imagesy($image);

header ( 'Content-type:image/jpeg' );
if($originalWidth == $width && $originalHeight == $height)
{
  if($thumb && $width >= $originalWidth) echo imageautocrop($image);
  else echo imagejpeg($image);
}
else
{
  $ratio = $originalHeight/$originalWidth;

  $height = $width*$ratio;

  $height = round($height);
    $new = imagecreatetruecolor($width, $height);

    imagecopyresampled($new, $image, 0, 0, 0, 0, $width, $height, imagesx($image),imagesy($image));
    imageantialias($new, true);

  if($thumb) imageautocrop($image);
  else imagejpeg($new, null, $quality);

  imagedestroy($new);
} 

I have tried using the exact dimensions as well as the cropped dimensions, neither of which work.

Tachyon
  • 2,171
  • 3
  • 22
  • 46
  • What is the error exactly, and where does it occur. Is it on the first function of `imagecreatefromstring`? – IncredibleHat Jul 10 '18 at 13:43
  • @IncredibleHat the error I am getting in my browser is, "The image ... cannot be displayed because it contains errors". It is occurring when I echo imagejpeg – Tachyon Jul 10 '18 at 13:44
  • Share your some part of BLOB... – Pradeep Rajput Jul 10 '18 at 13:46
  • Which one though, there are multiple. First if? second? And does php throw any notices or warnings when you enable full reporting? Add `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` to the top temporarily to see if there are some underlying warnings going on. – IncredibleHat Jul 10 '18 at 13:46
  • @IncredibleHat - It is happening on the first one, the second one fails with Call to undefined function imageantialias() which is strange because I have included the GD extension to php and apache. – Tachyon Jul 10 '18 at 13:49
  • Ok. That would explain the damaged image, because PHP is spitting out errors (which are not image data hehe). Gotta figure out why GD isn't setup so those functions exist. Do a `print_r(gd_info());` on a blank script and hit it. – IncredibleHat Jul 10 '18 at 13:50
  • 1
    @IncredibleHat - I have done that, the output I am getting is `Array ( [GD Version] => 2.2.5 [FreeType Support] => 1 [FreeType Linkage] => with freetype [GIF Read Support] => 1 [GIF Create Support] => 1 [JPEG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => 1 [XBM Support] => 1 [WebP Support] => 1 [JIS-mapped Japanese Font Support] => ) ` – Tachyon Jul 10 '18 at 13:54
  • Ok, this may help: https://stackoverflow.com/a/5756241/2960971 – IncredibleHat Jul 10 '18 at 13:56
  • I am getting this as the output from the imagejpeg: https://pastebin.com/dgAxgh1x @IncredibleHat - Can't recompile PHP, this is in a shared hosting environment – Tachyon Jul 10 '18 at 14:18
  • Its the only other answer I could find. Sorry. – IncredibleHat Jul 10 '18 at 14:21
  • @IncredibleHat - No problem, appreciate the help. Hopefully there is another fix. – Tachyon Jul 10 '18 at 14:22

0 Answers0