User Contributed Notes 25 notes

I have a few remarks om the mirror-function:The cases horizontal and vertical are switched.1 = vertical and 2 = horizontal.When I used it there appeared a black lining of 1 pixel on the side or on the top of the picture.To remove it the function becomes as follows:

I came across the problem of having a page where any image could be uploaded, then I would need to work with it as a true color image with transparency. The problem came with palette images with transparency (e.g. GIF images), the transparent parts changed to black (no matter what color was actually representing transparent) when I used imagecopy to convert the image to true color.

To convert an image to true color with the transparency as well, the following code works (assuming $img is your image resource):

I have created a PHP function which performs the standard 9-Slice scaling technique. This is extremely useful for thumbnail shadow scaling, and anything involving skinning. Feel free to pick apart and use

Note: instead of specifying margins, my 9-slicing routine uses a centered-rectangle concept... as input you provide the image (as a resource), the x and y coords of the rectangle, and the width and height of the rectangle.

The $src_im parameter should be an image resource. This script was written for 9-slicing translucent PNG images, and has only been tested with translucent PNG images, however it should work with other image types (possibly requiring some modification)

so if your source image was 400 x 400, you needed a 24 pixel margin on all sides, and your target size was 800 x 500, you would use the following parameters:

Here is some simple code for resizing an uploaded image and inserting a watermark (from a 24-bit PNG) on the bottom right of it. In this case, the water mark was a diagnol band that said "SOLD" across it. The code that verifies the uploaded image is the correct type has been omitted:

While replying to a post in a support forum I noticed something odd about imagecopy(). The first snippet (should) create an image object, allocate a colour resource within that image, fill the background with the allocated colour and then copy another, cropped to fit, image onto it.

But this produces a black background. I noticed taking away the imagefill() call yields the same results. The solution was to call imagefill() after the imagecopy(). Thinking linearly I would have guessed this to cover the previously copied image in white but it doesn't. I guess GD uses a layer system? Is this correct?

This function will put a truecolor png with transparency over a custom color backgorund.

The image will be gracefully blended with the background color using the alpha channel for each color.

In real world we'd just mix foreground and backgorund colors looking at their percentages (i.e. 20% of background + 80% of foreground)Here we have to calculate this for each r, g and b value of each color, and we have to use 127 instead of 100, because alpha channel goes from 0 to 127.

Although the following function doesn't use imagecopy(), I thought it might help in related tasks. Please see the code comments for details of it's operation. I made this function to assist in creating images using multiple "layers". For example if you wanted to dynamically create a logo image with seperate colors for say the logo itself and a glow around the logo, these steps would be followed:

-Using an image editor (like Photoshop), create a png-24 image with just the logo on a transparent background. The logo can be any color or multiple colors, but the final image created by this function will be of a single color.

-Create a similar image with just the glow (no logo)

-Create a background image

-Apply this colorize() function to the logo image and the glow image with your desired color for each.

-You can now use imagecopy() to merge all three into a single image ready for a browser.

Here's the code

<?php

/*======Colorize=====(requires GD 2.0.1 or greater)This function requires the following arguments: $src_path = A string representing the relative path to the src image. Ex: "images/myimage.png". This image must be a png-24 with an alpha channel. $dest_path = A string representing the relative path to the image to be created. $hex_color = A string representing a color in html format, including the # sign. Ex: "#D2E5FF"

This function examines the transparency of the source image, pixel by pixel, and creates a newone-color image with this same "transparency map".====================*/

function colorize($src_path, $dest_path, $hex_color) {

//get the png-24 image - it must have an alpha channel for this funciton to be effective$src = imagecreatefrompng($src_path);

//this must be set to false in order to be able to overwright the defualt black pixels of the background with our new //transparent pixels. Otherwise our new pixel would just be applied on top of the black.imagealphablending($dest, false);

As you probably know 'gif' is a paletted image, that is why if you want to copy one 'gif' onto another 'gif' using ImageCopy you need to create a paletted destination image using (ImageCreate), not ImageCreateTrueColor.

If you are getting an error when using ImageCopy(), be sure that both images are of the same type - either True Color or Palette.GD 1.x can copy images of different types, but with GD 2.0 this will cause an error.

sorry - forgot to fill in my email...Note that ImageCreateFromJPEG always creates a True Color Image.You can use ImageCreateTrueColor() instead of Image Create() to solve this problem.

If you want to copy a non-rectangular (hence transparent) image onto a background (for example, a pawn onto a chessboard) do the following:

First, create the pawn image pawn.png in your favorite graphics program. Do NOT make the image transparent, instead, give it a distinct solid background color. You will flag this color as transpernt inside PHP, otherwise imagecopy will not honor the transparency.