My Fine Day My story in every day

2Oct/0812

PHP – How to Merge Gif Animate

หลายวันก่อนมีน้องมาปรึกษาเรื่อง script php กับการ merge ไฟล์รูป gif animate เข้าด้วยกัน
เลยเอามาเขียนลงใน blog ด้วยดีกว่าเผื่อมีใครจะติดปัญหาแบบเดียวกันอยู่

หลายคนคงเคยประสบปัญหาในการรวมรูป gif เข้าด้วยกัน
แล้วรูปกลับไม่ขยับดุ๊กดิ๊กเหมือนเดิม กลับกลายเป็นภาพนิ่งซะงั้น  (x_x)
วันนี้ผมจะมาไขข้อข้องใจให้ครับ

ทำความเข้าใจก่อนว่ารูป gif animate คืออะไร?
จริงๆ แล้วรูป gif animate ก็คือรูปหลายๆ Frame มาวางต่อกันแล้วรวมเข้าเป็นไฟล์เดียว
ทำให้รูปเกิดความเคลื่อนไหว พูดไปแล้วงงป่าวๆ ดูรูปตัวอย่างเลยดีกว่าครับ เช่นรูปนี้

---->


Frame 1              Frame 2               Frame 3

Frame 4                 Frame 5               Frame 6

Frame 7                Frame 8               Frame 9

Frame 10              Frame 11              Frame 12

Frame 13              Frame 14


รู้แล้วว่า gif animate คืออะไร แล้วยังไงต่อ?

ตอนนี้เรารู้แล้วว่า gif animate แบ่งเป็นหลายๆ frame เพราะฉะนั้นถ้าเราใช้พวกคำสั่ง
imagecopymerge ไปตรงๆ เลย php จะทำการรวมให้เราเฉพาะ frame แรกเท่านั้น
ทำให้ผลลัพธ์ที่ได้กลายเป็นภาพนิ่ง (-_-)

ดังนั้น ถ้าเราต้องการรวมรุป gif 2 รูปเข้าด้วยกัน เราต้องแยกรูปที่จะรวมทั้ง 2 รูปออกเป็น frame
ก่อนแล้วแยกเก็บใสโฟลเดอร์ไว้ ผมขอเรียกขั้นตอนนี้ว่าการ split ซึ่งเพื่อนๆ สามารถ download
script ได้ตามลิงก์นี้ครับ http://phpclasses.ca/browse/package/3234.html
** มี bug ในไฟล์ชื่อ decode_a_gif.php บรรทัดที่ 10 แก้โดยเปลี่ยนจาก
fwrite(fopen(($i < 10 ? "frames/0$i_frame.gif":"frames/$i_frame.gif"), "wb"), $arr[$i]);
แก้เป็น
fwrite ( fopen ( ( $i < 10 ? "frames/0$i.gif" : "frames/$i.gif" ), "wb" ), $arr [ $i ] );
Split เสร็จแล้วทำไงต่อ?
เมื่อเรา split ได้รูปเป็นหลายๆ frame มา 2 ชุดแล้ว ให้เราทำการรวมรูป ใน frame ที่เราต้องการ
เช่น สมมุติว่ารูปแรกเราแยกมาเป็น 10 frame รูปที่ 2 แยกมาเป็น 2 frame ถ้าต้องการแทรกรูปที่ 2 ไปที่
frame ที่ 5 ของรูปที่ 1  ก็นำรูป frame ที่ 5  ของรูปที่ 1 ไปรวมกับ frame แรกของรูปที่ 2 เป็นต้น
ซึ่งคำสั่ง ในการรวมรูปในข้นตอนนี้ใช้  imagecopymerge ได้เลยครับ เพราะว่าเป็นการรวม
ภาพนิ่งธรรมดา อ่านดูแล้วอาจจะงงๆ  แนะนำให้ลองทำดูเลยดีกว่าครับ (*_*)

รวมรูปเสร็จแล้วก็มาถึงขั้นตอนสุดท้าย (^_^)V
เมื่อรวมรูปเป็น frameๆ เสร็จแล้ว สุดท้าย เราก็แค่ merge frame เหล่านั้นกลับมาเป็นรูป
gif animate เหมือนเดิมเท่านี้ก็เสร็จแล้วค้าบบพี่น้องงงง download script merge ได้ตาม
ลิงก์นี้เลยครับ  http://phpclasses.sgboards.com/browse/package/3163.html

