PHP imagecreatefromgif animated + color - php

I'm trying to create a Image with PHP.
With PNG it works pretty well (The Font Colors works!).
But if I'm using GIF, the Fonts dont get Colors (Always white).
And the GIF Image I use for the Background is not Animated!
This is how it works in PNG (Font Color):
<?php
$zeile1 = #file_get_contents('zeile1.txt');
$zeile1_title = #file_get_contents('zeile1_title.txt');
$image = imagecreatefrompng("bg_text.png");
imagesavealpha($image, true);
imagealphablending($image, true);
$finalImage = imagecreatetruecolor(391,300);
$font = '../banner1/arial.ttf';
$color = imagecolorallocate($finalImage, 0, 0, 0);
$font_title = '../banner1/arialbd.ttf';
$color_title = imagecolorallocate($finalImage, 255, 0, 126);
imagettftext($image, 15, 0, 2, 130, $color, $font, $zeile1);
imagettftext($image, 25, 0, 2, 130, $color_title, $font_title, $zeile1_title);
header('Content-type: image/png');
imagepng($image);
?>
But now I want a animated gif as background:
<?php
$zeile1 = #file_get_contents('zeile1.txt');
$zeile1_title = #file_get_contents('zeile1_title.txt');
$image = imagecreatefromgif("bg_text.gif");
imagesavealpha($image, true);
imagealphablending($image, true);
$finalImage = imagecreatetruecolor(391,300);
$font = '../banner1/arial.ttf';
$color = imagecolorallocate($finalImage, 0, 0, 0);
$font_title = '../banner1/arialbd.ttf';
$color_title = imagecolorallocate($finalImage, 255, 0, 126);
imagettftext($image, 15, 0, 2, 130, $color, $font, $zeile1);
imagettftext($image, 25, 0, 2, 130, $color_title, $font_title, $zeile1_title);
header('Content-type: image/gif');
imagegif($image);
?>
Now the Text is only white and the Title text, is not Showing + The image is not animated.
I hope you understand what I mean, because my english is bad :/

Related

PHP saving imagecreatefrompng

I want to save my pngs, but my code does not allow me to create new pngs or overwrite the existing ones. Ideally every time the page is loaded the image would be saved.
<?php
$width = 640;
$height = 480;
$font = 23;
$string = "This is my text";
$im = #imagecreatetruecolor($width, $height);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 0, 0, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
$im = imagecreatefrompng("test.png");
imagedestroy($im);
?>
imagecreateFROMpng as it names indicates creates an image object by reading a .PNG file. In order to save the image as a PNG, you must use the imagepng function:
...
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
imagepng($im, "test.png");
imagedestroy($im);

cant change font type in dynamic image

cant seem to get the font to work , so far i've seen that the imagestring doesn't support the font being changed so im trying to work with imagettftext like so
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
but instead of displaying any text , it just shows the image with all the imagestring's written , also this is my first time working with dynamic images so , im not quite sure what im doing wrong (if any thing) also Neverwinter.ttf is in the same file directory as the image script any help would be appriciated
<?php
$image = imagecreatefrompng("icons.png");
imagesavealpha($image, true);
imagealphablending($image, true);
$size = getimagesize("icons.png");
$finalImage = imagecreatetruecolor(320,125);
$font = imageloadfont('/runescape/phpfile/arial.ttf');
$font2 = imageloadfont('Neverwinter.ttf');
$font = 4;
$textcolorBlack = imagecolorallocate($finalImage, 0, 0, 0);
$textcolorWhite = imagecolorallocate($finalImage, 255, 255, 255);
$textcolorGreen = imagecolorallocate($finalImage, 0, 255, 0);
$textcolorBlue = imagecolorallocate($finalImage, 225,225, 255);
imagecopy($finalImage, $image, 0, 0, 0, 0, 320, 125);
//imagestring($finalImage, $font, 0, 0, 'random', $textcolorBlue);
$text = 'testing...';
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
// Content type
header('Content-type: image/png');
imagepng($finalImage);
?>
just change the line
$font2 = imageloadfont('Neverwinter.ttf');
to
$font2 = 'path/Neverwinter.ttf';

php imagerotate and imagecopymerge makes no transparent background and no auto width/height

I have this script, which makes an image and posts it on another image:
<?php
$img=imagecreatetruecolor(150,20);
imagealphablending($img,false);
$col=imagecolorallocatealpha($img,255,255,255,127);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
imagefilledrectangle($img,0,0,180,20,$col);
imagealphablending($img,true);
$font='../ttf/0001.ttf';
$color = imagecolorallocate($img, 0, 0, 0);
imagettftext($img,11,0,5,14,$color,$font,'Text goes here');
imagealphablending($img,false);
imagesavealpha($img,true);
imagejpeg($img, '../custom_images/test.jpg');
// Create image instances
$dest = imagecreatefromjpeg('../custom_images/121536.jpg');
$src = imagecreatefromjpeg('../custom_images/test.jpg');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 0, $color);
imagesavealpha($rotated, true);
$trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
imagefill($rotated, 0, 0, $trans_colour);
imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png');
$width = imagesx($new_img);
$height = imagesy($new_img);
// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);
imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
2 things:
Background is not transparent
I want the Width and Height to be automatic, so if the text is short, the image is it to.
What do I fault?

PHP imagecopymerge source image's background change transparent to black

I need to merge two pictures,
my code is like this:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
the frist picture is :
second is :
final result not transparent
Please help!!!Thanks!
new version of code:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image1, false);
imagesavealpha($image1, true);
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
and the result is :
Ok..i found a solution now..
$image = imagecreatefromjpeg("images/show01.jpg"); //
$image1 = imagecreatefrompng("small_image/1.png");//
$image3 = imagecreatefromgif("images/carBg.gif");//
$size = getimagesize("small_image/1.png");
$overlay = imagecreatetruecolor(80, 80);
$white = imagecolorallocate($overlay, 229, 229, 229);
imagefilledrectangle($overlay, 0, 0, 80, 80, $white);
imagecolortransparent($overlay,$white);
imagecopy($overlay, $image1, (80-$size[0])/2, (80-$size[1])/2, 0, 0, $size[0],$size[1]);
imagecopymerge($image3, $overlay, 0, 0, 0, 0, 80, 80, 100);
imagecopymerge($image, $image3, 280, 30, 0, 0, 80, 80, 100);
header('Content-Type: image/png');
// and finally, output the result
imagepng($image);
imagedestroy($image);

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

Categories