Page 1 of 2

Does anybody here know PHP GD?

Posted: Sun Jun 07, 2009 4:06 pm
by Pleek
I really need some help with a GD project, but i can't find anybody on the net that will help me! Does anybody here know it that would be interested in looking at my code and helping me out? My current code is this... It looks like this. My problem is the transparecies and the white pixels.

Code: Select all

 
<?php
 
header('Content-type: image/png');
 
$new_image = ImageCreateTruecolor(418, 252);
imagealphablending($new_image, true);
$bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127);
ImageFill($new_image, 0, 0 , $bg);
$trans = imagecolorallocate($new_image, 255, 255, 255);
imagecolortransparent($new_image, $trans);
 
$Background = imagecreatefrompng('BackgroundOne.png');
$Logo = imagecreatefrompng('CcLogo.png');
$AvatarBackground = imagecreatefrompng('AvatarBackground.png');
$Avatar = imagecreatefrompng('Avatar.png');
$AvatarReflection = imagecreatefrompng('AvatarReflection.png');
$UserRank = imagecreatefrompng('administrator.png');
 
imagecopy($new_image, $Background, 3, 42, 0, 0, 412, 167);
imagecopy($new_image, $Logo, 0, 0, 0, 0, 247, 252);
imagecopy($new_image, $AvatarBackground, 5, 44, 0, 0, 134, 164);
imagecopy($new_image, $Avatar, 12, 56, 0, 0, 120, 139);
imagecopy($new_image, $AvatarReflection, 11, 49, 0, 0, 120, 143);
imagecopy($new_image, $UserRank, 280, 50, 0, 0, 128, 28);
 
imagepng($new_image);
imagedestroy($new_image);
imagedestroy($Background);
imagedestroy($Logo);
imagedestroy($Avatar);
imagedestroy($AvatarReflection);
imagedestroy($UserRank);
 
?>
 

Re: Does anybody here know PHP GD?

Posted: Sun Jun 07, 2009 6:04 pm
by McInfo
Please run this diagnostic script and post the output.

gd_info.php

Code: Select all

<?php
header('Content-Type: text/plain');
print_r(gd_info());
?>
Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Sun Jun 07, 2009 6:41 pm
by McInfo
If you are using GD version 2.0.1 or later, adding this to your script should fix your problem.

Code: Select all

imagesavealpha($new_image, true);
alpha-blending.png
alpha-blending.png (2.43 KiB) Viewed 1618 times
PHP Manual wrote:You have to unset alphablending (imagealphablending($im, false)), to use it.
The manual is incorrect.

Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Sun Jun 07, 2009 9:19 pm
by Pleek
Thankyou! That fixed all the problems, now the image looks perfect. Before even when the background looked right when you saved the image and opened it the background was still white. Now it looks just how its supposed to. You wouldn't happen to know how to right align an imagestring would you?

View it here

GD info

Code: Select all

 
Array
(
    [GD Version] => bundled (2.0.28 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XBM Support] => 1
    [JIS-mapped Japanese Font Support] => 
)
 
The new code is this

Code: Select all

 
<?php
 
header('Content-type: image/png');
 
$new_image = ImageCreateTruecolor(418, 252);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127);
ImageFill($new_image, 0, 0 , $bg);
$trans = imagecolorallocate($new_image, 255, 255, 255);
imagecolortransparent($new_image, $trans);
 
$Background = imagecreatefrompng('BackgroundOne.png');
$Logo = imagecreatefrompng('CcLogo.png');
$AvatarBackground = imagecreatefrompng('AvatarBackground.png');
$Avatar = imagecreatefrompng('Avatar.png');
$AvatarReflection = imagecreatefrompng('AvatarReflection.png');
$UserRank = imagecreatefrompng('administrator.png');
 
imagecopy($new_image, $Background, 3, 42, 0, 0, 412, 167);
imagecopy($new_image, $Logo, 0, 0, 0, 0, 247, 252);
imagecopy($new_image, $AvatarBackground, 5, 44, 0, 0, 134, 164);
imagecopy($new_image, $Avatar, 12, 56, 0, 0, 120, 139);
imagecopy($new_image, $AvatarReflection, 11, 49, 0, 0, 120, 143);
imagecopy($new_image, $UserRank, 280, 50, 0, 0, 128, 28);
 
