Adding text to image (animated gif) with PHP - php

I want to allow my website visitors to personalize their banners with their names.
1) The banners are animated GIFs, but the script only seems to output the first frame of the GIF image
2) I need to add a text field to allow users to enter their desired text. After submitting the form, the image will be generated with their text
Thank you!
index.php (below)
<img src="image.php?text=MyNameHere" alt="" />
image.php (below)
<?php
//Image
$rImg = ImageCreateFromGIF("image.gif");
//Definir cor
$cor = imagecolorallocate($rImg, 0, 0, 0);
//Text
imagestring($rImg,5,126,22,urldecode($_GET['nome']),$cor);
//Header output
header('Content-type: image/gif');
imagegif($rImg,NULL,100);
?>

Perhaps with a combination of these:
https://github.com/Sybio/GifFrameExtractor
https://github.com/Sybio/GifCreator

Here's a class file with tons of options for text overlay and/or image overlay.
class.upload.php

Related

How do I extract an image from a MEDIUMTEXT, MyISAM field? [duplicate]

I have a PHP function that does on-the-fly image resizing for thumbnail creation.
I am having trouble as it's just displaying raw image stream instead of the actual image.
My code is using a function called thumbnail:
$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);
I've tried putting in:
header("Content-type: image/jpeg");
However, this just expects the full page to be an image. I have absolutely no idea where to go from here, been working at it for a while. I'd rather not save the image to disk although it's looking like this might be necessary.
You either
Do it the normal way
This mean you point at one url, and serve the contents of one image:
<img src="myimage.php">
and myimage.php is a script that looks like:
header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;
This technique has the advantage of being.. the normal way of doing things.
OR
Output the image inline
Using data uris outputting the contents as a base64 encoded string
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This technique is most appropriate with small images.
It has the advantage of meaning all the images are in the main http request - at the (possibly sizable) disadvantage of making the page harder to edit/build and possibly negating the benefits of browser caching (unless the html page is itself cached).
Being normal is easier
Regarding this statement in the question:
However, this just expects the full page to be an image
That's right - if you do it the normal way you want to point at your php script with the src attribute of an image tag, and server only an image - i.e. the exact same response as if you were pointing at an image file with a browser.
Unless you have a good reason to do things a different way - the "normal" way is a good place to start.
You can point an html img tag to an php file.
<img src='thumbnail.php?file=<?php echo $item['filename']; ?>' />
Then on your php file you display the image and change the headers since all it is doing is displaying an image.
$thumbnail = thumbnail($_GET['filename'], 209, 137);
imagejpeg($thumbnail);
header("Content-type: image/jpeg");
You need to insert the image like you would a normal image in HTML and create the image in a separate PHP file:
image.php
<?php
$img = imagecreate(100,100); //Create an image 100px x 100px
imagecolorallocate($img, 255,0,0); //Fill the image red
header('Content-type: image/jpeg'); //Set the content type to image/jpg
imagejpeg($img); //Output the iamge
imagedestroy($img); //Destroy the image
?>
myWebpage.html
<img src="image.php" />

Creating a dynamic image with php and drupal

