How do I split a .gif file into equal quarters?

Posted on 2005-04-07
Last Modified: 2010-05-18

this is probably a very simple question, but I have yet to work out how to do it!

I need to use PHP to split any given /gif image into equally-sized quarters, on the fly.
I have written a script that pans around a tiled map (each square being 100x87px); I'd now like to split up any given square into 4 equal parts and resize each of these to 100x87px for the script to re-display in order to create a simple zoom. The loss of resolution shouldn't be a problem.

All the files are GIFs.

Any comments would be most appreciated. Thanks.

Question by:lmpc
    LVL 4

    Expert Comment

    Litle less points for a copy&paste answer so here are the guidelines

    You should use the GD library of php

    1: you should create an image object from the origional file
    2: get sizes
    3: calc the x,y,width,hight based on the retrieved sizes in step 2
    4: use imagecopyresampled function ( to copy this part into a new image
    5: write the image object to a file.

    Lookup the GD documentation a bit wich is suited with a lot of samples of functions and you would be able to figure out,


    Author Comment

    Could you give an example of how to use the imagecopyresampled function to extract sections from the original image?
    I've used this function before, but only to resize images (and even then not understanding it properly). The link you gave for it is not very explicit.

    I've increased the points to 150.
    LVL 4

    Accepted Solution

    The official defenition is

    bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

    so example:

    lets say we have an image of 400 by 500
    and we want to copy

    the left right corner

    area coords of old image  for new image would be


    Notice that $my_new_image and $my_origional_image should exists

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now