imagepng($new_image);
imagedestroy($new_image);
imagedestroy($Background);
imagedestroy($Logo);
imagedestroy($Avatar);
imagedestroy($AvatarReflection);
imagedestroy($UserRank);
 
?>
 

Re: Does anybody here know PHP GD?

Posted: Sun Jun 07, 2009 10:45 pm
by McInfo
Your image looks good. I like your design.
Pleek wrote:You wouldn't happen to know how to right align an imagestring would you?
If you use a mono-spaced font, you can multiply the width of a single character by the number of characters in the string and subtract the width of the string from the width of the image. That will give you the x-coordinate for the string. If you use a variable-width font, you can make an array to hold the widths of characters. I don't know of a better way at the moment.

PHP Manual: imagefontwidth()

Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 11:45 am
by Pleek
so how would i align it if i don't know how many charactors it will be? Like how would i right align $UserName?

Example Image Here

Code: Select all

 
$UserName = $_GET['UserName'];
$Font = 'Arial.ttf';
$Black = imagecolorallocate($new_image, 0, 0, 0);
imagettftext($new_image, 18, 0, 350, 150, $Black, $Font, $UserName);
 

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 12:07 pm
by McInfo
For fixed-width (mono-spaced) fonts, use strlen() multiplied by the width of a single character. For variable-width fonts like Arial, you need to come up with a function that incorporates imagefontwidth() and adds up the widths of the characters. You may find substr() or str_split() useful.

Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 1:26 pm
by Pleek
how do i find the width of each charactor in a variable-width font? Im guessing the code i came up with only works with mono-spaced fonts. Because it aligns it, but its off quite a bit.

Code: Select all

 
$UserName = $_GET['UserName'];
$Font = 'Arial.ttf';
$Black = imagecolorallocate($new_image, 0, 0, 0);
$Charactors = strlen($UserName);
$Font2 = imageloadfont('Arial.fft');
$FontWidth = imagefontwidth($Font2);
$RightAlign = 408 - $Charactors * $FontWidth;
 
imagettftext($new_image, 18, 0, $RightAlign, 150, $Black, $Font, $UserName);
 

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 4:28 pm
by McInfo
I had to work this out. Here is what I discovered. One gotcha to remember is that imagettftext() positions text using the bottom-left (not upper-left) coordinates of the text's baseline (not the bottom of the text's bounding box).

Code: Select all

<?php
// Configuration
$imageWidth      = 200;
$imageHeight     = 50;
$fontFile        = './arial.ttf'; // Requires that arial.ttf be in the current directory
$fontScale       = 2; // Font Size Multiplier (greater than 0)
$text            = "Aligned text.";
$textAngle       = 0; // This script works best with $textAngle = 0
 
 
$image           = imagecreatetruecolor($imageWidth, $imageHeight);
$colorLine       = imagecolorallocate($image, 255, 0, 0);
$colorFont       = imagecolorallocate($image, 255, 255, 255);
 
$fontHeight      = imagefontheight($fontFile) * $fontScale;
$fontWidth       = imagefontwidth($fontFile) * $fontScale;
 
// An array of values describing the text's bounding box
$textPosition    = imagettfbbox($fontWidth, $textAngle, $fontFile, $text);
 
// Top-Right-X minus Top-Left-X
$textWidth       = $textPosition[4] - $textPosition[6];
 
// Bottom-Left-Y minus Top-Left-Y
$textHeight      = $textPosition[1] - $textPosition[7];
 
// The distance between the text baseline and the text bottom
$textBaseOffset  = $textPosition[1];
 
// Draw the baseline for the text aligned top-left
imageline($image, 0, $textHeight - $textBaseOffset, $textWidth, $textHeight - $textBaseOffset, $colorLine);
 
