imagecreatefrompng() generating black background instead of transparent part in image - php

I make an image using PHP but my code turn png transparency into a solid black color, Is there a solution to improve my code?
$filename = 'https://i.ibb.co/9sB9rTM/small-408587143-15774386991of1-2-2-Original-0-test.png';
$widthsrc=0;
$heightsrc=0;
$src1 = imagecreatefrompng($filename);
list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize($filename);
$background = imagecolorallocatealpha($src1, 255, 255, 255, 127);
$pre = imagecolortransparent($src1, $background);
imagealphablending($src1, false);
imagesavealpha($src1, true);
imagepng($src1);

First use always header like:
header("Content-type: image/png");
Second are you sure when you save image 'black solid' is not trasparent?
Example of creating image:
header("Content-type: image/png");
$string = 'TEXT';
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>

Related

Convert php string to image

I want to convert a php string to an image.
I use this code
header("Content-type: image/png");
$string = '.the_title().';
$font = 5;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image)
but its shows the_title as text instead of executing the string
Use imagecreatefromstring
$string = '.the_title().';
$data = base64_decode($string);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
Use imagestring like:
<?php
$string = the_title();
$im = imagecreate(150, 20); // image size 150x20px
imagecolorallocate($im, 255, 255, 255); // background white
$text_color = imagecolorallocate($im, 0, 0, 0); // text color black
imagestring($im, 3, 5, 5, $string, $text_color); // append string to image
header('Content-type: image/png'); // filetype
imagepng($im, 'image.png'); // save as image.png
imagedestroy($im); // free up memory

Php add centered text to image

I've tried a few times but I can't seem to get 2 centered lines of text at the bottom of this image on the transparent background
Any suggestions?
<?php
$filePath = "adopt.png"; //full path to your png, including filename and extension
$img = #imagecreatefrompng($filePath);
$width = imagesx($img);
$height = imagesy($img);
//create new image and fill with background color
$backgroundImg = #imagecreatetruecolor($width, $height);
imagecopy($backgroundImg, $img, 0, 0, 0, 0, 100, 130);
$color = imagecolorallocatealpha($backgroundImg, 0, 0, 0, 127); //fill transparent back
imagefill($backgroundImg, 0, 0, $color);
//save as png
header( "Content-type: image/png" );
imagepng( $backgroundImg );
imagedestroy( $backgroundImg );
?>
This process works. Without your code I can only regurgitate the example from the manual to you. http://php.net/manual/en/function.imagestring.php. But it works, I have used the imagestring function. It accepts co-ordinates for the text so I can not see why it would not work for you.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

How to convert text to image in PHP with line break?

Currently I am using the PHP code below to convert string text to image with transparent background. Now I need to convert the string text to image with line break, transparent background and also with custom font. Code I am using:
$width = 200;
$height = 40;
$text = "Test text -- line break here -- with line break";
$fontsize = 3;
$img = imagecreate($width, $height);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
$color = imagecolorallocate($img, 0, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $color);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Doing it like this will help you
header("Content-type: image/png");
$str1= 'Test text ';
$str2= 'with line break';
$image= imagecreate(200,40);
$background = imagecolorallocate($image,255,255,255);
$color= imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$background);
imagestring($image,10,5,5,$str1,$color);
imagestring($image,10,5,20,$str2,$color);
imagepng($image);
output look like this

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);
?>

Applying text to png transparency issue PHP

I am trying to write text onto a png, however when I do it puts a dark border around it, I am not sure why.
The original image:
The processed image:
Code:
// Load the image
$im = imagecreatefrompng("admin/public/images/map/order/wally.png");
// If there's an error, gtfo
if(!$im) {
die("");
}
$textColor = imagecolorallocate($im, 68, 68, 68);
$width = imagesx($im);
$height = imagesy($im);
$fontSize = 5;
$text = "AC";
// Calculate the left position of the text
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text)) / 2;
// Write the string
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
I've had this issue several times, let me find the answer...
Ok, found something:
imagesavealpha($im, true);
imagealphablending($im, true);
Write that before imagepng.
Yes, saving with alpha is important but loading it is important as well. Your PNG image might have transparency but it is good practice to account for that as well.
You'd need to create true color image, set alpha color and then draw your loaded image with text over it. So something like this:
// create true color image
$img = imagecreatetruecolor($width, $height);
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0);
imagealphablending($img, false);
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color);
imagealphablending($img, true);
// draw previously loaded PNG image
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height);
// draw your text
// save the whole thing
imagesavealpha($img, true);
imagepng($img, $file);

Categories