Jump to content

PHP image background


par123

Recommended Posts

Hi,

 

I am trying to create images using imagecreatetruecolor function. It creates an image with black background by default. Now, I want to make the background transparent. I used imagecolortransparent function for that. I am trying to write a string in the image in white font. For creating the white color, I use imagecolorallocate function. When I pass the image resource of my image as an argument to this funtion, the whole image becomes white. I tried using the imagecolortransparent funtion at the end after writing the string, but it does not work. I am using Php 5.3. Can someone please help? I am too much frustrated with this now.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/174986-php-image-background/
Share on other sites

Here is my code:

 

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

$im = imagecreatetruecolor(100, 100) or die("!ERROR!");

$black=imagecolorallocate($im,0,0,0);

imagecolortransparent($im,$black);

function imagestringcentered ($img,$font,$text,$color) {

while (strlen($text) * imagefontwidth($font) > imagesx($img)) {

  if ($font > 1) { $font--; }

  else { break; }

}

$cy = (imagesy($img)/2) - (imagefontheight($font)/2);

imagestring($img,$font,imagesx($img) / 2 - strlen($text) *

imagefontwidth($font) / 2,$cy,$text,$color);

}

 

$w = trim(stripslashes($_GET['w']));

if(!$w || $w == "")

        $w = "!NO DATA!";

$tc = imagecolorallocate($im,255,255,255);

imagestringcentered($im, 5, $w, $tc);

imagepng($im);

imagedestroy($im);

Link to comment
https://forums.phpfreaks.com/topic/174986-php-image-background/#findComment-922236
Share on other sites

I am trying to create a transparent background image with text written in the center of the image in white font. I have posted my code in the previous post. Can someone please tell me why soes the background not turn transparent? Rest all works fine. Just the backgroound does not turn transparent. I am working on it since long, but am not able to figure it out. Can someone please help?

Link to comment
https://forums.phpfreaks.com/topic/174986-php-image-background/#findComment-922331
Share on other sites

No, it does not make the background white. With the code posted, I am able to get the white font string in black background image. But I want the background to be transparent. So I tried using imagecreate function instead of imagecreatetruecolor. I know imagecreate would create an image with no background color.  I need white color for the font. So inorder to create white color, when I use imagecolorallocate, it makes the background white. There must be some other way of making the background transparent. Imagecolortransparent function too does not work.

Link to comment
https://forums.phpfreaks.com/topic/174986-php-image-background/#findComment-922362
Share on other sites

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.