|
|
imagecopy (PHP 3 >= 3.0.6, PHP 4, PHP 5) imagecopy -- Copy part of an image Описаниеbool 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.
Список параметров
dst_im
Destination image link resource
src_im
Source image link resource
dst_x
x-coordinate of destination point
dst_y
y-coordinate of destination point
src_x
x-coordinate of source point
src_y
y-coordinate of source point
src_w
Source width
src_h
Source height
Возвращаемые значения
Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
Jeff
22-Feb-2007 03:27
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):
<?php
$w = imagesx($img);
$h = imagesy($img);
if (!imageistruecolor($img)) {
$original_transparency = imagecolortransparent($img);
if ($original_transparency >= 0) {
$rgb = imagecolorsforindex($img, $original_transparency);
$original_transparency = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue'];
imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0));
}
$truecolor = imagecreatetruecolor($w, $h);
imagealphablending($img, false);
imagesavealpha($img, true);
imagecopy($truecolor, $img, 0, 0, 0, 0, $w, $h);
imagedestroy($img);
$img = $truecolor;
if ($original_transparency >= 0) {
imagealphablending($img, false);
imagesavealpha($img, true);
for ($x = 0; $x < $w; $x++)
for ($y = 0; $y < $h; $y++)
if (imagecolorat($img, $x, $y) == $original_transparency)
imagesetpixel($img, $x, $y, 127 << 24);
}
}
?>
And now $img is a true color image resource
john at mccarthy dot net
17-Jan-2007 05:37
Here is an upgrade of that cool wave function: Double the size of the image, wave it, then resample it down again. This makes even nicer, anti aliased waves.
// So easy and nice!
function wave_region($img, $x, $y, $width, $height,$amplitude = 4.5,$period = 30)
{
// Make a copy of the image twice the size
$mult = 2;
$img2 = imagecreatetruecolor($width * $mult, $height * $mult);
imagecopyresampled ($img2,$img,0,0,$x,$y,$width * $mult,$height * $mult,$width, $height);
// Wave it
for ($i = 0;$i < ($width * $mult);$i += 2)
{
imagecopy($img2,$img2,
$x + $i - 2,$y + sin($i / $period) * $amplitude, // dest
$x + $i,$y, // src
2,($height * $mult));
}
// Resample it down again
imagecopyresampled ($img,$img2,$x,$y,0,0,$width, $height,$width * $mult,$height * $mult);
imagedestroy($img2);
}
To use it in a full image:
wave_region ($oImage,0,0,imagesx($oImage),imagesy($oImage));
gilthans at NOSPAMgmail dot com
14-Jan-2007 11:19
This function does not resize the image. src_w and src_h are only used to define the portion of src_img that are going to be copied.
That is, it will copy src_img from coordinates src_x, src_y until src_w, src_h, and paste them on the dst_img starting from dst_x and dst_y.
If you wish to resize the image before copying it, check out
ImageCopyResized (though note it is slightly slower when you don't need to resize the image).
alex at gateway-productions dot com
08-Jan-2007 03:34
Id just like to make a clarification
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 )
width of src_w and a height of src_h
Will not resize an image when resource src_im and resource dst_im are both defined
this may be a error with gd I believe I am running v2
C. Jansen
16-Aug-2006 11:02
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.
<?php
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );
imagefill( $temp, 0, 0, $white );
imagecopy($temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height);
?>
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?
<?php
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );
imagecopy( $temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height );
imagefill( $temp, 0, 0, $white );
?>
I am using php 5.1.4 with the bundled GD (2.0.28)
administrador(ensaimada)sphoera(punt)com
03-May-2006 03:27
I've made a little function to wave images:
<?php
function wave_region($img, $x, $y, $width, $height,$grade=5){
for ($i=0;$i<$width;$i+=2){
imagecopy($img,$img,
$x+$i-2,$y+sin($i/10)*$grade, $x+$i,$y, 2,$height);
}
}
?>
More functions at http://www.sphoera.com
02-Apr-2006 10:21
Basic way to implement a "crop" feature : given an image (src), an offset (x, y) and a size (w, h).
crop.php :
<?php
$w=$_GET['w'];
$h=isset($_GET['h'])?$_GET['h']:$w;
|
|