I'm needing to create on my page an image with dynamic content.
This image needs to be transparent background (PNG).
So I searched and I figure out how to do this.
But I have one more problem... My dynamic content is an image as well... for real, it's a \EmojieOne library, that takes the code like that: ":joy:" from the database and print the real emoji with:
toImage(':joy:')
So now, when I try to use this in my imagestring (to create my image), The image generated receive the code instead to receive the emoji image.
//My EmojiOne class
$client = new Client(new Ruleset());
$emojiCode = $client->toImage($emojiCode);
//Creating my image PNG
$image = imagecreatetruecolor(800, 400);
imagesavealpha($image, true);
$textcolor = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocatealpha($image, 0, 0, 0, 125);
imagefill($image, 0, 0, $color);
//Printing the dynamic content in my new image
imagestring($image, 5, 0, 0, $emojiCode, $textcolor);
imagepng($image);
Does anyone know what I can do for the new image to receive the emoji image, rather than its HTML code in a text?
Related
I am starting on php and am trying to create an image using php-gd, but keep getting a weird output.
I am using xampp.
Here's my code: (a black line should be seen into the screen)
<?php
header ("Content-type: image/png");
$image = imagecreate(200, 50);
$black = imagecolorallocate($image, 0, 0, 0);
ImageLine ($image, 30, 30, 120, 120, $black);
imagepng($image);
?>
I tried with many samples found in the internet but i keep getting
, for anything I'm trying to do. (from showing text to creating a watermark). I use examples already tested by others, so the code isn't probably the problem.
Could you please say me what is wrong?
Edit : I am actually on the right window
(my file is stored at C:\xampp\htdocs\projet_30_03_21\template.php)
Edit : Ctrl+F5 doesn't work either. Also tried creating a new window.
You could use imagefill() to define a background before to add a black line, because the background is already black.
It's also recommanded to use imagecreatetruecolor() instead of imagecreate().
$image = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
imageline($image, 30, 30, 120, 120, $black);
header("Content-type: image/png");
imagepng($image);
Give the following image
Found the problem (https://www.php.net/manual/fr/install.windows.legacy.index.php#install.windows.legacy.building).
I had to uncomment this line of code :
;extension=gd
to
extension=gd.
I am trying to do the following:
I Have a picture I want to make that only text which is written in black remains visible and rest of every colour gets transparent.
I tried to do this using PHP imagecolortransparent function but I am not able to figure out how to make it work.
Any help will be great.
Thanks In advance
Updated Answer
There must be an easier way, but until I think of it, this should work:
// Read original image
$start = imagecreatefrompng("input.png");
// Make true colour image same size
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
$red=imagecolorallocate($im,255,0,0);
imagesavealpha($im, TRUE);
// Fill the new image with transparency
imagefill($im, 0, 0, $transparent);
// Copy only black pixels across from original to new
for($x=0;$x<imagesx($im);$x++){
for($y=0;$y<imagesy($im);$y++){
$rgb = imagecolorat($start,$x,$y);
if($rgb==0){
imagesetpixel($im,$x,$y,$black);
}
}
}
imagepng($im,"result.png");
Original Answer
I would do this:
// Read original image
$start = imagecreatefrompng("input.png");
// Create a proper truecolour image the same size that can support transparency
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$black = imagecolorallocate($im, 0, 0, 0);
$text="This is some quite black text";
imagestring($im,5,0,75,$text,$black);
imagepng($im,"result.png");
input.png
result.png
I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>
Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:
I'm trying to use the font-awesome stack on a commercial web development project, and we have got it to a working stage, however we are left with one problem.
When viewing our websites on a mobile devices (or a browser without imported font stack support) all of our icons are replaced with squares (because font-awesome uses Unicode chars to represent the icons).
This breaks a lot of the way our website looks and feels (especially the custom admin panel we have coded).
The solution we came up with was to fall back to using PHP to render an image containing the icon we want (with the colour we want specified as an argument, along with size etc)
This has never been a problem before, but now I'm having huge trouble getting PHP to render the Private Use Area (PUA) chars.
Here is some sample code I'm trying to use:
<?php
$icons = array(
"icon-glass" => "\f000",
"icon-music" => "\f001",
"icon-search" => "\f002",
// [...]
);
$font = "_assets/fonts/font-awesome/fontawesome-webfont.ttf";
if(isset($_GET['icon'])) {
$chr = $icons[$_GET['icon']];
header("Content-type: image/png");
$img = imagecreatetruecolor($_GET['w'], $_GET['h']);
imagealphablending($img, true);
$transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
imagefill( $img, 0, 0, $transparent );
$black = imagecolorallocate($img, 255, 0, 0);
imagettftext($img, 20, 0, 32, 32, $black, $font, $chr);
imagesavealpha($img, true);
imagepng($img);
exit;
}
?>
<div style="background-color:black; width:64px; height:64px;">
<img src="dev?icon=icon-dashboard&w=64&h=64">
</div>
<br />
<div style="background-color:blue; width:64px; height:64px;">
<img src="dev?icon=icon-bolt&w=64&h=64">
</div>
However this seems to just render the squares inside the image. I'm thinking this is because I'm inputting the unicode chars badly to PHP and it's possibly something really silly that I'm missing.
Any suggestions are welcome.
The PHP code I used to render Font Awesome TTF glyphs (mostly):
$text = json_decode('""');
...
imagesavealpha($im, true);
$trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans);
imagealphablending($im, true);
$fontcolor = imagecolorallocatealpha($im, 0, 0, 0, 0);
// Add the text
imagettftext($im, $x, 0, 0, 0, $fontcolor, $font, $text);
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
The json-decode() handles the complexity of the unicode character. I used a GD version 2 or greater so had to use points instead of pixels.
My full class takes into account the desired height, but ignores width. You can view it at https://github.com/sperelson/awesome2png.
Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);