// Text Aligned Top, Left
imagettftext($image, $fontWidth, $textAngle, 0, $textHeight - $textBaseOffset, $colorFont, $fontFile, $text);
 
// Text Aligned Top, Right
imagettftext($image, $fontWidth, $textAngle, $imageWidth - $textWidth, $textHeight - $textBaseOffset, $colorFont, $fontFile, $text);
 
// Text Aligned Bottom, Left
imagettftext($image, $fontWidth, $textAngle, 0, $imageHeight - $textBaseOffset, $colorFont, $fontFile, $text);
 
// Text Aligned Bottom, Right
imagettftext($image, $fontWidth, $textAngle, $imageWidth - $textWidth, $imageHeight - $textBaseOffset, $colorFont, $fontFile, $text);
 
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
ttf-alignment.png
ttf-alignment.png (1.1 KiB) Viewed 1618 times
Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 8:08 pm
by Pleek
ok, all that code lost me. Does that align it to the corners of the image? All i need is to right align a imagettftext(), can you write a simplified version that only does that? I don't need the other 3. Thanks

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 8:45 pm
by McInfo
Where in the code did you get lost?

If you want to show just one alignment, comment out all but one of lines 31, 34, 37, 40, and 43. The other 18 statements are essential (except line 12, which is only essential for line 31). I don't see how I can simplify it any more without writing a function or class (object).

Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 9:52 pm
by Pleek
Well see, i understood it for the most part. First time i tried it (before last post) i missed a variable that i didn't change. Now the text shows up, but its in the bottom corner of the image. How do i position it further into the picture? View it here

Code: Select all

 
$UserName = $_GET['UserName'];
$Font = './Arial.ttf';
$Black = imagecolorallocate($new_image, 0, 0, 0);
 
$imageWidth      = 418;
$imageHeight     = 252;
$fontScale       = 4; // Font Size Multiplier (greater than 0)
$fontHeight      = imagefontheight($fontFile) * $fontScale;
$fontWidth       = imagefontwidth($fontFile) * $fontScale;
$textAngle       = 0; // This script works best with $textAngle = 0
 
// An array of values describing the text's bounding box
$textPosition    = imagettfbbox($fontWidth, $textAngle, $Font, $UserName);
 
// Top-Right-X minus Top-Left-X
$textWidth       = $textPosition[4] - $textPosition[6];
 
// Bottom-Left-Y minus Top-Left-Y
$textHeight      = $textPosition[1] - $textPosition[7];
 
// The distance between the text baseline and the text bottom
$textBaseOffset  = $textPosition[1];
 
// Text Aligned Bottom, Right
imagettftext($new_image, $fontWidth, $textAngle, $imageWidth - $textWidth, $imageHeight - $textBaseOffset, $Black, $Font, $UserName);
 

Re: Does anybody here know PHP GD?

Posted: Mon Jun 08, 2009 10:27 pm
by McInfo
Subtract some pixels from the X and Y coordinates.
imagettftext($new_image, $fontWidth, $textAngle, $imageWidth - $textWidth - 20, $imageHeight - $textBaseOffset - 15, $Black, $Font, $UserName);
or
imagettftext($new_image, $fontWidth, $textAngle, $imageWidth - $textWidth - 15, $imageHeight - $textBaseOffset - 53, $Black, $Font, $UserName);
Also, make sure you are using the correct variables. Lines 9 and 10 still reference $fontFile instead of $Font.

Edit: This post was recovered from search engine cache.

Re: Does anybody here know PHP GD?

Posted: Tue Jun 09, 2009 9:45 am
by Pleek
Thank you so much for your help, with what you gave me here i was able to complete the image. And it looks exactly like the photoshop concept! Thank you so much!!!!

Image

Final Code...

Code: Select all

 
<?php
 
header('Content-type: image/png');
 
$new_image = ImageCreateTruecolor(418, 252);
imagealphablending($new_image, true);
imagesavealpha($new_image, true);
$bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127);
ImageFill($new_image, 0, 0 , $bg);
$trans = imagecolorallocate($new_image, 255, 255, 255);
imagecolortransparent($new_image, $trans);
 