I am new to drupal and php. Right now I am trying to customise my own drupal page by editing the page.tpl.php template in a zen sub-theme, and so far all the php code related with data and text are working fine on the page.
However when I am trying to further customise the page by trying to add a dynamic image through php, things are not working. The site now returns a broken link icon which blocks everything else that should be displayed on the page. I have tested the code which generates the dynamic image on an individual php page and it is working well. I wonder how can I resolve this issue and let the dynamic image be properly displayed on my drupal site? Many thanks in advance.
Here is my code
<div id="projects">
<?php
global $node;
$projectname = db_query("SELECT title FROM {node}
where type='project' and uid= :uid", array(':uid' =>$user->uid))- >fetchAll();
if ($projectname){
foreach($projectname as $item) {
print $item->title ;
echo "<br>";
}
}
else{
print "no project";
}
?>
#this is the chunk of codes that generates the dynamic image
<?php
//$image = #imagecreate(200, 20)or die("Cannot Initialize new GD image stream");
$image = imagecreate(200, 20);
$background = imagecolorallocate($image,0,0,0);
$foreground = imagecolorallocate($image,255,255,255);
imagestring($image,5,5,1,"This is a Test",$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>
</div>
The image you generate need to be in its own page. imagejpeg will print the raw image binary output.
For your web html page to display it, you can import it with a basic <img src="">.
Resume:
image.php => render in full binary an image
page.php => standard web html page with <img src="image.php"> somewhere to fetch your dynamically generated image
Edit: I may have read the question to fast, it still not clear for me...
Most common issue are that errors or text outputs have been send with the image and corrupt the raw output. Removing the header to display temporary the output will help showing what have been send. Also care with <?php blocks, a single space before your php code will corrupt the image.
In short: image.php must only return raw binary image data.

Outputting raw image stream rather than jpeg, on-the-fly image resizing

I have a PHP function that does on-the-fly image resizing for thumbnail creation.
I am having trouble as it's just displaying raw image stream instead of the actual image.
My code is using a function called thumbnail:
$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);
I've tried putting in:
header("Content-type: image/jpeg");
However, this just expects the full page to be an image. I have absolutely no idea where to go from here, been working at it for a while. I'd rather not save the image to disk although it's looking like this might be necessary.
You either
Do it the normal way
This mean you point at one url, and serve the contents of one image:
<img src="myimage.php">
and myimage.php is a script that looks like:
header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;
This technique has the advantage of being.. the normal way of doing things.
OR
Output the image inline
Using data uris outputting the contents as a base64 encoded string
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This technique is most appropriate with small images.
It has the advantage of meaning all the images are in the main http request - at the (possibly sizable) disadvantage of making the page harder to edit/build and possibly negating the benefits of browser caching (unless the html page is itself cached).
Being normal is easier
Regarding this statement in the question:
However, this just expects the full page to be an image
That's right - if you do it the normal way you want to point at your php script with the src attribute of an image tag, and server only an image - i.e. the exact same response as if you were pointing at an image file with a browser.
Unless you have a good reason to do things a different way - the "normal" way is a good place to start.
You can point an html img tag to an php file.
<img src='thumbnail.php?file=<?php echo $item['filename']; ?>' />
Then on your php file you display the image and change the headers since all it is doing is displaying an image.
$thumbnail = thumbnail($_GET['filename'], 209, 137);
imagejpeg($thumbnail);
header("Content-type: image/jpeg");
You need to insert the image like you would a normal image in HTML and create the image in a separate PHP file:
image.php
<?php
$img = imagecreate(100,100); //Create an image 100px x 100px
imagecolorallocate($img, 255,0,0); //Fill the image red
header('Content-type: image/jpeg'); //Set the content type to image/jpg
imagejpeg($img); //Output the iamge
imagedestroy($img); //Destroy the image
?>
myWebpage.html
<img src="image.php" />

Freezing GIF images

I have many animated .gifs on a page. I need a script or code that can freeze the animation of these .gifs so they do not animate and freeze on the first frame.
Currently the backgrounds are table cell backgrounds like this:
<td background="animated_background_1.gif">
and
<td style="background-image:url('animated_background_2.gif');">
The HTML used to display image (or as a background image or whatever):
<img src="giftopng.php?image=mypic.gif" alt="" />
The test script called: "giftopng.php"
<?php
//Get image name from URL
$image=$_GET['image'];
//Load the image
$img = ImageCreateFromGif($image);
//Convert to PNG and send it to browser
if($img) {
header("Content-Type: image/png");
ImagePNG($img);
//Clean-up memory
ImageDestroy($img);
}
?>
You could use something like this (source http://php.net/manual/en/function.imagecreatefromgif.php).
If the load is too high then you could cache the static image.
Have a look at http://slbkbs.org/jsgif code, it could help you with this issue.
jsgif is an animated GIF player bookmarklet with support for pausing, going frame-by-frame, playing in reverse, and other features that one might expect from a video player.

Print stylish text on image in php

Is it possible to print html div tag with style on image in PHP?. if not, then what is the alternative way?
Some hosts have ImageMagick for PHP. To add text to your image, take a look at the syntax of the commands here. The example given on that page should help some - it's pretty easy to get text on an image.
The benefits of using ImageMagick over a fixed image is that you can vary the content of the text, which is what you might want (you didn't mention needing a static text; for this, I'd use an image with a transparent background). For more comprehensive font commands, take a look here.
To put a transparent image on top of your base image, take a look at this very nicely designed site.
I'll also give the code presented on that site here:
$photo = imagecreatefromjpeg("original.jpg");
$watermark = imagecreatefrompng("watermark.png");
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
header("Content-Type: image/jpeg");
imagejpeg($photo);
To output the image to a file, please Google that and replace the last two lines of the example given above.
For ImageMagick stuff, take a look here
I hope this helps :-)
James
You can set the image as the background graphic of any div using CSS. Then the text within that div will appear on top of the image.
(CSS)
.mydiv {
background:url(/path/to/image.gif);
width:100px; /* set to width of the image */
height:100px; /* set to height of the image */
}
(HTML)
<div class='mydiv'>Some text here</div>
There is no easy way to print text on images using html/css on server side, because php can't parse html, so you'd better find another solution like php GD.

Categories