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

Posted on 2005-04-07
Medium Priority
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
  • 2

Expert Comment

ID: 13733731
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 (http://nl3.php.net/manual/en/function.imagecopyresampled.php) 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

ID: 13734877
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.

Accepted Solution

danny_ebbers earned 450 total points
ID: 13735171
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 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