• PHP Help General Help
  • [RESOLVED] ImageCreateFromjpeg in GD returning bad quality images for some reason. Help?!?

I'm currently writing a code for a dynami image generating script to return XBox Live gamercards. I've got everything working almost perfectly apart from imagecreatefromjpeg (which I am using to return the thumbnail images of recently played games). My code is below along with a sample image for you to see what I mean;

<?php

/**
 * XBL gamercard
 *
 * Website: http://www.360elites.net
 * Created by euantor
 */


Header ("Content-type: image/png");


#pulling XBL info using the Duncan Mckenzi XBL API.

$name = $_GET["gamertag"];
$name = str_replace(" ", "+", $name);



$url = "http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=".$name;

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);

$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);



$gamertag = $xml->Gamertag;
$status = "Status:  ".$xml->PresenceInfo->StatusText;
$gamerscore = "Gamerscore:  ".$xml->GamerScore;
$gamerpic = $xml->TileUrl;
$gamerpic = imageCreateFromPNG($gamerpic);
$country = $xml->Country;
$zone = "Zone:  ".$xml->Zone;


$game1 = $xml->RecentGames->XboxUserGameInfo[0]->Game->Image32Url;
$game1 = imageCreateFromjpeg($game1);

$game2 = $xml->RecentGames->XboxUserGameInfo[1]->Game->Image32Url;
$game2 = imageCreateFromjpeg($game2);

$game3 = $xml->RecentGames->XboxUserGameInfo[2]->Game->Image32Url;
$game3 = imageCreateFromjpeg($game3);

$game4 = $xml->RecentGames->XboxUserGameInfo[3]->Game->Image32Url;
$game4 = imageCreateFromjpeg($game4);

$img_handle = imageCreateFromPNG("./images/card.png");
$imgcolor = ImageColorAllocate ($img_handle, 153, 153, 153);
$imgcolorhead = ImageColorAllocate ($img_handle, 255, 255, 255);
$black = imagecolorallocate($img_handle, 0, 0, 0);
$grey = imagecolorallocate($img_handle, 56, 51, 49);
$font = 'arial.ttf';
$fontitalic = 'ariali.ttf';
$fontbold = 'ariblk.ttf';


#Gamertag
imagettftext($img_handle, 11, 0, 4, 15, $black, $fontbold, $name);

#Gamerpic
imagecopymerge($img_handle, $gamerpic, 4, 25, 0, 0, 64, 64, 100);

#Status
imagettftext($img_handle, 9, 0, 75, 40, $grey, $font, $status);

#Gamerscore
imagettftext($img_handle, 9, 0, 75, 55, $grey, $font, $gamerscore);

#Zone
imagettftext($img_handle, 9, 0, 75, 70, $grey, $font, $zone);




imagecopymerge($img_handle, $game1, 4, 95, 0, 0, 32, 32, 100);
imagecopymerge($img_handle, $game2, 39, 95, 0, 0, 32, 32, 100);
imagecopymerge($img_handle, $game3, 73, 95, 0, 0, 32, 32, 100);
imagecopymerge($img_handle, $game4, 108, 95, 0, 0, 32, 32, 100);


ImagePng ($img_handle);
ImageDestroy ($gamerpic);
ImageDestroy ($game1);
ImageDestroy ($img_handle);

?>

Example image: http://www.360elites.net/gamercard/index.php?gamertag=etorano

As you can see, the images at the bottom are of really poor quality whilst the rest of it is perfectly sharp.

I'd love some advice on what I'm doing wrong and/or what I should do to combat the quality problems.

