|
|
 |
imagecopy (PHP 3 >= 3.0.6, PHP 4, PHP 5) imagecopy -- Copy part of an image Descriptionint imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
Copy a part of src_im onto
dst_im starting at the x,y coordinates
src_x, src_y with
a width of src_w and a height of
src_h. The portion defined will be copied
onto the x,y coordinates, dst_x and
dst_y.
User Contributed Notes
imagecopy
slavo at polovnictvo-mn dot sk
20-Mar-2005 06:25
<?
$max_rating = 5;
$image_rating_width = 52;
$sirka_null = $image_rating_width / $max_rating * $_REQUEST['rating'];
$rating = imagecreatetruecolor(52,12);
$nula = imagecreatefromjpeg("0.jpg");
$full = imagecreatefromjpeg("5.jpg");
imagecopy($rating, $full, 0, 0 , 0, 0, $sirka_null, 12);
imagecopy($rating, $nula, $sirka_null, 0 , $sirka_null, 0, (52-$sirka_null+1), 12);
header("Content-Type: image/jpeg");
imagejpeg($rating);
imagedestroy($rating);
exit;
?>
0.jpg is equal in size to 5.jpg (foe example 5 stars in image)
0.jpg has 0 filled stars as rating
5.jpg has 5 filled stars as rating
my images have size 52x12 px
You just put them together by imagecopy function :-)
Borszczuk
04-Jan-2005 09:30
I got some comments to Mirror() function given below by lecoguic at yahoo dot fr. First, it's quite good example of coding-without-bit-of-design approach ;) Lecoguic loops by all pixels and copy it each by each. Not good. The *only* reason I could find this approach explained is that he wanted MIRROR_BOTH to be handled. So the code looks quite ok, but performs quite poor. Below is much faster flipping function, blitting whole strips at once so it much more faster. $imgsrc is imagehandle. Function returns image handle to newly created flipped image.
define("IMAGE_FLIP_HORIZONTAL", 1);
define("IMAGE_FLIP_VERTICAL", 2);
define("IMAGE_FLIP_BOTH", 3);
function ImageFlip($imgsrc, $type)
{
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$imgdest = imagecreatetruecolor($width, $height);
switch( $type )
{
// mirror wzgl. osi
case IMAGE_FLIP_HORIZONTAL:
for( $y=0 ; $y<$height ; $y++ )
imagecopy($imgdest, $imgsrc, 0, $height-$y-1, 0, $y, $width, 1);
break;
case IMAGE_FLIP_VERTICAL:
for( $x=0 ; $x<$width ; $x++ )
imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);
break;
case IMAGE_FLIP_BOTH:
for( $x=0 ; $x<$width ; $x++ )
imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);
$rowBuffer = imagecreatetruecolor($width, 1);
for( $y=0 ; $y<($height/2) ; $y++ )
{
imagecopy($rowBuffer, $imgdest , 0, 0, 0, $height-$y-1, $width, 1);
imagecopy($imgdest , $imgdest , 0, $height-$y-1, 0, $y, $width, 1);
imagecopy($imgdest , $rowBuffer, 0, $y, 0, 0, $width, 1);
}
imagedestroy( $rowBuffer );
break;
}
return( $imgdest );
}
cod at crescentofdarkness dot cjb dot net
20-Dec-2004 11:45
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.
Try it on a color-to-transparent gradient!
<?php
function pngcolorizealpha($file, $color) {
$background = array(
'red' => hexdec(substr($color,0,2)),
'green' => hexdec(substr($color,2,2)),
'blue' => hexdec(substr($color,4,2))
);
$im1 = imagecreatefrompng($file) or die('Cannot Initialize new GD image stream');
$im2 = imagecreatetruecolor(imagesx($im1), imagesy($im1));
$col1 = imagecolorallocate($im2, $background['red'], $background['green'], $background['blue']);
imagefill($im2,0,0,$col1);
for ($i=0; $i< imagecolorstotal($im1); $i++) {
$foreground = imagecolorsforindex($im1, $i);
$r = (($foreground['red'] / 127) * (127 - $foreground['alpha'])) + (($background['red'] / 127)* $foreground['alpha']);
$g = (($foreground['green'] / 127) * (127 - $foreground['alpha'])) + (($background['green'] / 127)* $foreground['alpha']);
$b = (($foreground['blue'] / 127) * (127 - $foreground['alpha'])) + (($background['blue'] / 127)* $foreground['alpha']);
imagecolorallocate($im2, $r,$g,$b);
}
imagecopy($im2, $im1, 0, 0, 0, 0, imagesx($im1), imagesy($im1));
header ("Content-type: image/png");
imagepng($im2);
imagedestroy($im1);
imagedestroy($im2);
}
?>
lecoguic at yahoo dot fr
30-May-2004 06:25
Here is a function to flip an image using imagecopy, just replace SRC_IMAGE and DEST_IMAGE with your own filename
(works only for jpg source image)
<?
define("MIRROR_HORIZONTAL", 1);
define("MIRROR_VERTICAL", 2);
define("MIRROR_BOTH", 3);
function Mirror($src, $dest, $type)
{
$imgsrc = imagecreatefromjpeg($src);
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$imgdest = imagecreatetruecolor($width, $height);
for ($x=0 ; $x<$width ; $x++)
{
for ($y=0 ; $y<$height ; $y++)
{
if ($type == MIRROR_HORIZONTAL) imagecopy($imgdest, $imgsrc, $width-$x-1, $y, $x, $y, 1, 1);
if ($type == MIRROR_VERTICAL) imagecopy($imgdest, $imgsrc, $x, $height-$y-1, $x, $y, 1, 1);
if ($type == MIRROR_BOTH) imagecopy($imgdest, $imgsrc, $width-$x-1, $height-$y-1, $x, $y, 1, 1);
}
}
imagejpeg($imgdest, $dest);
imagedestroy($imgsrc);
imagedestroy($imgdest);
}
Mirror(SRC_IMAGE, DEST_IMAGE, MIRROR_HORIZONTAL);
print "<img src='SRC_IMAGE'>";
print "<br><br>";
print "<img src='DEST_IMAGE'>";
?>
RT
09-May-2004 05:09
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
function colorize($src_path, $dest_path, $hex_color) {
$src = imagecreatefrompng($src_path);
$w = imagesx($src);
$h = imagesy($src);
$dest = imagecreatetruecolor($w, $h);
imagealphablending($dest, false);
$red = hexdec(substr($hex_color, 1, 2));
$green = hexdec(substr($hex_color, 3, 2));
$blue = hexdec(substr($hex_color, 5, 2));
for ($i = 0; $i < $h; $i++) { for ($j = 0; $j < $w; $j++) { $retrieved_color = imagecolorat($src, $j, $i);
$rgba_array = imagecolorsforindex($src, $retrieved_color);
$alpha = $rgba_array['alpha'];
$color_to_paint = imagecolorallocatealpha($dest, $red, $green, $blue, $alpha);
imagesetpixel($dest, $j, $i, $color_to_paint);
}
}
imagesavealpha($dest, true);
imagepng($dest, $dest_path);
}
?>
pmidden at gmx dot net
27-Sep-2003 11:47
I've found a convenient way to "blit" one image into another. Using the following Code you can copy one image into another, leaving out the pixels with the specified transparent color. I'm using PNG but by replacing all PNG functions by JPEG or GIF functions you are able to use those formats as well:
<?php
$background = imagecreatefrompng("background.png");
$insert = imagecreatefrompng("insert.png");
imagecolortransparent($insert,imagecolorexact($insert,255,0,255));
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);
imagecopymerge($background,$insert,0,0,0,0,$insert_x,$insert_y,100);
imagejpeg($background,"",100);
?>
matheus at slacklife dot com dot br
12-Sep-2003 06:24
// Image Resize
function createthumb($IMAGE_SOURCE,$THUMB_X,$THUMB_Y,$OUTPUT_FILE){
$BACKUP_FILE = $OUTPUT_FILE . "_backup.jpg";
copy($IMAGE_SOURCE,$BACKUP_FILE);
$IMAGE_PROPERTIES = GetImageSize($BACKUP_FILE);
if (!$IMAGE_PROPERTIES[2] == 2) {
return(0);
} else {
$SRC_IMAGE = ImageCreateFromJPEG($BACKUP_FILE);
$SRC_X = ImageSX($SRC_IMAGE);
$SRC_Y = ImageSY($SRC_IMAGE);
if (($THUMB_Y == "0") && ($THUMB_X == "0")) {
return(0);
} elseif ($THUMB_Y == "0") {
$SCALEX = $THUMB_X/($SRC_X-1);
$THUMB_Y = $SRC_Y*$SCALEX;
} elseif ($THUMB_X == "0") {
$SCALEY = $THUMB_Y/($SRC_Y-1);
$THUMB_X = $SRC_X*$SCALEY;
}
$THUMB_X = (int)($THUMB_X);
$THUMB_Y = (int)($THUMB_Y);
$DEST_IMAGE = imagecreatetruecolor($THUMB_X, $THUMB_Y);
unlink($BACKUP_FILE);
if (!imagecopyresized($DEST_IMAGE, $SRC_IMAGE, 0, 0, 0, 0, $THUMB_X, $THUMB_Y, $SRC_X, $SRC_Y)) {
imagedestroy($SRC_IMAGE);
imagedestroy($DEST_IMAGE);
return(0);
} else {
imagedestroy($SRC_IMAGE);
if (ImageJPEG($DEST_IMAGE,$OUTPUT_FILE)) {
imagedestroy($DEST_IMAGE);
return(1);
}
imagedestroy($DEST_IMAGE);
}
return(0);
}
} # end createthumb
schdenis at telecom dot by
02-May-2003 05:02
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.
mbostrom at paragee dot com
30-Nov-2002 02:56
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.
Then:
$board = imagecreatefrompng ("board.png");
$pawn = imagecreatefrompng ("pawn.png");
imagecolortransparent ($pawn, imagecolorat ($pawn, 0, 0));
imagecopy ($board, $pawn, $x, $y, 0, 0, $pawnWidth, $pawnHeight);
imagedestroy ($pawn);
adam at shallimus dot com
04-Oct-2002 08:47
One way 'round the even/odd image size problem would be to use bcdiv.
robert at scpallas dot com
08-Feb-2002 10:33
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.
jsnell at networkninja dot com
17-Feb-2001 09:09
Want to make an image tile properly? This is accomplished by swapping all four quadrants of the image. Here is some sample code that uses imagecopy to do it:
function backgroundify(&$image)
{
$ix = imagesx($image);
$iy = imagesy($image);
$ixhalf = floor($ix / 2); // DON'T USE ON IMAGES WITH ODD SIZES!
$iyhalf = floor($iy /2);
$panel_temp = ImageCreate ($ix, $iy) or die("Cannot Initialize new GD image stream");
imagecopy($panel_temp, $image, 0,0,0,0,$ix, $iy);
imagecopy($image, $panel_temp, 0,0,$ixhalf,$iyhalf, $ix-$ixhalf, $iy-$iyhalf); // move bottom right to top left
imagecopy($image, $panel_temp, $ixhalf,$iyhalf, 0,0,$ix-$ixhalf, $iy-$iyhalf); // top left to bottom right
imagecopy($image, $panel_temp, $ixhalf, 0,0,$iyhalf,$ix-$ixhalf, $iy-$iyhalf); // bottom left to topo right
imagecopy($image, $panel_temp, 0,$iyhalf, $ixhalf,0,$ix-$ixhalf, $iy-$iyhalf); // top left to bottom right
}
| |