How do I determine how many times a gif loops using PHP - php

I'd like to know [if there is] a simple way to find out how many times an animated gif loops, using PHP.
As far as I can tell this information is encoded into the header of the gif binary (after a string which is something like "NETSCAPE2.0", for what it's worth), but I've no idea how to read that (in either PHP or any text editor, Sublime gives me what looks like hex codes and TextEdit gives me nonsense).
I'd assume I'd need to read the file look for the above string and hopefully the text after it would be an integer defining the number of loops. Maybe?
Any suggestions? I'm happy to play around with any wild ideas!
Thanks.

There's some information on the gif format here. It seems to include the information about animation:
http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
and this is more specific about what you asked about
http://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html

I found this class GifFrameExtractor From How to extract frames of an animated GIF with PHP
You can get separately an array of images and an array of durations:
$frameImages = $gfe->getFrameImages();
$frameDurations = $gfe->getFrameDurations();
Combining those two method $frameImages*$frameDurations will give you the time for one iteration.
Compare it with the time from the beginning of animation to now.

you can't achieve this without writing some js code...
but you must know/calculate gif's time length for 1 loop.
in your form add
<input id='looper' type='hidden' name='loopingTime' value='0'/>
add function
function loaded(){
$('#looper').val($('#looper').val()+1);
//assuming that calculating time with precision 1second is enough to determine number of loops
setInterval(loaded,1000);
}
add call to this method when gif starts animating
<img id="photo"
onload='loaded(this.id)'
src="a_gif_animation.gif"
alt="this is some alt text"
title="this is some title text" />
in your action method you'll have a parameter loopingTime containig number of seconds played by gif, but youll have to calculate gif loop time somehow.

Related

extract info from jpeg with PHP

I want to extract variable lengths of information from a jpeg-file using PHP, but it is not exif-data.
If I open the jpeg with a simple text editor, I can see that the wanted informations are at the end of the file and seperated by \00.
Like this:
\00DATA\00DATA00DATA\00DATA\000\00DATA
Now if I use PHP's file_get_contents() to load the file into a string, the dividers \00 are gone and other symbols show up.
Like so:
ÿëžDATADATADATADATADATA ÿÙ
Could somebody please eplain:
Why do the \00 dividers vanish?
How to get the informations using PHP?
EDIT
The question is solved, but for those seeking a smarter solution, here is the file I try to obtain the DATA parts from: https://www.dropbox.com/s/5cwnlh2kadvi6f7/test-img.jpg?dl=0 (yes I know its corrupted)
Use instead $data = exif_read_data("PATH/some.jpg") it will give you all headers data about image, you can check its manual here - http://php.net/manual/en/function.exif-read-data.php
I came up with a solution on my own. May not be pretty, but works for me.
Using urlencode(file_get_contents()) I was able to retrieve the \00 parts as %00.
So now it reads like this:
%00DATA%00DATA%00DATA%00DATA%000%00DATA
I can split the string at the %00 parts.
I am going to accept this answer, once SO lets me do so and nobody comes up with a better solution.

regex in order to write only tags (html,php)

I have an HTML document which is pretty long. It has a table that may vary between 1000 and 1200 pixels wide (it changes each day). Lots of text, tables and sometimes embedded PDFs.
I want to display on another page a short preview (like on online newspaper, where you can find title, a few sentences, maybe an image and then a link to the complete article).
First problem: the page where I want the preview is only 800 pixel wide.
My first idea was (in order to display only 10 sentences):
$lineswritten=0;
$stream=fopen($document,"r");
while ((($line = fgets($stream)) !== false)&&($lineswritten<10))
{
if($lineswritten>=10)
{
echo "$line";
$line=trim($line);
if($line!="") // if line is blank don't count it as text
{
$lineswritten=$lineswritten+1;
}
}
}
fclose($stream);
But I have some problems.
First of all: tags. Both the main and the preview pages are built with tables. If in the first 10 rows of the preview, they open a table but they doesn't close it, all the layout of the preview page is messed up.
I tought of checking for table tags ( and ) with regex but I have not yet studied these expressions.
Is it possible to check for these tags and write only them after row 10?
Second problem.
Images. I may have an image which is really big. Is it possible to retrieve just the image path from a tag? If that could be possible I can check the image dimensions and eventually scale it down.
Third problem
I have pdfs embedded with codes like:
<iframe src="http://docs.google.com/gview? url=http://www.mywebsite.ch/pdffolder/8121202.pdf&embedded=true" style="width:990px; height:700px;" frameborder="0"></iframe>
Obviously width and height are not so easy: they may vary too. Is it possible to recognize strings like this and write them on the preview page with height:200px and fixed width of 700px ?
Thank you very much!
Don't use a regex for manipulating HTML, use php's DOM tools instead.
for instance, the second problem (getting image paths from images) can be solved by using the DOMDocument::getElementByTageName method like so:
$dom = new DOMDocument;
$dom->loadHTML($table);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
//do whatever with the image sorce
}
The intent of this code is clearer, and you don't have to write a very long, complex and hard to manage regex to accomplish it.

how to add a percent symbol to a string value output from sql query