Thanks in advance,
euantor

    You might try using imagecopyresampled() instead of imagecopymerge().

      EDIT: That didn't work. The new code is:

      <?php
      
      /**
       * XBL gamercard
       *
       * Website: http://www.360elites.net
       * Created by euantor
       */
      
      
      Header ("Content-type: image/png");
      
      
      #pulling XBL info using the Duncan Mckenzi XBL API.
      
      $name = $_GET["gamertag"];
      $name = str_replace(" ", "+", $name);
      
      
      
      $url = "http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=".$name;
      
      $ch = curl_init($url);
      
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
      
      $data = curl_exec($ch);
      curl_close($ch);
      
      $xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
      
      
      
      $gamertag = $xml->Gamertag;
      $status = "Status:  ".$xml->PresenceInfo->StatusText;
      $gamerscore = "Gamerscore:  ".$xml->GamerScore;
      $gamerpic = $xml->TileUrl;
      $gamerpic = imageCreateFromPNG($gamerpic);
      $country = $xml->Country;
      $zone = "Zone:  ".$xml->Zone;
      
      
      $game1 = $xml->RecentGames->XboxUserGameInfo[0]->Game->Image32Url;
      $game1 = imageCreateFromjpeg($game1);
      
      $game2 = $xml->RecentGames->XboxUserGameInfo[1]->Game->Image32Url;
      $game2 = imageCreateFromjpeg($game2);
      
      $game3 = $xml->RecentGames->XboxUserGameInfo[2]->Game->Image32Url;
      $game3 = imageCreateFromjpeg($game3);
      
      $game4 = $xml->RecentGames->XboxUserGameInfo[3]->Game->Image32Url;
      $game4 = imageCreateFromjpeg($game4);
      
      $img_handle = imageCreateFromPNG("./images/card.png");
      $imgcolor = ImageColorAllocate ($img_handle, 153, 153, 153);
      $imgcolorhead = ImageColorAllocate ($img_handle, 255, 255, 255);
      $black = imagecolorallocate($img_handle, 0, 0, 0);
      $grey = imagecolorallocate($img_handle, 56, 51, 49);
      $font = 'arial.ttf';
      $fontitalic = 'ariali.ttf';
      $fontbold = 'ariblk.ttf';
      
      
      #Gamertag
      imagettftext($img_handle, 11, 0, 4, 15, $black, $fontbold, $name);
      
      #Gamerpic
      imagecopymerge($img_handle, $gamerpic, 4, 25, 0, 0, 64, 64, 100);
      
      #Status
      imagettftext($img_handle, 9, 0, 75, 40, $grey, $font, $status);
      
      #Gamerscore
      imagettftext($img_handle, 9, 0, 75, 55, $grey, $font, $gamerscore);
      
      #Zone
      imagettftext($img_handle, 9, 0, 75, 70, $grey, $font, $zone);
      
      
      
      
      imagecopyresampled($img_handle, $game1, 4, 95, 0, 0, 32, 32, 32, 32);
      imagecopyresampled($img_handle, $game2, 39, 95, 0, 0, 32, 32, 32, 32);
      imagecopyresampled($img_handle, $game3, 73, 95, 0, 0, 32, 32, 32, 32);
      imagecopyresampled($img_handle, $game4, 108, 95, 0, 0, 32, 32, 32, 32);
      
      
      ImagePng ($img_handle);
      ImageDestroy ($gamerpic);
      ImageDestroy ($game1);
      ImageDestroy ($img_handle);
      
      ?>
      

      yet the images still look weird:

      http://www.360elites.net/gamercard/index.php?gamertag=etorano

        You're still using imagecopymerge():

        #Gamerpic
        imagecopymerge($img_handle, $gamerpic, 4, 25, 0, 0, 64, 64, 100);
          bradgrafelman;10952271 wrote:

          You're still using imagecopymerge():

          #Gamerpic
          imagecopymerge($img_handle, $gamerpic, 4, 25, 0, 0, 64, 64, 100);

          Yes, for that one I am. However, that one is showing correctly - it's the JPEGs that I'm having difficulties ($game1, $game2, $game3, $game4). Therefore, I didn't change that one.

            I've looked into this a little more and it's actually the gamerpic being affected too - I just couldn't see it on mine due to it consisting of only two colours.

            So basically, all images being fetched are being fetched in suh a way that their colours are washed out/wrong. Could this be due to my php.ini or php version or something? I've tried using imagecopy() imagecopyresampled() imagecopymerged() and all the rest to no avail...

              I'm guessing that the base PNG image is palette-based. GD will be trying to force the JPEGs to use that palette when it copies them.

              Try creating a truecolour image and copy the base image into that; then carry on from there.

                Ok, I'll give it a go. i didn't think about that one, cheers!

                  Yup, that sorted it. Now to just add the BG onto the color 🙂

                  Thanks an absolute tonne! I've been puzzling over this for 2 days now!

                    Zstar Electronic Co.Ltd, Sell fire cards for DS/NDSL/NDSi, also have Wii, DSiLL, NDSi, NDSL, PSP2000, PSP3000, PS2, PS3, PSP go, PSP, Xbox360 accessories, all kinds of phones are available
                    www.zstar.hk
                    WWW.TIGERSUPERMALL.COM

                      Write a Reply...