百科狗-知识改变命运!
--

imagecopy() - gd函数(图像处理)

乐乐12个月前 (11-21)阅读数 10#技术干货
文章标签图像

imagecopy()

(PHP 4, PHP 5, PHP 7)

imagecopy() - gd函数(图像处理)

拷贝图像的一部分

说明

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): bool

将$src_im图像中坐标从$src_x,src_y开始,宽度为$src_w,高度为$src_h的一部分拷贝到$dst_im图像中坐标为$dst_x和$dst_y的位置上。

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: 
There is function to crop blank edges from image. 
Simple and basic image cropping: 
concerning the previous post of Borszczuk and the function to mirror images:
There´s a way better (and faster) method for this task with imagecopyresampled. 
Regarding the image_flip function discussed in the notes here, don't forget to support transparency.
AFTER: 
$imgdest = imagecreatetruecolor($width, $height); 
ADD:
imagealphablending($imgdest, false);
imagesavealpha($imgdest, true);
In another post here it mentioned you didn't have to use the imagesavealpha function, but I found without it the background transparency can turn the background canvas black.
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):

And now $img is a true color image resource
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 
Here a function to make holes into images:
// Set the alpha channel for a part of an image (it ignores the canvas alpha atm).
// $img_canvas - 32-bit true color image w/ alpha channel
// $img_mask - 8-bit gray scale image (white parts will be masked transparent in the canvas).
// This relies on the current pixel format:
// (high byte) -> (alpha channel} {red} {green} {blue} 
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: 
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?

I am using php 5.1.4 with the bundled GD (2.0.28)
This is based on the Skew function from designerkamal at gmail dot com.
This is a function for skewing images in PHP with anti-aliasing. It works with alpha PNG images.
Warning: the bigger the image you skew, the longer it will take to process. It's about 3 times longer than without anti-aliasing. 
Skewing images in PHP... 
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: 
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.
I used this to watermark images. This is the function I wrote:

Usage:
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)