Jump to content

PHP image reflection help...


Teachy

Recommended Posts

Hello guys, I hope you can help me with this

 

I am trying to add a PHP-serverside image reflection, using the tutorial at http://www.talkphp.com/advanced-php-programming/1714-image-reflections-php.html

 

At the moment, we only want to apply it to our staff signatures, and for the reflection to be about 50% the height of the real image (which then fades into the background)...

 

I tried using the tutorial and putting the relevant lines in, changing jpeg to png (as that's what the rest uses) and only using the header, imagepng and imagedestroy lines once, but I just got tonnes of errors. Can you help me integrating that script into mine to make it work?

 

Thanks!

 

<?php
include_once('/home/site/functions-connectonly.php');
putenv('GDFONTPATH=' . realpath('.'));

if (isset($_GET['staffid'])) {
  $staffid = ($_GET['staffid']);

  $num_users=mysql_num_rows(mysql_query("Select * from user where account='".mysql_real_escape_string(htmlentities($staffid))."'"));
  if ($num_users>0) {

    $ourGroups='';
    // fetch user from db
    $user=mysql_fetch_array(mysql_query("Select * from user where account='".mysql_real_escape_string(htmlentities($staffid))."'"));
    $flag=mysql_fetch_array(mysql_query("Select * from flag where id_flag=".$user['location'])); 

    // Group code here (irrelevant)

    $name = "".$user['account']."";
    if (strlen($ourGroups)>0) {
       $text = "\n".$ourGroups."\nhttp://oursite.com";
    } else {
       $text = "\nNo groups\nhttp://oursite.com";
    }

    // tell the browser what it's about to recieve
    header("Content-type: image/png");

    // import background image
    $img = @imagecreatefrompng("/home/site/img/sigbg.png");
    $im = imagecreatetruecolor(185, 58);

    // font details
    $font = 'segoe.ttf';

    // insert the flag
    $insert = imagecreatefrompng("/home/site/img/flags/".$flag['img']."");

    // Select the first pixel of the overlay image (at 0,0) and use
    // it's color to define the transparent color

    imagecolortransparent($insert,imagecolorat($insert,0,0));

    // Get overlay image width and hight for later use

    $insert_x = imagesx($insert);
    $insert_y = imagesy($insert);

    // Combine the images into a single output image. Some people
    // prefer to use the imagecopy() function, but more often than
    // not, it sometimes does not work. (could be a bug)

    imagecopymerge($img,$insert,162,5,0,0,$insert_x,$insert_y,100);

    // text color
    $white = imagecolorallocate($img, 255, 255, 255);
    $grey = imagecolorallocate($img, 225, 225, 225);

    // add the text to the image ,
    $textarr = explode("\n",$text);
    $i = 16;
    foreach($textarr as $a){
      imagettftext($img, 10, 0, 52, 16, $white, $font, $name);
      imagettftext($img, 8, 0, 52, $i, $grey, $font, $a);
      $i=$i+17;
    }

    // date in the past, so that the image is not cached by the browser
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    // create the png image
    imagepng($img);

    // clean up
    imagedestroy($img);

  }
}
?>

Link to comment
Share on other sites

Hi, thanks for your reply.

 

Got the errors sorted, but now, I don't see my normal signature, instead just a grey gradient which is halfway down the image, where I guess it should be... *baffled*

 

<?php
include_once('/home/site/functions-connectonly.php');
putenv('GDFONTPATH=' . realpath('.'));

if (isset($_GET['staffid'])) {
  $staffid = ($_GET['staffid']);

  $num_users=mysql_num_rows(mysql_query("Select * from user where account='".mysql_real_escape_string(htmlentities($staffid))."'"));
  if ($num_users>0) {

    $ourGroups='';
    // fetch user from db
    $user=mysql_fetch_array(mysql_query("Select * from user where account='".mysql_real_escape_string(htmlentities($staffid))."'"));
    $flag=mysql_fetch_array(mysql_query("Select * from flag where id_flag=".$user['location'])); 

    // Group code here (irrelevant)

    $name = "".$user['account']."";
    if (strlen($ourGroups)>0) {
       $text = "\n".$ourGroups."\nhttp://oursite.com";
    } else {
       $text = "\nNo groups\nhttp://oursite.com";
    }

    // tell the browser what it's about to recieve
    header("Content-type: image/png");

    // import background image
    $img = @imagecreatefrompng("/home/site/img/sigbg.png");
    // Create new blank image with sizes.  Normal size is 58, add 30
    $newImage = imagecreatetruecolor(185, 88);
    for ($x = 0; $x < 185; $x++) {
      for ($y = 0; $y < 88; $y++)
      {
        imagecopy($newImage, $img, $x, 185 - $y - 1, $x, $y, 1, 1);
      }
    }

    $background = imagecreatetruecolor(185, 88);

    $gradientColor = "255 255 255"; // White
    $gradparts = explode(" ",$gradientColor); // get the parts of the  colour (RRR,GGG,BBB)
    $dividerHeight = 1;

    // font details
    $font = 'segoe.ttf';

    // insert the flag
    $insert = imagecreatefrompng("/home/site/img/flags/".$flag['img']."");

    // Select the first pixel of the overlay image (at 0,0) and use
    // it's color to define the transparent color

    imagecolortransparent($insert,imagecolorat($insert,0,0));

    // Get overlay image width and hight for later use

    $insert_x = imagesx($insert);
    $insert_y = imagesy($insert);

    // Combine the images into a single output image. Some people
    // prefer to use the imagecopy() function, but more often than
    // not, it sometimes does not work. (could be a bug)

    imagecopymerge($background,$newImage,0,0,0,0,185,88,100);
    imagecopymerge($img,$insert,162,5,0,0,$insert_x,$insert_y,100);

    // text color
    $gradient_y_startpoint = $dividerHeight;
    $gdGradientColor=ImageColorAllocate($background,$gradparts[0],$gradparts[1],$gradparts[2]);

    $white = imagecolorallocate($img, 255, 255, 255);
    $grey = imagecolorallocate($img, 225, 225, 225);

    // create from a the image so we can use fade out.
    $gradient_line = imagecreatetruecolor(185, 1);

    // Next we draw a GD line into our gradient_line
    imageline ($gradient_line, 0, 0, 185, 0, $gdGradientColor);

    // add the text to the image ,
    $textarr = explode("\n",$text);
    $i = 16;
    foreach($textarr as $a){
      imagettftext($img, 10, 0, 52, 16, $white, $font, $name);
      imagettftext($img, 8, 0, 52, $i, $grey, $font, $a);
      $i=$i+17;
    }

    $ii = 0;
    $transparency = 30; //from 0 - 100

    while ($ii < 88) //create line by line changing as we go
    {
      imagecopymerge ($background, $gradient_line, 0,$gradient_y_startpoint, 0, 0, 185, 1, $transparency);
        
      ++$ii;
      ++$gradient_y_startpoint;
                
      if ($transparency == 100) {
        $transparency = 100;
      } else {
        // this will determing the height of the
        // reflection. The higher the number, the smaller the reflection.
        // 1 being the lowest(highest reflection)
        $transparency = $transparency + 1;
      }
    }

    // Set the thickness of the line we're about to draw
    imagesetthickness ($background, 88);

    // Draw the line
    imageline ($background, 0, 0, 185, 0, $gdGradientColor);

    // date in the past, so that the image is not cached by the browser
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    // create the png image
    imagepng($background);

    // clean up
    imagedestroy($background);
    imagedestroy(gradient_line);
    imagedestroy(newImage);

  }
}
?>

Link to comment
Share on other sites

Hi there

Thanks for your replies :)

I've seen the Javascript one, it doesn't really do it the same as the PHP one and we ideally want it serverside

 

Barand, I used the [-php][-/php] ones as that was the formatting, that's how it showed up. I still see that when I edit too

Link to comment
Share on other sites

  • 4 weeks later...

I am not too sure about the functions that you are using to create the images, but, I have created an open source project called vsrefleciton on SourceForge.net.

It uses PHP and ImageMagick to create great Image Reflections on the fly.

 

Possibly you could use some of the code to make your project work.

The sourceforge download is located at:  http://sourceforge.net/project/showfiles.php?group_id=217530

 

You can visit the project homepage at:    http://www.vsourcesoftware.com/vsreflection.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.