Freezing GIF images - php

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.

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" />

Adding text to image (animated gif) with 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

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" />

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.

PHP crop and resize image on the fly

I have a web page that displays images that I don't know their size in advance. I was trying to use the GD functions to make the script resize and crop the images from me " Just before they are displayed.. I don't need caches" but I failed.
I need a script that I can call like this
<img src="display.php?src=blablabla&height=100&width=200" ?>
or even by calculating the width and height of css to preserve the proportions and make the image touch the box from inside like
<img src="blabla.jpg" style="height:<?php echo $height; ?>; width:<?php echo width; ?>" />
I don't need any sort of caching. How can I do that ?
WideImage rlz! :)
The resize's like that:
header('Content-type: image/jpeg');
echo WideImage::load('image.jpg')->resize(200, 100)->asString('jpg', 80);
// image.jpg resized at 200x100 with 80% of quality
You'll need to use the first style. Because this would be happening server-side, you can't check the CSS to get the desired size.
You just need to use the GD functions to open the appropriate file, use imagecopyresampled() to resize it, and then output to the buffer using imagejpeg. Don't forget to set the right headers:
header('Content-type: image/jpeg');
OR phpthumb http://phpthumb.sourceforge.net/
Demo is available at: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
You are looking for TimThumb (Demo | Source Code):
Simply copy the source code into a new
document called ‘timthumb.php’, place
it in a folder on your site (ex:
/scripts/) and call the image like
this:
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">

Categories