0

I want to make a banner with the images saved in database, but banner get a strange color when $imagesB take images from database , when i put the link of one photo in $imagesB, this keep the real color and i don't know why image take a greener color when i take from database.

this is the code

<?php 
  require('db.php');
  session_start();

  $w = 480*10;
  $h = 270*5;

  header("Content-Type: image/png");

  $im = imagecreate($w, $h) or die("Cannot initialize GD extension");
  $canvas = imagecolorallocate($im, 245, 245, 245);

  $sql = "SELECT * FROM Cinema";
  $result = mysqli_query($mysqli, $sql);
  $results = array();

  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      $results[] = $row['Coperta'];
    }
  }

  for($i=0;$i<10;$i++){
  // here i also tried to obtain the photos directly from mysql with $imagesB ="http://projects.rusticwoodromania.com/uploads/".$row['Coperta']; but the same results
    $imagesB = "http://projects.rusticwoodromania.com/uploads/".$results[$i];
  // when this link it's "http://projects.rusticwoodromania.com/uploads/1535026678_545047747.png"; the image look normal not with this kind of green


    $imgB = imagecreatefrompng($imagesB);
    $a = $i*480;

    for($j=0;$j<5;$j++){
      $b = $j*270;
      imagecopyresampled($im, $imgB, $a, $b, 0, 0, 480, 270, 480, 270);
    }
  }

  imagepng($im);

?>

1.Link with "greener" image

2.Link with normal image

  • Try using `imagecolorallocate($im, 0, 0, 0);` instead. – Raymond Nijland Aug 25 '18 at 21:45
  • These two links are different: `$imagesB ="http://projects.rusticwoodromania.com/uploads/".$row['Coperta'];` and `$imagesB = "http://projects.rusticwoodromania.com/uploads/".$results[$i];` – Chukwuemeka Inya Aug 26 '18 at 00:00
  • Do you really need a png? i think it might be related to poor server libraries for that purpose. have you tried it with jpeg? – Erubiel Aug 26 '18 at 21:26
  • I tell you this cause i see you are using GD and i had a similar problem, while saving as png. I really needed a png so i tried with this library http://php.net/manual/en/book.imagick.php but i had to install it and reboot the server. But i used it with this library http://image.intervention.io/, but this library calls the imagick one, so maybe using directly imagick should work. – Erubiel Aug 26 '18 at 21:31
  • Possible duplicate of [PHP imagecreatefrompng losing color](https://stackoverflow.com/questions/18845160/php-imagecreatefrompng-losing-color) – Jasper Aug 26 '18 at 21:42
  • First step is to take a look at your files: they are all called '.png' but some are actually jpg files. You'll notice ```imagecreatefrompng``` won't work on ```1535019389_992041950.png``` for instance, but```imagecreatefromjpeg``` will. – Jasper Aug 26 '18 at 21:44

0 Answers0