Jump to content

Anyone familiar with the GD Library?


Moron

Recommended Posts

I'm trying to display employee photos which are stored in the database as binary.

As a test, I can put the following code sample (from php.net) as the very [b]first[/b] thing on the page...

[code]<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
      . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
      . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
      . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if ($im !== false) {
   
    imagejpeg($im);
}
else {
    echo 'An error occurred.';
}
?>[/code]

...and I get:

[img]http://mywebpages.comcast.net/techie4/phprules.jpg[/img]

So the GD library is working.

...but I get [b]nothing[/b] else on the page.

If I put the above code anywhere except at the very top of the page, then I just get the ASCII garbage as written.

Anyone know why I can't just plug it where I want into the page? I've already looked at the header issue and that's not the problem.
Link to comment
https://forums.phpfreaks.com/topic/27980-anyone-familiar-with-the-gd-library/
Share on other sites

Yes, it is a header problem. You need to put the code either in a seperate script or in a section of your main script that exits right after sending the data. The script would be invoked via the HTML <img> tag. In the script, you should issue
[code]<?php
header('Content-type: image/jpeg');
?>[/code]

before sending the image data.

Ken
[quote author=kenrbnsn link=topic=115765.msg471453#msg471453 date=1164128008]
Yes, it is a header problem. You need to put the code either in a seperate script or in a section of your main script that exits right after sending the data. The script would be invoked via the HTML <img> tag. In the script, you should issue
[code]<?php
header('Content-type: image/jpeg');
?>[/code]

before sending the image data.

Ken
[/quote]

Thanks a ton, Ken! One question: when you say a "seperate script," does that mean that I could just have it in an .inc file and call that file?

[quote author=kenrbnsn link=topic=115765.msg471495#msg471495 date=1164132680]
Please post the code of "showpic.inc" and how you are invoking it.

Ken
[/quote]

Here you go. It's showpic.inc. It's just the sample from php.net:

[code]
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
      . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
      . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
      . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if ($im !== false) {
   
    imagejpeg($im);
}
else {
    echo 'An error occurred.';
}
?>[/code]

I'm invoking it by:

[code]<?php
include('showpic.inc');
?>[/code]

You may notice that I don't have the "header" line in the code. That's because I have...

[code]
header('Content-Type: image/jpeg');[/code]

...at the top of my page. If I put it in showpic.inc also, it conflicts and throws an error.

this is how you export a php made image into html, it has to be put out as a html string.
[code]
<img src="showpic.php">
[/code]

then in your showpic.php
[code]
<?
header('Content-Type: image/jpeg');
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
      . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
      . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
      . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if($im !== false) imagejpeg($im);
else echo 'An error occurred.';
?>
[/code]
Try this simple script. Everything in one script, no includes:
[code]<?php
if (isset($_GET['p'])) {
  $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
      . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
      . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
      . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
  $data1 = base64_decode($data);

  $im = imagecreatefromstring($data1);
  if ($im !== false) {
      header('Content-Type: image/jpg');
      imagejpeg($im);
  }
  exit();
}
for($i=0;$i<10;$i++)
    echo '<img src="' . $_SERVER['PHP_SELF'] . '?p=1"> ... ' . $i .' ... <br>';
?>[/code]

Ken
[quote author=kenrbnsn link=topic=115765.msg471544#msg471544 date=1164136118]
Try this simple script. Everything in one script, no includes:
[code]<?php
if (isset($_GET['p'])) {
   $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
   $data1 = base64_decode($data);

   $im = imagecreatefromstring($data1);
   if ($im !== false) {
      header('Content-Type: image/jpg');
      imagejpeg($im);
   }
   exit();
}
for($i=0;$i<10;$i++)
     echo '<img src="' . $_SERVER['PHP_SELF'] . '?p=1"> ... ' . $i .' ... <br>';
?>[/code]

Ken
[/quote]

This works if it's the absolute first thing on the page. Otherwise, it just shows:

...0...
...1...
etc....

with X's and no image.
[quote author=taith link=topic=115765.msg471522#msg471522 date=1164134217]
this is how you export a php made image into html, it has to be put out as a html string.
[code]
<img src="showpic.php">
[/code]

then in your showpic.php
[code]
<?
header('Content-Type: image/jpeg');
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if($im !== false) imagejpeg($im);
else echo 'An error occurred.';
?>
[/code]
[/quote]

Thanks, but this just gives me the X and no picture.

:-[

EDIT! - Typo on my part! I put showpic.[b]inc[/b], not showpic.[b]php[/b]. IT SHOWS!

Thanks!

Now I need to make it point to my database pics!

sorry... i'm not going to be much more help here... i only ever worked with simple php images (stick men(dont laugh ;D))...
however you no longer need the whole
[code]<?
if($im !== false)
else echo 'An error occurred.';
?>[/code]
as pictures dont output text the same way :-)
Okay, I'm thiiiissss close now. Here are the contents of the showpic.php file:

[code]
<?php
header('Content-Type: image/jpeg');
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if($im !== false) imagejpeg($im);
else echo 'An error occurred.';
?>

<?php
session_start();
$_SESSION['empcode'] = $empcode;
$_SESSION['middle'] = $middle;
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['leavehours'] = $leavehours;
?>

<?php
//CONNECTION STATEMENT. Can also be plugged into an .inc file, then just refer to the .inc file.

$conn = mssql_connect("ORSERVI","USERNAME","PASSWORD") or die ("Could Not Connect to Database.<br>Please Notify Computer Services.");
mssql_select_db('PerMaster',$conn);

?>

<?php


$RESULTDS=mssql_query("SELECT DISTINCT LH.[Employee Number], M2.[HRYRAT], M2.[EMPNO], M2.[MANLAP], M2.[PAYCTR], M2.[MANLAC], M2.[MANLTC], M2.[MSKLAB], M2.[MSKLTC], M2.[NAMEMI], M2.[NAMEL], M2.[NAMEF], EH.[DATE], EH.[ENETPA], LH.[LOCC], EH.[EGRSER], EH.[EREGHR], EH.[EDEDUC], EH.[EROTHR], EH.[EFWHD], EH.[EPEDAT], EH.[ESSDED], EH.[EHOSPD], EH.[ELIFED], EH.[ECRUD], M2.[POSITN], M2.[MCTDWH], M2.[MHDATE], M2.[MCTDCS], M2.[MO3TOT], M2.[MCTDLD], M2.[MCTDGE], M2.[MALPPP], M2.[MSLPPP], M2.[MWHSTA], M2.[MWHALL], M2.[MWHADD], EH.[EGARND], EP.[EMPNO], DP.[EMPNO], EI.[DDEPTN], EI.[NEWOCC], EI.[HRYRAT], EI.[MEMPAD], EI.[MEMPCS], EI.[MEMPZI], view_EmployeeInfo.[EmpPicture]

FROM LEAVHST LH

JOIN MASTERL2 M2
ON LH.[Employee Number]=M2.EMPNO  JOIN EARNHIST EH
ON EH.[EEMPNO]=M2.EMPNO JOIN EMPPICTURE EP
ON EP.[EMPNO]=EH.EEMPNO JOIN Departments DP
ON DP.[EMPNO]=EP.EMPNO JOIN View_EmployeeInfo EI
ON EI.[EMPNO]=M2.EMPNO

WHERE M2.[EMPNO] = '".$_POST['employeenumber']."' and
M2.[MSSNO] = '".$_POST['password']."'  


ORDER BY EH.[DATE] desc");

$RESULT=mssql_fetch_assoc($RESULTDS) or die("<CENTER><img src=\"http://orserva/images/invalidnumber.gif\"></CENTER><BR><CENTER><table width=60 //border=0><tr><td><font size=\"3\" color=\"#000000\" face=\"arial\">Need help? Here is an example entry:</font></td></tr><tr><td align=center><img //src=\"http://orserva/images/leaveexample.gif\"></td></tr><tr><td><font size=\"3\" color=\"#000000\" //face=\"arial\">Enter your Employee Number<BR>Enter your password (Social Security Number)<BR>Click Submit<BR><BR>If you're still having difficulty, click here to </font><font size=\"3\" color=\"#ff0000\" //face=\"arial\"><BR><a href=\"mailto:[email protected]? subject=Problem with the Pay Stubs On Demand //System\"><b>E-mail the HelpDesk</b></a></font></td></tr></table></CENTER>");


?>
[/code]

This works [b]only[/b] if the script at the top is left at the top. So....how do I use $RESULT[EmpPicture] as my variable to get the binary picture from?

In the other page, using [i]<img src=......> does indeed now let me put the picture where I want it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.