หวังว่าข้อมูลจะเป็นประโยชน์กับเพื่อนๆ นะครับ
ใครติดปัญหาอะไร หรือมีวิธีอะไรแนะนำที่ดีกว่านี้ก็ Comment ไว้ได้เลยนะครับ
ขอบคุณมากครับ Y(=^=)Y

Comments (12) Trackbacks (0)
  1. Where is the English version?

  2. hS1KcS Thanks for good post

  3. I came here from php.net – in the hope to find a good tutorial about how to merge gif animations with php.
    But sorry I can not read this – when do you think is your english version ready ?

    thanks
    paul

  4. http://www.myfineday.com – great domain name for blog like this)))

  5. Grieved~~
    I can’t read the article.Could you translate it into English?

  6. English version?

  7. [v.2 much better - oh well, maybe not much, but still.. better.]
    [I hope this helps - author plz send me a feedback if u liked:]
    [my comments or sth misunderstood is in "[..]” ]

    PHP – How to Animate and merge GIF?
    Posted in PHP and tagged with gif decode , gif encode , merge gif , PHP , split gif on 10/02/2008 01:54 am[WTF :) i'm not codding during night] by admin
    Several days earlier i wanted to conslut how to make a php script that merge GIF files,animate images, all together.
    If somene have similiar problem plz comment on my blog.

    Many people have experienced continuing difficulties integrating images into a gif.[sth] Today [tonight :) ] i’ll give them a solution.

    *Firt of all you have got to understand how gif is animated?

    The gif itself is a multiple frames. [linked with a delay of ] frames.
    Then joined and pasted them into a single file.
    Let’s make a motion picture. [cuting a crap :) ]
    Take look at a sample like this :
    [pictures]
    [Frame 1 ..to..14]
    [can you see a gif animation?]

    * Know, What if that gif is animate addition to [sth..]?
    Now we know that a gif animated frame is divided into several other pictures.
    So we have to use them in in order:

    php imagecopymerge will, honestly, include only the first frame!
    and make the results to become still (-_-).

    So what to do if we want to include in a gif two different model altogether.
    We need to separate 1 form 2 and form a frame, next go thith them in a separate storage folder. The directory could be also “./” .
    split ,download split, and share it with friends – they can download from now.
    [just guessing a meaning]

    This script is based on old package
    http://phpclasses.ca/browse/package/3234.html

    [Important!]
    Workaround of a a bug: in a file decode_a_gif.php
    **A bug in the file name decode_a_gif.php solve by changing line 10 from:
    fwrite(fopen(($i < 10 ? “frames/0$i_frame.gif”:”frames/$i_frame.gif”), “wb”), $arr[$i]);
    to:
    fwrite ( fopen ( ( $i < 10 ? “frames/0$i.gif” : “frames/$i.gif” ), “wb” ), $arr [ $i ] );
    [I think the usage of $i.gif” is the case here]

    *Ok split is finished. now what?
    When we split. A 1 multiple frame is split to 2 and then we make a frame in which we want.
    For example, suppose that we first form a separate 10 frame Figure 2 is a separate 2 frame To insert Figure 2 to The frame 5 of Figure 1 is a lead frame 5 of Figure 1 to include the first frame of Figure 2.
    The instructions included in condensed form in use now have imagecopymerge
    Because a total i still see regular readers may already be numb.

    *(^_^)V And then the completed form to the final step

    Accompanied by a picture frame, we just finished the final merge frame ,and them returning to form.

    gif animation is done.
    the [convertion] is finished,

    download script merge at site [?].
    http://phpclasses.sgboards.com/browse/package/3163.html

    I really have hope that the information will be useful to you – friends. Got a problem? Any suggestions or comments? or how to get it done better? plz comment!
    Thanks

    [sory that translation is quite poor - i speak taji quite bad (actually I cannot) - i had to ask neighbuour for help.. and he is not able to recognize mouse from keyboard :) - his a bit of n00b, and did a mentioned that i english is not my 1st language not even third - so i guess i'm n00b to :) )]

  8. Thank you very much Lukas Gintowt and Dan (^_^).

  9. Ok, my gif working, but… how to change number of used colors in image gif? How to set 16, 32, 100, 200 colors?
    I need to compress GIF… and set type of compress (dither, pattern or nothing).

    And what is this “Disposal” in Example.php from package?

  10. I have been looking looking around for this kind of information. Will you post some more in future? I’ll be grateful if you will.


Leave a comment


No trackbacks yet.