This is probably more simple than I think, but I'm having a really hard time wrapping my head around this. I've been trying to learn how to code after a 10 year hiatus from it, so hacking and codecademy are my teachers. Anyways, ...
What I'm trying to do is simply make a whole number from an sql entry a percentage.
I have a form which when submitted enters an sql value whole number that is used to output a css value. I'm only trying to change the width value from a whole number to a percentage to allow some semblance for a responsive effect.
Here's the php pre-output:
<video id="vp2_html5_rightSidePlaylist_'.$row["id"].'" width="'.$row["playerWidth"].'%" height="'.$row["playerHeight"].'" '.$preload_aux.'><div class="xplaylist">'.$playlist_str.'</div></video>
The output is as follows:
<video id="vp2_html5_rightSidePlaylist_1" width="100" height="297" preload="auto" src="http://localhost:888/test.mp4">
Thanks in advance for any help!
If you are using mysql, you could use the CONCAT() function to add some character to the end of a specific field.
CONCAT(str1,str2,...) - Returns the string that results from concatenating the arguments.
For example -
SELECT `id`,`title`,CONCAT(`percentage`,'%') as percentage FROM `items` LIMIT 100
Now, what ever value was stored in the percentage field will have a % character appended to it in the result set.
I guess this is not an issue of your coding.
May I assume that you got this:
<video id="vp2_html5_rightSidePlaylist_1" width="100" height="297" preload="auto" src="http://localhost:888/test.mp4">
..via a tool like firebug/developer-tools?
When yes, this is not the original source, there has been a cleanup of invalid parts.
One of them is the percent-sign, it's not valid inside the width-attribute. To get the desired result, use css instead:
'style="width:'.$row["playerWidth"].'%;height:'.$row["playerHeight"].'px;"'
Use this:
Select CONCAT(`playerWidth`,'%') AS playerWidth, id .....etc FROM tableName

Determing div layout dimensions php

I have three div's which are being filled with dynamic text from a database. The div #container is a fixed height and width where the text inside wraps. The three divs are different font sizes. Any of the three div's could have enough text to exceed the container size. I need to determine if the text exceeds the container size and at which letter in which div it occurs. The extraneous text will then be wrapped in something like <span class=hide">text here</span>
<div id="container">
<div id="first"><?php echo $arr['first'] ?></div>
<div id="mid"><?php echo $arr['mid'] ?></div>
<div id="last"><?php echo $arr['last'] ?></div>
</div>
I'm thinking this is impossible to do in PHP as the styling is done client side. Maybe there is a way to fake it? Though that could get ugly really fast.
I'm trying really hard not to do it in javascript because this calculation will be done about 10 times per page viewed. Please don't tell me it's impossible to do in PHP, there's always a way.
Any ideas?
Just in case you decide that client-side makes more sense for you I put together a fiddle. I realize you want to avoid client-side, but you mentioned this would be happening ten times which honestly is very little these days with how much js speed has increased in browsers. It is also a much simpler problem client side.
http://jsfiddle.net/JSRtk/
Basically you detect if the container is overflown. If so you display a 'read more' button. When clicked it will expand the container to show all text and go away.
$('#container > div').each( function() {
if (checkOverflow(this)) {
console.log('overflow detected in ' + $(this).attr('id'));
$(this).after('<p>Read more...</p>');
}
});
$('p').live('click', function() {
$(this).prev('div').css('height', 'auto');
$(this).hide();
});
function checkOverflow(el)
{
var curOverflow = el.style.overflow;
if ( !curOverflow || curOverflow === "visible" )
el.style.overflow = "hidden";
var isOverflowing = el.clientWidth < el.scrollWidth
|| el.clientHeight < el.scrollHeight;
el.style.overflow = curOverflow;
return isOverflowing;
}
Do you have to wrap it in a span for a purpose (i.e. crawlers/seo)? If not you could either set the CSS on the div with a fixed width to have overflow hidden, alternatively, you could figure out how many characters roughly fit in that width (count them) then use strlen() and substr() like so
<?php
$string = "This is a string thats too long to fit";
if(strlen($string) > 20)
echo substr($string,0,20);
else
echo $string;
?>
There is no way for you to calculate the size of a display element in PHP since it is run on the server and not on the client, and it's the client that renders the HTML.
If you have the same container size every time and the same font and font size and styles and everything, you could probably estimate a number of character and cut it off in PHP at that number of characters using substr. But even then, unless you build a table of character sizes or use a monospaced font, there is no way to reliably do what you want.

How do I protect phone number from bots

I want a phone number which display on public page will be protected. Example converts phone number characters to HTML entities and bots can't grab the number in plain text. Let me know the trick.
This is a...passing thought, though I'm not sure how practical it would be:
<span class="protectedNumber" title="01234567890"></span>
css:
span.protectedNumber:before {
content: "Phone number: " attr(title);
}
JS Fiddle demo.
Edited, in response to 'cross browser?' question in comments, to add a jQuery option to assist with those browsers that don't have the ability to deal with css-generated content:
$(document).ready(
function(){
$('.protectedNumber').each(
function(){
$(this).text('Phone number: ' + $(this).attr('title'));
});
});
some ideas
display the phone number as an image
use javascript to create and display the phone number
throw in html tags in between the numbers (e.g. [span]) that visually makes no difference but makes it more difficult for the bot to recognize the phone number
Try writing the number using ASCII:
http://www.ascii.cl/htmlcodes.htm
<html>
<body>112</body>
</html>
The first thing I'd think of is render an image.
Use Javascript to obfuscate
Obfuscate using a php function
Sure just print the phone number using words instead of numbers...
Create an image of the number, this will foil MOST bots, but some may have OCR, so obfuscate it.
ie: Good:
Better:
The 2nd 1 better because like Captcha, the background contains "noise" that makes it hard for OCR enabled bots to harvest, but is as readable to human eyes..
The hard solution would be use Captcha or a simple PHP script to create the picture on the fly, but in most cases, unless you using alot of different #'s the "better" solution above easiest and quickest method, can do easy even in simple program like Paint in 5 min.
For the visually impaired, include a small link to an audio file (mp3) of you saying the number, if it linked properly, and accordingly, it should work.

Categories