I'm using Creating image function to create an image with exact text message using the following code
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
The output should be like this
Now my question is there any way i can write image over it not only text
so that if i've flag image at same path (flag.gif ) and i would like to write it just after my $message to be like this
so is this possible and how could be ! ~ thanks a lot
Update
based on #MarcB idea of using imagecopy function
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
the output is not true color WHY :(
ANY help about this new problem ~ thanks
With the help of Surreal Dreams and Marc B
This one works fine
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromjpeg("myimage.jpg"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
Output
I've should have learned the following functions
imagecopy
imagecreatefromjpeg
imagecreatefromgif
imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.
To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.
Related
I'm trying to play around with GD, and I'm trying to get it to work with large images. I want a image that's originally 640x640 to resize to 130x130 on my image that I'm creating in GD. However, with my code it just crops 130x130 of the image from the upper left corner. In other words, I don't get the whole image in 130x130. I've been trying every snippet I could find, but still no luck in getting a hold of this. This is the code I have;
header ("Content-type: image/jpeg");
$image1Url = "background.jpg";
$image2Url = "image.jpg";
$image1 = imageCreateFromjpeg($image1Url);
$image2 = imageCreateFromjpeg($image2Url);
imagecopymerge($image1, $image2, 10, 10, 0, 0, 130, 130, 100);
$line1 = "This is the first line";
$line2 = "This is the second line";
$font = "./VERDANA.TTF";
$white = imagecolorallocate($image1, 255, 255, 255);
$yellow = imagecolorallocate($image1, 252, 205, 5);
imagefttext($image1, 14, 0, 150, 110, $yellow, $font, $line1);
imagefttext($image1, 14, 0, 150, 135, $white, $font, $line2);
Imagejpeg ($image1, NULL, 100);
ImageDestroy ($image1);
ImageDestroy ($image2);
I want the image specified as $image2Url to be scaled down to 130x130 no matter what size it's originally is. It's important to me that I maintain the aspect ratio though.
I've been trying different snippets I could find, but still no luck... I've been able to resize the original image to the size I want, but not within the final image in my GD script.
If you're using PHP version >= 5.5 you should use imagescale(). If not, use the following right after loading $image2:
$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);
// then use $image3 instead of $image2
I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.php
I have binary file where is stored image, i try get this image, but display only black color image, what can be wrong with my code or binary file.
<?php
function LoadPNG ($imgname) {
$im = #imagecreatefrompng ($imgname);
if (!$im) {
$im= imagecreate (150, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc= imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('452');
imagepng($img);
imagedestroy($img);
?>
with this code I get I error that can't load file
File: testams.serveriai.lt.lazdynas.serveriai.lt/452
Script: testams.serveriai.lt.lazdynas.serveriai.lt/crypt.php
Your PNG image is corrupt, it has a \n character instead of \r\n, (byte position 5) typically a problem arising from FTP transfering a binary image in text mode from Windows to Unix.
Before messing with PHP, you should check simply that the image is OK, eg adding the .png extension, placing it in a visible folder (in the web server) and browsing it.
Is there a php or javascript method of converting a simple html file with text to a JPEG image and saving it automatically. I would like to be able to pull it into Outlook 2007 as "embedded content" for a company's dynamic disclaimer
Any help would be appreciated
UPDATE
i have found the following script that produces a image with only a string:
<?php
$text = "This is a example";
header("Content-Type: image/png");
$im = #imagecreate(700, 300)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 12, 5, 5, $text, $text_color);
imagepng($im);
imagedestroy($im);
?>
Is there a way to get <br> tags to work in a php string?
use the imageTTFtext() function and concatenation the <br /> tag.
<?php
header(“Content-type: image/gif”);
$string = 'This is another text i want to include';
$im = imagecreate( 400, 200 );
$red = imagecolorallocate($image, 255,0,0);
$blue = imagecolorallocate($image, 0,0,255 );
$font = “/usr/local/jdk121_pre-v1/jre/lib/fonts/LucidaSansRegular.ttf”;
imageTTFtext( $im, 50, 0, 20, 100, $blue, $font, “This is some text!” );
imagegif($im);
?>
I created a canvas with a width of 400 pixels, you'll need a FreeType font in addition to your GD library. with the imageTTFtext() function i defined a width of 50 and angle of 0 of the text. so to make the string more complex, you can place the <br /> tag like this:
imageTTFtext( $im, 50, 0, 20, 100, $blue, $font, “This is some text!<br/>”.$string."some other text" );
Hope you get the picture now...
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!