$Background = imagecreatefrompng('BackgroundOne.png');
$Logo = imagecreatefrompng('CcLogo.png');
$AvatarBackground = imagecreatefrompng('AvatarBackground.png');
$Avatar = imagecreatefrompng('Avatar.png');
$AvatarReflection = imagecreatefrompng('AvatarReflection.png');
$UserRank = imagecreatefrompng('administrator.png');
 
imagecopy($new_image, $Background, 3, 42, 0, 0, 412, 167);
imagecopy($new_image, $Logo, 0, 0, 0, 0, 247, 252);
imagecopy($new_image, $AvatarBackground, 5, 44, 0, 0, 134, 164);
imagecopy($new_image, $Avatar, 12, 56, 0, 0, 120, 139);
imagecopy($new_image, $AvatarReflection, 11, 49, 0, 0, 120, 143);
imagecopy($new_image, $UserRank, 280, 50, 0, 0, 128, 28);
 
$UserName = $_GET['UserName'];
$Posts = $_GET['Posts']. ' Posts';
$PS = $_GET['PS'];
$Site = $_GET['Site'];
$Font = './Arial.ttf';
$Black = imagecolorallocate($new_image, 0, 0, 0);
 
$imageWidth      = 418;
$imageHeight     = 252;
// An array of values describing the text's bounding box
$textPositionUser    = imagettfbbox(18, $textAngle, $Font, $UserName);
$textPositionPosts    = imagettfbbox(18, $textAngle, $Font, $Posts);
$textPositionPS    = imagettfbbox(12, $textAngle, $Font, $PS);
$textPositionSite    = imagettfbbox(12, $textAngle, $Font, $Site);
 // Top-Right-X minus Top-Left-X
$textWidthUser       = $textPositionUser[4] - $textPositionUser[6];
$textWidthPosts       = $textPositionPosts[4] - $textPositionPosts[6]; 
$textWidthPS       = $textPositionPS[4] - $textPositionPS[6]; 
$textWidthSite       = $textPositionSite[4] - $textPositionSite[6]; 
// Bottom-Left-Y minus Top-Left-Y
$textHeightUser      = $textPositionUser[1] - $textPositionUser[7];
$textHeightPosts      = $textPositionPosts[1] - $textPositionPosts[7];
$textHeightPS      = $textPositionPS[1] - $textPositionPS[7]; 
$textHeightSite      = $textPositionSite[1] - $textPositionSite[7]; 
// The distance between the text baseline and the text bottom
$textBaseOffsetUser  = $textPositionUser[1];
$textBaseOffsetPosts  = $textPositionPosts[1];
$textBaseOffsetPS  = $textPositionPS[1];
$textBaseOffsetSite  = $textPositionSite[1];
// Text Aligned Bottom, Right
imagettftext($new_image, 18, 0, $imageWidth - $textWidthUser - 15, $imageHeight - $textBaseOffsetUser - 145, $Black, $Font, $UserName);
imagettftext($new_image, 18, 0, $imageWidth - $textWidthPosts - 15, $imageHeight - $textBaseOffsetPosts - 115, $Black, $Font, $Posts);
imagettftext($new_image, 12, 0, $imageWidth - $textWidthPS - 15, $imageHeight - $textBaseOffsetPS - 88, $Black, $Font, $PS);
imagettftext($new_image, 12, 0, $imageWidth - $textWidthSite - 15, $imageHeight - $textBaseOffsetSite - 60, $Black, $Font, $Site);
 
imagepng($new_image);
imagedestroy($new_image);
imagedestroy($Background);
imagedestroy($Logo);
imagedestroy($Avatar);
imagedestroy($AvatarReflection);
imagedestroy($UserRank);
 
?>
 

Re: Does anybody here know PHP GD?

Posted: Tue Jun 09, 2009 10:10 am
by JeffG
I hesitate to point this out, since that looks really great, but you might want to fix a typo in the text: You have 'Ect' instead of 'Etc.'.