I am executing my following code for creating a transparent image but everytime it shows me black background.
kindly tell me my my fault in the code.
<?php
//set the content type
header('Content-type: image/jpeg');
//create the image
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
// Make the background transparent
imagecolortransparent($im, $black);
//text to draw
$text=$_POST['text'];
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, -$blue, $font, $text);
//view the image
imagejpeg($im);
imagedestroy($im);
?>
You cannot make jpeg images transparent. Use png instead
Change below 2 lines:
header('Content-type: image/png');
imagepng($im);
Update
Reference Link: create transparent png image
Related
I have GD library installed as you can see.(image)
but when i try to create a image it shows special characters instead of the image.(image)
I'm using laravel and Xampp.
Anyone Know what is the problem ?
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
If you want your image to show as an image, you need to remove any output before sending a header. Remove var_dump(gd_info()); from your code to see the actual image:
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
From https://www.php.net/manual/en/function.header.php:
"Remember that header() must be called before any actual output is sent"
I have an transparent image with a bar through the middle...
http://i66.tinypic.com/2zyeg4h.png
Using php I would like to remove the middle part on the bar, leaving a transparent space in the middle of the bar. I try this code...
$im = imagecreatefrompng('****root****/image.png');
//make a yellow box
$transparent = imagecolorallocate($im, 255, 255, 0);
//make the yellow box transparent
imagecolortransparent($im, $transparent);
imagefilledrectangle($im, 200, 115, 300, 137, $transparent);
imagealphablending($im, false);
imagesavealpha($im, true);
header ('Content-Type: image/png');
imagepng($im);
$save = "****root****/test.png";
imagepng($im, $save);
imagedestroy($im);
?>
But the output image has the rectangle retain the yellow colour, rather than turn it transparent.
http://i67.tinypic.com/1ny6j4.png
Where am I going wrong? If I remove...
imagealphablending($im, false);
imagesavealpha($im, true);
I get the transparent box in the center (in the browser), but the rest of the image loses it's transparency and has a white background instead, also when I download that image and open in a photo editor, the transparency box in the center is gone. I have GD libraries set up on my server.
You need to use the gd function imagecolorallocatealpha to create a transparent color.
You could generate the whole image with PHP.
e.g.
<?php
// generate empty image
$image = imagecreate(500, 262);
// define black
$black = imagecolorallocate($image, 0, 0, 0);
// create left part of the bar
imagefilledrectangle($image, 0, 110, 200, 162, $black);
// create right part of the bar
imagefilledrectangle($image, 300, 110, 500, 162, $black);
// save image
imagepng($image, 'outimage.png');
If you want to stick with an existing image
<?php
// generate empty image
$image = imagecreatefrompng('input.png');
// define transparent
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
// create the transparent area
imagefilledrectangle($image, 200, 110, 300, 162, $transparent);
// save image
imagepng($image, 'output.png');
input.png
output.png
You don't need to send a header if you're not submitting the image as a result to a browser.
e.g.
header('Content-Type: image/png');
imagepng($image);
Would display the image in the requesting browser rather than saving it to a file.
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);
?>
i have a image and i wrote some text on this image by using php "imagettftext" function. now i don't know that how it will save automatic.
header('Content-Type: image/png');
// Create the image
$im = imagecreatefromjpeg('image-1.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'www.blockprintsonline.com';
// Replace path by your own font path
$font = 'arial.ttf';
list($width, $height, $type, $attr) = getimagesize($get_image);
$width1=$width*20/100;
$height1=$height*50/100;
$font_size=$width*4/100;
// Add some shadow to the text
//imagettftext($im, 30, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $font_size, 0, $width1, $height1, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
If you want to save the image to a file, check out the docs for the imagepng() function here.
By passing a filename as the second argument it will save the image to a file, e.g:
imagepng($im, "path/to/save/image/in.png");
<?php
// Save the image as 'simpletext.png'
imagepng($im, 'simpletext.png');
// Free up memory
imagedestroy($im);
?>
this is an example on how to save an image.
If you check the documentation for imagepng you would see that you can provide a filename. That would save the image to disk
Please take look the demo site at
http://jsfiddle.net/Alidad/fexGj/
That demo is called "Image title", my next step is to convert both text and images as one group to images so that way i can click right mouse to copy and paste over.
However, there is PHP code that allow me to convert into the image from text but I can't hardly figure out how can I convert from "image title" (demo) into the HTML!
Here is sample PHP code but I can't figure out how to combine it!
<?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);
?>
Have any help me please how to convert this to images from jQuery code!
AM
Easiest way would probably be to just do:
// index.html
<img src="render_image.php?text=Hello World" />
and then:
// render_image.php
<?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, $_GET['text'], $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Using this with slight modifications to your javascript code should work.
More info on using the $_GET variable: http://php.net/manual/en/reserved.variables.get.php