Aralık
19
2009

Php ve python ile hareketli gif süresi hesaplama

Bir projemde lazım olmuştu hareketli gif süresinin hesaplanması. Sizin de işinize yararsa ne mutlu bana..

php ile


function gifSuresiniVer( $dosya ) {
    /*
    http://www.w3.org/Graphics/GIF/spec-gif89a.txt burdaki bilgiler dogrultusunda
    gif in nasil bir yapisi oldugunu anlayabiliriz.
    */
	$gif_grafik_kontrol_regex = "/21f904[0-9a-f]{2}([0-9a-f]{4})[0-9a-f]{2}00/";
	$kaynak = file_get_contents($dosya);
	$icerik = bin2hex($kaynak);

	/*butun frame sureleri toplanir*/
	$toplam_sure = 0;
	preg_match_all($gif_grafik_kontrol_regex, $icerik, $eslesenler);
	foreach ($eslesenler[1] as $eslesen) {
		//little-endian hex unsigned int'ler decimal'e cevrilir.
		$sure = hexdec(substr($eslesen,-2) . substr($eslesen, 0, 2));
		if ($sure == 0) {
			$sure = 1;
		}
		$toplam_sure += $sure;
	}

    /*
    sureler 100 luk saniyeler olarak saklanir bu yuzden saniyeye ceviriyoruz.
    */
	$toplam_sure /= 100;

	return $toplam_sure;
}

function gifToplamResim( $dosya ) {
    $kaynak = file_get_contents( $dosya );
    $son = 0;
    $adet = 0;
    while ($adet < 100) {
        $nerede = strpos($kaynak, "\x00\x21\xF9\x04", $son);
        if ( $nerede === false ) {
                break;
        } else {
                $son = $nerede + 1;
                $nerede2 = strpos( $kaynak, "\x00\x2C", $son );
                if ( $nerede2 === false ) {
                        break;
                } else {
                        if ( $nerede + 8 == $nerede2 ) {
                                $adet ++;
                        }
                        $son = $nerede2 + 1;
                }
        }
    }

    return $adet;
}

function hareketliGifSureVer( $image_filename ) {
	$frame_sayisi = gifToplamResim($image_filename);
	if ($frame_sayisi == 0) {
		return false;
	}
	$sure = gifSuresiniVer($image_filename);
	return $sure;
}

$dosya = "ornek.gif";
echo hareketliGifSureVer($dosya);

test edecekseniz $dosya = "ornek.gif"; ( satır 64) yerine olusturduğunuz php dosyası ile aynı klasorde olan bir hareketli gif adı yazınız.

python ile

#!/usr/bin/python
# -*- coding: utf-8 -*-
from Image import open

dosya = "ornek.gif"
resim = open(dosya)
resim.seek(0)
sure = 0
try:
    while 1:
        sure += resim.info["duration"]
        resim.seek(resim.tell() + 1)
except EOFError:
    pass
print sure

test edecekseniz dosya = "ornek.gif" ( satır 5) yerine olusturduğunuz py dosyası ile aynı klasorde olan bir hareketli gif adı yazınız.

Python da bu iş için özel fonksiyonlar olduğu için bana pek bi iş düşmedi :D .

Etiketler: , , ,
Kategori Programlama

Follow comments via the RSS Feed | Yorum Yap | Trackback URL

1 Yorum to "Php ve python ile hareketli gif süresi hesaplama"

  1. online wrote:

    cok tesekkurler ilginc

Yorum Yapın

 
Powered by Wordpress and MySQL. Original theme by openark.org - customization by me :)