[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Returning  junk when using imagecreatefrompng()

Posted on 2013-01-29
4
Medium Priority
?
471 Views
Last Modified: 2013-01-31
I have a IIS server running php 5.4.9

I have added the extension path  of where I put the php_gd2.php and added "extension = php_gd2.php" on my php.ini

When I fun the following script:

<?php 
                 


$string = 'TEST TEXT';
$im     = imagecreatefrompng("images/backtoa_manager.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
                    
                    
                    ?>

Open in new window


I get a bunch of junk on my browser. that looks like png code.
‰PNG  IHDRÉ      Ÿ­Å IDATxœí{y´eWYçoOgºço¨¹*y•Te ¤ª ‘0…µƒ4 „]vŒ-.•W°qÓy¸Q”´¢µÁ4þ!Ø!C‘W$Á„ªT*©$j®TUjxӝÎ=çì±ÿØ÷ÞºU‡vi/×Êþã­óöÙgßþöoû›.ðr{¹½Ü^nÿ¾ùç½tÿjŒ¼ÜþåíÜLà_¼}ÌÿÍvÞp2çBHÿ!££IÀySœû°ÖzÒÃOàœëÏé?x)/|”É‘·çžÈyËtä•ñpiçXò“;?Õ`2r>§#¼GÞ éŸ<²ð18€Rznž 8s#Ôû2YËùÂñ#=ÿ„7˜oT¶Úä%3œÇ÷÷nÎý?€Azø̇O,`×ýâ5Œ1ZkÁ8çq „”ˆƒ°( ?¿Â  `œ1–+€RšeY–e~@RJRÊ¢A¹š«ªH„ Z™¨’ Œ¶¥¶J{*Ah­hgl¯(ä¶ô‹1ÆÖ³'n­-Ë@H9çœSÆZí8cpÊHRJýîrÎ˲BȲŒRzåÕûì³*µñ      ¦€\»B[c£iš¦i(ËÒo¨3ΖPÚ(:2‰Rc¬ˆ?©ÀÀ)µ eaaðŒê¶[8¾ rI’LÆéò oæù\Ï”ÆØùtEN•JÇ!t»Æ      q”Z ‡€±D0!úV£ñÝnW;ÀSkÔ|§³– Vôzâ8Niä­t b¬Ó@ÌbêM ΘRÖãÚ‚tcÌcó{d`à·ÎèÊ2Ïe       I¢˜Ç$li†`µîÏa‡lú½×x2²§fÿ|g}eê_ùáº4E§¼ê-Æ/®p›ÃpΡþÙ£•`áò…bïß<ŸLĪÔédzñ¯ eÙwXa½³òØ£GŠf1qÉä%7lôØ¢Þè;UÄRx/Ásìá“­£í°i£±pÍõ+& ÃЏᜯ[·Î͹¹¹ž—{—UêÕÂi<Ë»(qÐ̐RzÒ„J©öè뉡¯W,‡:-dË/_~ñôT_ ¶ƒÝÿû±Ê²*t©EÌ7¾ùò'¿°+]ž°Öé\½òÝWsAÕž¨7ÎB€hZø5rÎ9gƒ ëkGk-J@k†¡’Òà”åyîù¬•¾ `#`ý«-`Þ’c”˜¾é=e?3!ÄZëûU+'„x0}X÷¯£¾êú³y‡r†Gþø £ú'pÔ–'ñàèL:ëO´q~Û¶mÛû~í—¯zÇ¥AEPÎþŠ²ŒÓŠgBYMüÞpžuÚñdÜ|¡µcÇŽ·ýÄۏýâ5´2Ä’c©,K¾t÷âg~Òå91Æ;ƒ}E-D Vcj€ÓÏý¯¿ºç¦›nj6›“Ë'WVE…',Qª î99xp?€(Š8çÞý!Ë—“JЖ @’Š$#ÍJºí¶]´&''½Nʲ¬( ïçöíú´/kG©7zlº²¬úù?ýüOþç÷\F_Б™5¶}ª       ÙlN,›hi‰8hlõ…ð H”îÉRBî›”’RÇ€†cŽpJk;*£Tx{7Š„š‹þ‡<Ø0½þñ'¤¿ëDyT€1V9 ^xH]°Ñ•É1g¬¬å<`µ1R¯¼‘RpF†`m#ö!$´UJ§½JÛ³gϽíM„ Ì~ý[·Ür˯üÆmì.m·Nv¬¶ $]/¿v2^-Mi:‡ºs{çËvI9«_\¯¬ªC"VÚg¿úL¾”F–_=¹ü•“”‘…} ùb¯·íرCõäÞ{ö¬½ví™çΔíÒ9„µpÅUkV^¾Iàø±ãލhîˆPÜtÓ


Any suggestions?
0
Comment
Question by:Cree
  • 2
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 38833123
It's not junk - that's the PNG data!

The problem is that the browser doesn't realize that the data is a PNG image (as opposed to just html/text, which is what a PHP file normally returns)

You must send the correct MIME headers so that the browser renders the data as a PNG instead of just displaying it as raw text.

See the example here:

http://php.net/manual/en/function.imagepng.php

The appropriate call for the content-type header for a PNG graphic is:

header('Content-Type: image/png');
0
 

Author Comment

by:Cree
ID: 38836980
I tried that before but this is all i see. page looks like when header is added.
this is my code now


                    <?php 
                 
header('Content-Type: image/png');

$string = 'testing';
$im     = imagecreatefrompng("http://192.168.0.117/images/backtoc_manager.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
                    
                    
                    ?>

Open in new window

In case you wonder if that image doesn't exist...

image from url.
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 2000 total points
ID: 38838071
Your web browser is properly recognizing the image as a PNG now, but the data it is getting is corrupted.

It's probably all those spaces you have before the opening <?php. Those spaces are getting sent to the browser which is tripping it up.

It is extremely important that the very first bytes of your PHP file are the "<?php", and the very last bytes are the "?>", with no whitespace or newline characters before or after.

Also, making the last line of your script be "exit();" wouldn't hurt.

<?php 
                 
header('Content-Type: image/png');

$string = 'testing';
$im     = imagecreatefrompng("http://192.168.0.117/images/backtoc_manager.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);

exit();
                    
?>

Open in new window

0
 

Author Closing Comment

by:Cree
ID: 38839262
Thank you .  That did it. The image is showing up with text perfectly now.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question