FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Image crop





DanielXP
Hi

I need a script that can open a gif file from the server (100x100px) and resize it 10 100x75px.

I only need it to work with gif files.


Also is there a way to add a 1 px solid black border around the edge?

Thanks

Daniel
rickylau
You may find PHP's GD library useful.

Functions you may need:

Opening an image file:
Code:
resource imagecreatefromgif (string $filename)

Creating an image:
Code:
resource imagecreate (int $width, int $height)

Resizing an image:
Code:
bool imagecopyresized (resource $destinationImage, resource $sourceImage, int $destinationX, int $destinationY, int $sourceX, int $sourceY, int $destinationWidth, int $destinationHeight, int $sourceWidth, int $sourceHeight)

Copying an image without resizing:
Code:
bool imagecopy (resource $destinationImage, resource $sourceImage, int $destinationX, int $destinationY, int $sourceX, int $sourceY, int $sourceWidth, int $sourceHeight)

Allocating a colour for an image: (First call will set the background colour)
Code:
int imagecolorallocate (resource $image, int $red, int $green, int $blue)

Saving the image to file:
Code:
bool imagegif (resource $image, string $filename)

Outputting the image to browser:
Code:
bool imagegif (resource $image)


For your question, I would suggest the solutions that way:

To resize an image
(Note that resizing an image from 100x100 to 100x75 changes the aspect ratio of the image)

  1. Load the source image file
  2. Create a new destination image with desired size
  3. Resize and copy the source image to destination image
  4. Save the destination image


To add a 1 pixel black border to an image

  1. Load the source image file
  2. Create a new destination image with the size of source file plus 2 for both width and height
  3. Set the background colour of destination image to black
  4. Copy source image to destination image
  5. Save the destination


Hope this can help you

Ricky
DanielXP
Thanks for your reply rickylau.

I have tryed lots of ways to do this and it keeps saving the 100x75 image as just a black fill.
DanielXP
Its a problem i got with

imagecreatefromgif(

It dont seem to be opening the gif file
TomS
That's because GIF isn't supported in the newer versions of GD.

You should consider using PNGs. They allow much more colors (24bit) and alpha transparency.

EDIT: The english page is much more updatet, than the german one:

php.net wrote:
Note: GIF support was removed from the GD library in Version 1.6, and added back in Version 2.0.28. This function is not available between these versions.


Current version on frihost seems to be 2.0.34 (at least on this server).
So check if the new version is installed on your server with the following
Code:
<?php
phpinfo();
?>
Related topics
E-Cards with PHP & MySQL
PHP Image Upload (MySQL/Folder)
PHP Image suddenly doesn't work?
php image exif functions
mysql/php help please
PHP image
PHP image uploads with progress bar AND resizing? [Resolved]
let me tell you... this is not an image! it's HTML code!
Transparent background image
PHP Image Questions
PHP Image functions
Opening any image with PHP. Couldn't find anywhere.
xMPACT! PHP Image Viewer 1.0
PhP Image gallery?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.